我无法理解我的错误在哪里。
我正在尝试遍历表并将以image
数据类型存储的PDF文件传送到特定位置。
我写了光标,但它在'+'
旁边给了我一个语法错误CREATE TABLE #tblPolicyForms
(
FormID INT NOT NULL,
PDF VARBINARY(MAX) NULL,
PDF_FIlename VARCHAR(MAX)
)
INSERT INTO #tblPolicyForms
SELECT
FormID, PDF, PDF_FileName
FROM
[dbo].[tblPolicyForms]
WHERE
FormID IN (19, 20, 21, 22)
--select * from #tblPolicyForms
DECLARE @FormID varchar(4);
DECLARE @FileName VARCHAR(200);
DECLARE FormID_cursor CURSOR FOR
SELECT
FormID, PDF_FIlename
FROM
#tblPolicyForms
WHERE
FormID IN (19, 20, 21, 22)
--where whatever conditions you need to get the FormIDs you want to use
OPEN FormID_cursor
FETCH NEXT FROM FormID_cursor INTO @FormID, @FileName
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC master.dbo.xp_cmdshell 'BCP "SELECT PDF FROM [MEJAMES].[dbo].tblPolicyForms " queryout "C:\Users\oserdyuk\Documents\ExportDir\'+@FileName+'" -T -N'
FETCH NEXT FROM FormID_cursor INTO @FormID, @FileName
END
CLOSE FormID_cursor
DEALLOCATE FormID_cursor
答案 0 :(得分:1)
使用变量:
DECLARE @cmd NVARCHAR(MAX) = N'BCP "SELECT PDF FROM [MEJAMES].[dbo].tblPolicyForms " queryout "C:\Users\oserdyuk\Documents\ExportDir\'+@FileName+'" -T -N';
EXEC master.dbo.xp_cmdshell @cmd;
来自文档EXEC:
{值
| @variable [OUTPUT]
| [默认]
}
所以你只能传递值(文字),变量或默认值。在您使用+
的示例中,您试图传递导致错误的表达式。
注意:最好以分号结束每个语句。