上執行下面提到聲明:導出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)
聽起來像是*你的'proc_generate_excel_with_columns'正在使用xp_cmdshell錯誤地執行BCP。如果不知道'proc_generate_excel_with_columns'是 –
是否再次檢查我的問題,我不可能回答,現在我已經提到過程 – Madeeha
您缺少-T或-U -P。再試一次...查看我的代碼,瞭解AdventureWorks的工作示例。 –