我想将数据从表传输到文本文件。我试图使用bcp实用程序和xp_cmdshell。但导出不成功。
如果我执行下面的查询,它的工作正常。
EXEC xp_cmdshell 'bcp "SELECT CONTRACT_NUMBER,BODY,SYNOPSIS,2,0 FROM AMPWEBAPP_TEST_DB..TM_DONOT_PUBLISH_TEXT_GRAPHIC_COMMENTS WHERE SYNOPSIS IS NOT NULL AND BODY IS NOT NULL" queryout "C:\bcptest.txt" -c -T -S'
但我想在条件中追加一个值。以下是我的查询,它不起作用。
DECLARE @ACC VARCHAR(MAX)
SET @ACC='12121'
EXEC xp_cmdshell 'bcp "SELECT CONTRACT_NUMBER,BODY,SYNOPSIS,2,0 FROM AMPWEBAPP_TEST_DB..TM_DONOT_PUBLISH_TEXT_GRAPHIC_COMMENTS WHERE SYNOPSIS IS NOT NULL AND BODY IS NOT NULL AND ACCOUNT_NUMBER='+@ACC+'" queryout "C:\bcptest.txt" -c -T -S'
答案 0 :(得分:0)
我猜ACCOUNT_NUMBER是varchar
字段,在这种情况下,您应该使用配额包含12121
,因为它是varchar
常量,所以:
EXEC xp_cmdshell 'bcp "SELECT CONTRACT_NUMBER,BODY,SYNOPSIS,2,0 FROM AMPWEBAPP_TEST_DB..TM_DONOT_PUBLISH_TEXT_GRAPHIC_COMMENTS WHERE SYNOPSIS IS NOT NULL AND BODY IS NOT NULL AND ACCOUNT_NUMBER='''+@ACC+'''" queryout "C:\bcptest.txt" -c -T -S'