Bulk insert images into SQL Server database

对着背影说爱祢 提交于 2019-12-11 07:06:35

问题


I want to insert images in this way:

DECLARE @lpath varchar(100)
SET @lpath = 'd:\Photo\5604.jpg'

--insert into Photos(id, Photo, Path)
SELECT 
    4144, *, @lpath
FROM 
    OpenRowSet(BULK @lpath, Single_blob) AS i

but it's not working

If I execute the code like this:

SELECT 
    1, *, @lpath
FROM 
    OpenRowSet(BULK N'd:\Photo\5604.jpg', Single_blob) AS i

it works well.

How to execute script like in the first way?


回答1:


You cannot use variables in OpenRowSet, try to use dynamic SQL like this:

DECLARE @lpath NVARCHAR(100) 
SET @lpath = 'd:\Photo\5604.jpg'
DECLARE @sql NVARCHAR(MAX)

SET @sql='SELECT 4144, *, ''' + @lpath + '''
          FROM OpenRowSet(BULK ''' + @lpath + ''', Single_blob) AS i'

EXEC(@sql)


来源:https://stackoverflow.com/questions/45377118/bulk-insert-images-into-sql-server-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!