2014-01-14 119 views
0

上執行下面提到聲明:導出SQL查詢結果到Excel

EXEC proc_generate_excel_with_columns 
    'your dbname', 'your table name','your file path' 

我得到以下error.Can誰能幫助?

User name not provided, either use -U to provide the user name or use -T for Trusted Connection usage: bcp {dbtable | query} {in | out | queryout | format} datafile [-m maxerrors] [-f formatfile] [-e errfile] [-F firstrow] [-L lastrow] [-b batchsize] [-n native type] [-c character type] [-w wide character type] [-N keep non-text native] [-V file format version] [-q quoted identifier] [-C code page specifier] [-t field terminator] [-r row terminator] [-i inputfile] [-o outfile] [-a packetsize] [-S server name] [-U username] [-P password] [-T trusted connection] [-v version] [-R regional enable] [-k keep null values] [-E keep identity values] [-h "load hints"] [-x generate xml format file] [-d database name] NULL

我的程序是這樣的:

create procedure proc_generate_excel_with_columns 
(
    @db_name varchar(100), 
    @table_name varchar(100), 
    @file_name varchar(100) 
) 
as 

--Generate column names as a recordset 
declare @columns varchar(8000), @sql varchar(8000), @data_file varchar(100) 
select 
    @columns=coalesce(@columns+',','')+column_name+' as '+column_name 

from 
    information_schema.columns 
where 
    table_name='dbo.vcuriosoftronic.tblPayrollGroups' 
select @columns=''''''+replace(replace(@columns,' as ',''''' as '),',',',''''') 

--Create a dummy file to have actual data 
select @data_file=substring('D:\TestFile.xls',1,len('D:\TestFile.xls') 
          -charindex('\',reverse('D:\TestFile.xls'))) 
          +'D:\TestFile.xls' 

--Generate column names in the passed EXCEL file 
set @sql='exec master..xp_cmdshell ''bcp 
      " select * from (select '[email protected]+') as t" 
      queryout "'[email protected]_name+'" -c''' 
exec(@sql) 

--Generate data in the dummy file 
set @sql='exec master..xp_cmdshell ''bcp 
     "select * from [myserver]..'[email protected]_name+'" 
     queryout "'[email protected]_file+'" -c''' 
exec(@sql) 

--Copy dummy file to passed EXCEL file 
set @sql= 'exec master..xp_cmdshell ''type '[email protected]_file+' >> "'[email protected]_name+'"''' 
exec(@sql) 

--Delete dummy file 
set @sql= 'exec master..xp_cmdshell ''del '[email protected]_file+'''' 
exec(@sql) 
+1

聽起來像是*你的'proc_generate_excel_with_columns'正在使用xp_cmdshell錯誤地執行BCP。如果不知道'proc_generate_excel_with_columns'是 –

+0

是否再次檢查我的問題,我不可能回答,現在我已經提到過程 – Madeeha

+0

您缺少-T或-U -P。再試一次...查看我的代碼,瞭解AdventureWorks的工作示例。 –

回答

1

退房如何使用BCP導出數據我的博客文章。

http://craftydba.com/?p=1690

以下代碼段使用-T,可信的連接。如果您正在代理中運行一項工作,它將在該安全帳戶下運行。

請傳遞標準安全憑證-U -P或確保該帳戶能夠運行該命令。

-- BCP - Export query, pipe delimited format, trusted security, character format 
DECLARE @bcp_cmd4 VARCHAR(1000); 
DECLARE @exe_path4 VARCHAR(200) = 
    ' cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ & '; 
SET @bcp_cmd4 = @exe_path4 + 
    ' BCP.EXE "SELECT FirstName, LastName FROM AdventureWorks2008R2.Sales.vSalesPerson" queryout ' + 
    ' "C:\TEST\PEOPLE.TXT" -T -c -q -t0x7c -r\n'; 
PRINT @bcp_cmd4; 
EXEC master..xp_cmdshell @bcp_cmd4; 
GO 

假定BCP路徑在搜索列表中進行更新。下面是一個屏幕截圖帶有路徑和查詢在消息窗口改變了SQL Server 2012的

enter image description here

看,它已經從打印語句的BCP命令。您可以將該命令放入批處理文件中,以便在DOS提示符下進行測試。這是您的調試練習。

+0

謝謝Crafty ..但我已經在我的程序中使用了-T,這是提到的問題。 – Madeeha

+0

它不在上面的代碼中......這就是爲什麼你會收到這條消息嗎?您有一個數據和列標題轉儲。然後連接,並刪除數據文件。但bcp命令缺少憑證類型... –

+0

在哪些語句中,我有喲使用-T命令? – Madeeha