CREATE proc out2xls @服务器名 varchar(255), @库名 varchar(255), @表名 varchar(255), @用户名 varchar(100), @密码 varchar(100), @路径及文件名 varchar(255) as declare @temp1 nvarchar(4000),@temp2 varchar(8000)set @temp1='select @value1='''',@value2='''' select @value1=@value1+'',''''''+a.name+''''+char(39)+'' [''+a.name+'']'',@value2=@value2+'',cast(''+''[''+a.name+'']''+ '' as varchar(200))'' from '+@库名+'..syscolumns a,'+@库名+'..sysobjects d where a.id=d.id and d.name='''+@表名+''''+' order by a.colorder' exec sp_executesql @temp1,N'@value1 nvarchar(4000) output , @value2 varchar(8000) output',@temp1 output,@temp2 outputselect @temp1=right(@temp1,len(@temp1)-1),@temp2=right(@temp2,len(@temp2)-1)exec('select * into '+@库名+'.dbo.中间表 from (select '+ @temp1+' union all SELECT '+@temp2+' FROM '+@库名+'..'+@表名+') tem3') set @temp2='bcp '+@库名+'.dbo.中间表 out '+@路径及文件名+' -c -S'+@服务器名+' -U'+@用户名+' -P'+@密码EXEC master..xp_cmdshell @temp2 exec('drop table '+@库名+'.dbo.中间表') GO exec out2xls 'daliserver','pubs','jobs','sa','element','c:\a.txt' go drop proc out2xls
我给你发的短信你收到了吗
用BCP 引出 SQL表的 格式文本A(内容包括 字段名称)
用语句把‘字段名称’从A中取出,再放入取应EXCEL文件的第一行。
如果是在前台程序中处理,就灵活很多.
第二行,工作簿中的表格名。必须已经有此表。
第三行, 数据库连接参数。
第四行,任意的Select查询语句。
已经看到你的贴子,但是“http://upload.smiling.com/file/152580/sqlsvr2xls.rar”这个地址没法下载,连ping http://upload.smiling.com 或 ping upload.smiling.com都不行,你能邮给我吗? ([email protected] 或 [email protected])谢谢!
@服务器名 varchar(255),
@库名 varchar(255),
@表名 varchar(255),
@用户名 varchar(100),
@密码 varchar(100),
@路径及文件名 varchar(255)
as
declare @temp1 nvarchar(4000),@temp2 varchar(8000)set @temp1='select @value1='''',@value2='''' select @value1=@value1+'',''''''+a.name+''''+char(39)+'' [''+a.name+'']'',@value2=@value2+'',cast(''+''[''+a.name+'']''+ '' as varchar(200))'' from '+@库名+'..syscolumns a,'+@库名+'..sysobjects d where a.id=d.id and d.name='''+@表名+''''+' order by a.colorder'
exec sp_executesql @temp1,N'@value1 nvarchar(4000) output , @value2 varchar(8000) output',@temp1 output,@temp2 outputselect @temp1=right(@temp1,len(@temp1)-1),@temp2=right(@temp2,len(@temp2)-1)exec('select * into '+@库名+'.dbo.中间表 from (select '+ @temp1+' union all SELECT '+@temp2+' FROM '+@库名+'..'+@表名+') tem3')
set @temp2='bcp '+@库名+'.dbo.中间表 out '+@路径及文件名+' -c -S'+@服务器名+' -U'+@用户名+' -P'+@密码EXEC master..xp_cmdshell @temp2
exec('drop table '+@库名+'.dbo.中间表')
GO
exec out2xls 'daliserver','pubs','jobs','sa','element','c:\a.txt'
go
drop proc out2xls