2017-08-16 71 views
0

我從視圖中選擇數據數據導出到Excel中使用BCP和commandShell

我正在錯誤

usage: bcp [[db_name.]owner.]table_name[:slice_num] [partition pname] {in | out} [filename] 
[-m maxerrors] [-f formatfile] [-e errfile] [-d discardfileprefix] 
[-F firstrow] [-L lastrow] [-b batchsize] 
[-n] [-c] [-t field_terminator] [-r row_terminator] 
[-U username] [-P password] [-I interfaces_file] [-S server] 
[-a display_charset] [-z language] [-v] 
[-i input_file] [-o output_file] 
[-A packet size] [-J client character set] 
[-T text or image size] [-E] [-g id_start_value] [-N] [-W] [-X] 
[-M LabelName LabelValue] [-labeled] 
[-K keytab_file] [-R remote_server_principal] [-C] 
[-V [security_options]] [-Z security_mechanism] [-Q] [-Y] 
[-y sybase directory] [-x trusted.txt_file] 
[--clienterr errfile] [--maxconn maximum_connections] 
[--show-fi] [--hide-vcc] 
[--colpasswd [[[db_name.[owner].]table_name.]column_name [password]]] 
[--keypasswd [[db_name.[owner].]key_name [password]]] 
[--initstring ASE initialization string] [--quoted-fname] 

NULL

declare @sql varchar(8000) 

Select @sql= 'bcp "Select * from eBenefitSync_GUI_PH.dbo.Disp_View" out D:\Contacts.txt -c -T ' +'-s ' [email protected]@servername+'\'[email protected]@servicename 
--EXECUTE master.dbo.xp_cmdshell @SQL 
+0

請打印'@ sql'併發布 –

+0

您是否嘗試過使用''-s'+ @@ SERVERNAME'? –

+0

是的,它沒有使用它,但使用IP,不知道爲什麼(超時錯誤),我也使用了servername \ instancename以及 – Ashu

回答

0

用法告訴你,它需要一個數據庫.table語法,而不是SQL查詢。嘗試從取出SELECT *:

declare @sql varchar(8000) 

Select @sql= 'bcp "eBenefitSync_GUI_PH.dbo.Disp_View" out D:\Contacts.txt -c -T ' +'-s ' [email protected]@servername+'\'[email protected]@servicename 
--EXECUTE master.dbo.xp_cmdshell @SQL 
+0

我有一個複雜的查詢,我在多個表上使用連接。你可以建議我怎麼辦? – Ashu

+0

這不是你使用你的觀點嗎? (我假設Disp_view是一個視圖) – Simon