/*-- 数据测试环境 --*/
if exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tb]
GO
create table tb(单位名称 varchar(10),日期 datetime,销售额 int)
insert into tb
select 'A单位','2001-01-01',100
union all select 'B单位','2001-01-02',101
union all select 'C单位','2001-01-03',102
union all select 'D单位','2001-01-04',103
union all select 'E单位','2001-01-05',104
union all select 'F单位','2001-01-06',105
union all select 'G单位','2001-01-07',106
union all select 'H单位','2001-01-08',107
union all select 'I单位','2001-01-09',108
union all select 'J单位','2001-01-11',109
/*-- 常规处理方法*/
declare @sql varchar(8000)
set @sql='select 日期=convert(varchar(10),日期,120)'
select @sql=@sql+',['+单位名称
+']=sum(case 单位名称 when '''+单位名称+''' then 销售额 else 0 end)'
from(select distinct 单位名称 from tb) a
exec(@sql+' from tb group by convert(varchar(10),日期,120)')
--生成数据处理临时表
select id=identity(int,0,1),groupid=0
,值=',['+单位名称 +']=sum(case 单位名称 when '''
+单位名称+''' then 销售额 else 0 end)'
into #temp from(select distinct 单位名称 from tb) a
--分组临时表,判断慨最多多少个单位可以组合成一个不超过8000的字符串,这里取假设为5个
update #temp set groupid=id/5 --5为每组的单位个数
--得到处理字符串
select @sql0=@sql0+值 from #temp where groupid=0 --第一个变量
select @sql1=@sql1+值 from #temp where groupid=1 --第二个变量
--select @sqln=@sqln+值 from #temp where groupid=n --第n个变量
--查询
exec('select 日期=convert(varchar(10),日期,120)'
+@sql0+@sql1
-- ...+@sqln
+' from tb group by convert(varchar(10),日期,120)
')
--创建数据处理临时表
set @tbname='[##temp_'+convert(varchar(40),newid())+']'
set @sql='create table '+@tbname+'(值 varchar(8000))
insert into '+@tbname+' values(''create view '
+stuff(@tbname,2,2,'')+' as
select 日期=convert(varchar(10),日期,120)'')'
exec(@sql)
set @sql='insert into '+@tbname+'
select '',[''+单位名称+'']=sum(case 单位名称 when ''''''
+单位名称+'''''' then 销售额 else 0 end)''
from(select distinct 单位名称 from tb) a'
exec(@sql)
set @sql='insert into '+@tbname+'
values(''from tb group by convert(varchar(10),日期,120)'')'
exec(@sql)
--生成创建视图的文件,注意使用了文件:c:\temp.txt
set @sql='bcp "'+@tbname+'" out "c:\temp.txt" /S"'
+@servername+'" /U"'+@username+'" /P"'+@pwd+'" /c'
exec master..xp_cmdshell @sql
--删除临时表
set @sql='drop table '+@tbname
exec(@sql)
--调用isql生成数据处理视图
set @tbname=stuff(@tbname,2,2,'')
set @sql='isql /S"'+@servername
+case @username when '' then '" /E' else '" /U"'+@username+'" /P"'+@pwd+'"' end
+' /d"'+db_name()+'" /i"c:\temp.txt"'
exec master..xp_cmdshell @sql
--调用视图,显示处理结果
set @sql='select * from '+@tbname+'
drop view '+@tbname
exec(@sql)
--生成数据处理临时表
select id=identity(int,0,1),gid=0
,a=',['+单位名称 +']=sum(case 单位名称 when '''
+单位名称+''' then 销售额 else 0 end)'
into # from(select distinct 单位名称 from tb) a
--判断需要多少个变量来处理
select @i=max(len(a)) from #
print @i
set @i=7800/@i
--分组临时表
update # set gid=id/@i
select @i=max(gid) from #
--生成数据处理语句
select @sqlhead='''select 日期=convert(varchar(10),日期,120)'''
,@sqlend=''' from tb group by convert(varchar(10),日期,120)'''
,@sql1='',@sql2='select ',@sql3='',@sql4=''
while @i>=0
select @ic=cast(@i as varchar),@i=@i-1
,@sql1='@'+@ic+' varchar(8000),'+@sql1
,@sql2=@sql2+'@'+@ic+'='''','
,@sql3='select @'+@ic+'=@'+@ic+'+a from # where gid='+@ic
+char(13)+@sql3
,@sql4=@sql4+',@'+@ic
--生成数据处理临时表,修改a=后面的内容为相应的处理语句
select id=identity(int,0,1),gid=0
,a=',['+code+']=sum(case b.c_code when '''
+code+''' then b.value else 0 end)'
into # from #Class
--生成数据处理语句,将@sqlhead,@sqlend赋值为相应的处理语句头和尾
select @sqlhead='''select a.id,a.name,a.code'''
,@sqlend=''' from #Depart a,#Value b where a.Code=b.d_Code group by a.id,a.code,a.name'''
,@sql1='',@sql2='select ',@sql3='',@sql4=''