Solved

Inserting Binary Data

Posted on 2010-08-18
5
913 Views
Last Modified: 2012-05-10
Hi,

I have a table that contains that contains the following columns

FileKey
FileName
FilePath

What I essentially would like to do is to add a fourth column that contains the binary data of the file.

Trouble is there are well over 500 files listed so I’m looking for help in creating a script that would do the update automatically?

Not sure if the best way is to use a scripting language (asp.net) to somehow loop through the records?

Any suggestions appreciated.
0
Comment
Question by:andyw27
  • 3
5 Comments
 
LVL 10

Expert Comment

by:Jini Jose
Comment Utility
yes you can do that.
just loop through the databse records and get the filename
then find the file then save to database.
0
 
LVL 18

Expert Comment

by:Matthew Kelly
Comment Utility
This should work (Using FETCH to store values in variables): http://msdn.microsoft.com/en-us/library/ms180152.aspx
DECLARE @FileKey int, @FileName varchar(50), @FilePath varchar(50);



DECLARE file_cursor CURSOR FOR

SELECT FileKey, FileName, FilePath FROM FileTable;



OPEN file_cursor;



-- Perform the first fetch and store the values in variables.

-- Note: The variables are in the same order as the columns

-- in the SELECT statement. 



FETCH NEXT FROM file_cursor

   INTO @FileKey, @FileName, @FilePath;



-- Check @@FETCH_STATUS to see if there are any more rows to fetch.

WHILE @@FETCH_STATUS = 0

BEGIN



   -- Concatenate and display the current values in the variables.

   INSERT INTO FileTable (FileBinary) VALUES (LOAD_FILE(CONCAT(@FilePath, '\\', @FileName))) WHERE FileKey=@FileKey;



   -- This is executed as long as the previous fetch succeeds.

   FETCH NEXT FROM file_cursor

   INTO @FileKey, @FileName, @FilePath;

END



CLOSE file_cursor;

DEALLOCATE file_cursor;

GO

Open in new window

0
 
LVL 18

Expert Comment

by:Matthew Kelly
Comment Utility
Actually, LOAD_FILE is only in MySQL, so that insert would need to be replaced with the following.

See Example D on this page: http://msdn.microsoft.com/en-us/library/ms190312.aspx
INSERT INTO FileTable  (FileBinary)

SELECT *

FROM OPENROWSET (

BULK @FilePath + '\\' + @FileName,

SINGLE_BLOB) AS BinFile

Open in new window

0
 

Author Comment

by:andyw27
Comment Utility
Thanks the suggestion.  I've given it a go, however I get a few errors on this bit:

 INSERT INTO FileTable  (FileBinary)
SELECT *
FROM OPENROWSET (
BULK @FilePath + '\\' + @FileName,
SINGLE_BLOB) AS BinFile

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@FilePath".

The individual error messages are:

Incorrect syntax near '@FilePath', Expecting STRING, or TEXT_LEX

Invalid Object name 'SINGLE BLOB'

(My FileBinary field is of type varbinary(MAX))
0
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 500 total points
Comment Utility
http://www.sqlservercentral.com/Forums/Topic469199-5-1.aspx

Sorry, OPENROWSET doesn't take a variable, so it must be done with EXEC, creating dynamic SQL
Set @sql = '
             INSERT INTO FileTable  (FileBinary)
SELECT *
FROM OPENROWSET (
BULK ''' + @FilePath + '\\' + @FileName + ''',
SINGLE_BLOB) AS BinFile            
'
        Exec (@sql)

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Backup 24 69
Update foreign key reference after insert 9 30
Help with SQL Query 23 39
JavaScript error 1 33
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now