exec xp_cmdshell bcp 语法

exec xp_cmdshell bcp syntax

我似乎找不到正确的语法来使用 exec xp_cmdshell bcp 在 sql 服务器管理 studio.I 中使用列名导出数据studio.I已经尝试了以下变体

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"

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

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

好的,您实际上需要对 header 行进行排序,使其显示在顶部。这应该可以解决

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'