if exists(select name from sysobjects where name='out2xls' and type='p')
drop procedure out2xls
go
CREATE proc out2xls
@服务器名 varchar(255),
if existes(select name from sysobjects where name='fang' and type='p')
drop@库名 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 'dzd2034','test','ERPcharge','sa','','c:\a.txt'
go
drop proc out2xls
drop procedure out2xls
go
CREATE proc out2xls
@服务器名 varchar(255),
if existes(select name from sysobjects where name='fang' and type='p')
drop@库名 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 'dzd2034','test','ERPcharge','sa','','c:\a.txt'
go
drop proc out2xls
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout E:\boc\DT.xls -c -Sservername -Usa -Ppassword'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout E:\boc\DT.xls -c -Sservername -Usa -Ppassword'
然后将查询结果全部选中,另存为.csv格式就可以了
程序中
用dw_1.saveasciias()zh