问题
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