简体   繁体   English

SQL Server 2005使用bcp写入文件

[英]SQL Server 2005 write to file with bcp

Attempting to write query results to file. 尝试将查询结果写入文件。 Using the following in SQL Server 2005: 在SQL Server 2005中使用以下命令:

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

I've played around with the single and double quotes, with differing syntax errors. 我玩过单引号和双引号,但语法有所不同。 In the current form, this returns the error "Copy direction must be either 'in', 'out' or 'format'." 在当前形式下,这将返回错误“复制方向必须为'in','out'或'format'。”

I've also toyed around with this on the command line and I always get "cannot find the file specified. 我也在命令行上玩弄了这个,我总是得到“找不到指定的文件。

Full text of stored procedure: 存储过程的全文:

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,"

Any advice? 有什么建议吗?

您需要将单引号加倍:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM