2017-03-25 71 views
-1

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

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

我已经玩过单引号和双引号,但有不同的语法错误。在当前表单中,这会返回错误“复制方向必须是','出'或'格式'。”

我也有这种玩弄围绕在命令行中,我总是得到“找不到指定文件

存储过程的全文:。?

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

任何意见

+0

你会得到什么错误? –

+0

这就像因为sql server运行的主体没有c:\ Contractors \的读权限。解决这个问题的最好方法是使用代理用户 –

+0

我建议在数据库之外运行windows命令。用xp_cmdshell处理错误情况等并不那么简单。 –

回答

0

你需要把单引号加倍:

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