5

我想将查询结果发送到文件,所以尝试使用 bcp 命令。但不能向其传递任何参数。它显示错误。

EXEC xp_cmdshell 'bcp "SELECT * FROM CG.dbo.cdyy where EndTime between     DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,'+@date+'),0)) and DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,1,'+@date+')+1,0))  " queryout "D:\cdr_cg.txt" -T -c -t,'
4

1 回答 1

5

将您的参数放在呼叫 master..xp_cmdshell 之前

DECLARE @date varchar(10) = '20130311',
        @bcp varchar(8000)

SELECT @bcp = 'bcp "SELECT * FROM CG.dbo.cdyy WHERE EndTime between DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,''' + @date + '''),0)) AND DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,1,'''  + @date + ''')+1,0))  " queryout "D:\cdr_cg.txt" -T -c -t,'

EXEC master..xp_cmdshell @bcp
于 2013-03-11T11:00:24.967 回答