Question

I currently have a varchar(255) column that stores the path to a small image file. I want to try to load the file into a new column of type varbinary(max), but don't know how to write a stored procedure to do such. Something like such

UPDATE MyTable
SET image = "file located in field imagePath"

I know that makes no sense because I don't have a where clause, but what would I put in it's place??

Was it helpful?

Solution

SQL is not well suited to accessing the file system, but this task would not be difficult to accomplish in a C#/VB.NET program.

EDIT: Tim Lehner's OPENROWSET solution is ideal if you are on SQL 2005 or later.

OTHER TIPS

You may need to create a stored procedure to do this. I've used the procs outlined in here to good effect:

Reading and Writing Files in SQL Server using T-SQL

My purposes only included text, but binary is mentioned.

Depending upon your version of SQL Server, you might try openrowset, or as @Jeremy Pridemore mentions, you may be able to use the CLR.

UPDATE

This code may help you if you're using SQL2005 or above:

declare @MyFile varbinary(max)
select @MyFile = bulkcolumn 
from openrowset (bulk 'C:\SomeFolder\SomeImage.jpg', single_blob) myfile
select @MyFile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top