Sql server exec xp_cmdshell bcp语法

Sql server exec xp_cmdshell bcp语法,sql-server,bcp,xp-cmdshell,Sql Server,Bcp,Xp Cmdshell,在sql server management studio中,我似乎找不到使用exec xp_cmdshell bcp导出具有列名的数据的正确语法 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" 及 及 及 我已经成功地使用

在sql server management studio中,我似乎找不到使用exec xp_cmdshell bcp导出具有列名的数据的正确语法

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'

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

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'

用法:bcp{dbtable | query}{in | out | queryout | format}数据文件[-m maxerrors][f formatfile][e errfile][f firstrow][L lastrow][b batchsize][n本机类型][c字符类型][w宽字符类型][n保留非文本本机版本][V文件格式版本][q带引号的标识符][c代码页说明符][t字段终止符][-r行终止符][i输入文件][o输出文件][a数据包大小][S服务器名称][U用户名][P密码][T可信连接][v版本][r区域启用][k保留空值][E保留标识值][h“加载提示”]
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'
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'