2017-08-16 158 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

已更新,以添加訂單 –