2017-08-16 156 views
1

我似乎无法找到合适的格式,导出使用EXEC的xp_cmdshell在SQL Server Management BCP列名数据studio.I've尝试以下变化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" 

而且

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

好吧,您实际上需要订购标题行,使其显示在最上面。这应该处理它

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' 
+0

用法:bcp {dbtable |查询} {in | out |查询|格式} datafile [-m maxerrors] [-f formatfile] [-e errfile] [-F firstrow] [-L lastrow] [-b batchsize] [-n本机类型] [-c字符类型] [-w宽字符类型] [-N保留非文本本机] [-V文件格式版本] [-q带引号的标识符] [-C代码页说明符] [-t字段终止符] [-r行终止符] [-i inputfile] [ - o-outfile] [-a packetsize] [-S服务器名称] [-U用户名] [-P密码] [-T可信连接] [-v版本] [-R区域启用] [-k保留空值] [ - E保持身份值] [-h“加载提示”] –

+0

已更新,以添加订单 –