Skip to content
Advertisement

How to Insert Large Amount of Varbinary(max) Rows Into Table

I use a local SQL Server. I have table with 250k of photos (DropPhotos). Photos are stored in a Varbinary(max) column which is called Content. I want to move all of this rows into another table within same database (BasePhotos) along with some additional columns. I wrote SQL statement like this:

INSERT INTO dbo.BasePhotos (PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType)
    SELECT PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType
    FROM dbo.DropPhotos

When I start execution everything goes normal. After several minutes (5 – 15 mins) my screen starts to turn on and off randomly, then it turns off and after several more minutes my pc goes into reboot. I suspect that moving so much binary data drains all of it RAM and leads to shutdown. I’ve searched online for solution and discovered that there is exist some kind of Bulk Insert. Maybe it could help me? But as far as I know it is used to insert rows from file, not from one table from another.

So my question is quite simple: How to approach inserting large amount of heavy rows into table.

PhotoId is uniqueidentifier

Advertisement

Answer

Update: Please note that OP is asking for moving records in the question, this was later changed to copying records in a comment.

The batch approach discussed above, may be implemented like this:

set nocount on
DECLARE @IdTable TABLE (id uniqueidentifier)

insert into @IdTable(id) 
    select top 100 
        dropPhotos.PhotoId 
    from 
        dbo.DropPhotos dropPhotos
        left join dbo.BasePhotos basePhotos on dropPhotos.PhotoId = basePhotos.PhotoId
    where
        basePhotos.PhotoId is null

while exists(select top 1 1 from @IdTable)
begin
    INSERT INTO dbo.BasePhotos (PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType)
        SELECT PhotoId, Name, Content, AddedDate, CreationDateTime, PhotoType
        FROM dbo.DropPhotos
        WHERE PhotoId IN (select id from @IdTable)

    delete from @IdTable

    insert into @IdTable(id) 
        select top 100 
            dropPhotos.PhotoId 
        from 
           dbo.DropPhotos dropPhotos
           left join dbo.BasePhotos basePhotos on dropPhotos.PhotoId = basePhotos.PhotoId
        where
            basePhotos.PhotoId is null
end
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement