我写了这个光标
DECLARE IMGPATH CURSOR FAST_FORWARD FOR
select ImageFileName from tempImageFiles
OPEN IMGPATH
FETCH NEXT FROM IMGPATH INTO @ImageFileName
WHILE @@FETCH_STATUS = 0
BEGIN
--SELECT @ImageFileName = LEFT(@ImageFileName, (6-4))
SET @strSQLtoExecute = N'INSERT tempImage (imageData) SELECT BulkColumn FROM OPENROWSET(BULK ''C:\Temp\Images\' + @ImageFileName + ''', SINGLE_BLOB ) AS x'
PRINT @strSQLtoExecute
EXEC SP_executeSQL @strSQLtoExecute, @ImageFileName
FETCH NEXT FROM IMGPATH INTO @ImageFileName
END
CLOSE IMGPATH
DEALLOCATE IMGPATH
执行时我收到以下错误
INSERT tempImage (imageData)
SELECT BulkColumn
FROM OPENROWSET(BULK 'C:\Temp\Images\1.jpg', SINGLE_BLOB ) AS x
Msg 102,Level 15,State 1,Line 1
附近的语法不正确
'1。'。
INSERT tempImage (imageData)
SELECT BulkColumn
FROM OPENROWSET(BULK 'C:\Temp\Images\10.jpg', SINGLE_BLOB ) AS x
Msg 102,Level 15,State 1,Line 1
附近的语法不正确
'10。'。
INSERT tempImage (imageData)
SELECT BulkColumn
FROM OPENROWSET(BULK 'C:\Temp\Images\11.jpg', SINGLE_BLOB ) AS x
Msg 102,Level 15,State 1,Line 1
附近的语法不正确
'11。'。
但是,如果我单独执行每个语句,它就可以正常工作。
现在感到困惑。
答案 0 :(得分:0)
参数语句应如下所示:
EXEC SP_executeSQL @strSQLtoExecute, @ImageFileName=@ImageFileName
我认为这会解决您的问题。
或者,完全省略参数,并更改执行字符串:
SET @strSQLtoExecute = N'INSERT tempImage (imageData) SELECT BulkColumn FROM OPENROWSET(BULK ''C:\Temp\Images\'' + @ImageFileName + '''', SINGLE_BLOB ) AS x'