有些地方我已经写了注释,你对应改一下吧,应该不难....睡觉了,不行的话引用回复,我明天再看,可以的话记得结贴...第一步,开启需要的配置项:sp_configure 'show advance',1 go reconfigure go sp_configure 'xp_cmdshell',1 go reconfigure go第二步:把你要导出的数据集导出到CSV,CSV和excel是可以互转的: use xxx goif OBJECT_ID('temtable','u')is not null drop table temtableselect * into temtable from (select 'applicationID'as [applicationID],'Category' as [Category] --这一步是为了生成列名,根据你的数据的列名明确指定 union all select CONVERT(varchar(20),[applicationID]), [Category] from #temtable)a--这部分就是要导出的数据 declare @filename nvarchar(30) --下面是生成文件名 select @filename='Report'+right(replace(convert(date,GETDATE()),'-',''),2)+substring(replace(convert(date,GETDATE()),'-',''),5,2)+substring(replace(convert(date,GETDATE()),'-',''),1,4)+'.csv' ------------------------------------------ --配置BCP命令 declare @strSQL nvarchar(1024) set @strSQL='bcp "SELECT applicationID,Category FROM 某某库.dbo.temtable" queryout C:\Temp\DBA_date\'+@filename+' -c -T -t","' print @strSQL EXEC master..xp_cmdshell @strSQL
因为查询到的结果如果单纯复制粘贴的话是不包含标题行的,所以复制到excel以后必须手动添加标题行和更改sheet的名称,有点麻烦,这样说的话看来就不能省事了?呵呵
那查询一次导出一次,然后我再把多个excel手动合并,这样的代码如何写?请指教,谢谢
我手动合并,现在只需要如何将一个查询的结果带标题行导出到excel中 谢谢
go
reconfigure
go
sp_configure 'xp_cmdshell',1
go
reconfigure
go第二步:把你要导出的数据集导出到CSV,CSV和excel是可以互转的:
use xxx
goif OBJECT_ID('temtable','u')is not null
drop table temtableselect * into temtable from (select 'applicationID'as [applicationID],'Category' as [Category] --这一步是为了生成列名,根据你的数据的列名明确指定
union all
select CONVERT(varchar(20),[applicationID]), [Category] from #temtable)a--这部分就是要导出的数据
declare @filename nvarchar(30)
--下面是生成文件名
select @filename='Report'+right(replace(convert(date,GETDATE()),'-',''),2)+substring(replace(convert(date,GETDATE()),'-',''),5,2)+substring(replace(convert(date,GETDATE()),'-',''),1,4)+'.csv'
------------------------------------------
--配置BCP命令
declare @strSQL nvarchar(1024)
set @strSQL='bcp "SELECT applicationID,Category FROM 某某库.dbo.temtable" queryout C:\Temp\DBA_date\'+@filename+' -c -T -t","'
print @strSQL
EXEC master..xp_cmdshell @strSQL
朋友,我在reconfigure 时 报这样的错消息 5808,级别 16,状态 1,第 1 行
不支持对系统目录进行即席更新。
消息 15123,级别 16,状态 1,过程 sp_configure,第 51 行
配置选项 'xp_cmdshell' 不存在,也可能是高级选项。
go
reconfigure
go
sp_configure 'xp_cmdshell',1
go
reconfigure
go
我收藏Huang版主写的东东了,谢谢
版主
我还是报那两个错 我在reconfigure 时 报这样的错消息 5808,级别 16,状态 1,第 1 行
不支持对系统目录进行即席更新。
在 sp_configure 'xp_cmdshell',1
时报 消息 15123,级别 16,状态 1,过程 sp_configure,第 51 行
配置选项 'xp_cmdshell' 不存在,也可能是高级选项。
是否Sql2005不支持这个命令?