SQL Server 2008 R2
这不起作用:
IF @exists = 1
SELECT
@pic = pImage.BulkColumn
FROM
OPENROWSET(BULK @fn, SINGLE_BLOB) AS pImage
ELSE
SET @pic = NULL
除非我将@fn(NVARCHAR)替换为硬编码字符串,例如 N'C:\ path \ to \ some.jpg'。但是必须有一种方法可以在那里使用变量,对吗?
答案 0 :(得分:0)
我建议使用Dynamic sql,类似于这个答案:Using a Variable in OPENROWSET Query
在你的情况下它会是这样的:
DECLARE @sql VARCHAR(MAX)
IF @exists = 1
SET @sql = 'SELECT pImage.BulkColumn FROM OPENROWSET(BULK ''' + @fn + ''', SINGLE_BLOB) AS pImage'
sp_execute @sql
--Here you would need to add something to retreive @pic (I didn't add it since I don't know what @pic is. You can guide you with this link https://msdn.microsoft.com/en-us/library/ms188001.aspx
ELSE
SET @pic = NULL