SQL Server 2005 使用 bcp 写入文件

SQL Server 2005 write to file with bcp

正在尝试将查询结果写入文件。在 SQL Server 2005 中使用以下内容:

EXEC xp_cmdshell "bcp 'select License + '-' + ISNULL(Name,'') as Employer from People' queryout 'c:\People\text.txt' -c -x -T,"

我试过单引号和双引号,但有不同的语法错误。在当前形式中,此 returns 错误 "Copy direction must be either 'in', 'out' or 'format'."

我也在命令行上玩过这个,我总是得到“找不到指定的文件。

存储过程全文:

EXEC
master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC
master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE
EXEC xp_cmdshell "bcp 'select contrLice + '-' + ISNULL(contrName,'') as Employer from Contractors' queryout 'c:\Contractors\text.txt' -c -x -T,"

有什么建议吗?

您需要将单引号加倍:

exec xp_cmdshell "bcp 'select License + ''-'' + coalesce(Name, '''') as Employer from People' queryout 'c:\People\text.txt' -c -x -T,"