Hello,
I have an old database table as follows:
create table dbo.Books_OLD (
Id int identity not null constraint PK_Books_OLD_Id primary key (Id),
Title nvarchar (200) not null,
ImageContent varbinary (max) null,
ThumbnailContent varbinary (max) null
)
insert into dbo.Books_OLD (Title, ImageContent, ThumbnailContent) values ('Title 1', 0x11223344, 0x11223344);
insert into dbo.Books_OLD (Title, ImageContent, ThumbnailContent) values ('Title 2', null, 0x11223344);
insert into dbo.Books_OLD (Title, ImageContent, ThumbnailContent) values ('Title 3', 0x11223344, null);
insert into dbo.Books_OLD (Title, ImageContent, ThumbnailContent) values ('Title 4', null, null);
And I have a new database with the following tables:
create table dbo.Books (
Id int identity not null constraint PK_Books_Id primary key (Id),
Title nvarchar (200) not null
)
create table dbo.Files (
Id int identity not null constraint PK_Files_Id primary key (Id),
Content varbinary (max) null,
Name nvarchar (280) null
)
create table dbo.BookFiles (
BookId int not null,
FileId int not null,
constraint PK_BookFiles_Id primary key (BookId, FileId)
)
alter table dbo.BookFiles
add constraint FK_BookFiles_BookId foreign key (BookId) references Books(Id) on delete cascade on update cascade,
constraint FK_BookFiles_FileId foreign key (FileId) references Files(Id) on delete cascade on update cascade;
I am adding books and files to these tables from an old database:
insert into MyDb.Books (Title)
SELECT
Title,
ImageContent,
ThumbnailContent
FROM MyDb.Books_OLD
For each new book I need:
-
Check if ImageContent is null.
If it is not null then create a new BookFile / File.
The ImageContent will be File.Content and "Image" will be File.Name. -
Check if ThumbnailContent is null.
If it is not null then create a new BookFile / File.
The ThumbnailContent will be File.Content and "Thumbnail" will be File.Name.
How can I do this?
Thank You