I have a table in Microsoft SQL Server with file path, now I want to store the files as filestream data in the database.
I want to run a bulk update query to convert each row of the table to the corresponding filestream data using the filepath column.
Columns of my table are SrNo, FilePathLocation, FileStreamBlob, Key
.
I am able to run the following query successfully for 1 row but I have over 500+ rows in the table with filepath can anyone please help me with the update query for updating all the rows of the table using the filepath column.
DECLARE @file_stream VARBINARY(MAX)
DECLARE @command nvarchar(1000)
DECLARE @filePath NVARCHAR(128)
SET @filePath = (SELECT FilePathLocation FROM ScanFileTable WHERE SrNo = 2077)
SET @command = N'SELECT @file_stream1 = CAST(bulkcolumn AS varbinary(MAX))
FROM OPENROWSET(BULK ''' + @filePath + ''', SINGLE_BLOB) ROW_SET'
EXEC sp_executesql @command, N'@file_stream1 VARBINARY(MAX) OUTPUT',@file_stream1 =@file_stream OUTPUT
UPDATE ScanFileTable
SET FileStreamBlob = @file_stream
WHERE SrNo = 2077