exec xp_cmdshell bcp语法

时间:2017-08-16 19:07:49

标签: sql-server bcp xp-cmdshell

我似乎找不到使用exec xp_cmdshell bcp在sql server management studio中使用列名导出数据的正确语法。我尝试了以下变体

EXEC xp_cmdshell bcp "select "a_id","b_id","c_id" union select a_id,b_id,c_id from  tablename out 
"\\network_path\a.txt" -c -Uusername -Ppassword -Sservername"

并且

EXEC xp_cmdshell bcp 'select 'a_id','b_id','c_id' union select a_id,b_id,c_id from  tablename out 
'\\network_path\a.txt' -c -Uusername -Ppassword -Sservername'

EXEC xp_cmdshell bcp 'select "a_id","b_id","c_id" union select a_id,b_id,c_id from  tablename out 
"\\network_path\a.txt" -c -Uusername -Ppassword -Sservername'

EXEC xp_cmdshell bcp "select 'a_id','b_id','c_id' union select a_id,b_id,c_id from  tablename out 
'\\network_path\a.txt' -c -Uusername -Ppassword -Sservername"

我已成功使用以下命令导出表格,但我还需要列名。

bcp tablename out "\\network_path\a_test.txt" -c -Uusername -Ppassword -Sservername'

1 个答案:

答案 0 :(得分:1)

好的,你实际上需要订购标题行,使其显示在顶部。这应该处理它

EXEC xp_cmdshell 'bcp "select * from (select ''a_id'' as a_id,''b_id'' as b_id,''c_id'' as c_id union select a_id,b_id,c_id from tablename)q order by case a_id when ''a_id'' then 0 ELSE 1 END" queryout "\\networkpath\a.txt" -c -Uusername -Ppassword -Sservername -ddatabasename'