关于动态报表

ysx192 2004-04-07 03:23:10
如下:
declare @str varchar(8000)
while @@fetch_status=0
begin
@str=@str+',sum(case when aaa then bbb when ccc then ddd else 0 end)
fetch next from cursor_test into
end

@str='select aaaa,bbbb,'+str +'from 表名'

当循环次数很多时,len(@str)>8000 ,

请问碰到这种情况有什么办法来做好?????
...全文
53 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-04-07
  • 打赏
  • 举报
回复
更多的参考我的贴子:

化解字符串不能超过8000的方法
http://expert.csdn.net/Expert/topic/2303/2303308.xml?temp=.8503076
zjcxc 元老 2004-04-07
  • 打赏
  • 举报
回复
/*--化解字符串不能超过8000的方法

经常有人提到,用动态生成SQL语句的方法处理数据时,处理语句超长,无法处理的问题
下面就讨论这个问题:
--邹建 2003.9--*/

/*-- 数据测试环境 --*/
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

/*-- 要求结果
日期 A单位 B单位 C单位 D单位 E单位 F单位 G单位 H单位 I单位 J单位
---------- ----- ----- ----- ----- ----- ----- ---- ---- ---- ------
2001-01-01 100 0 0 0 0 0 0 0 0 0
2001-01-02 0 101 0 0 0 0 0 0 0 0
2001-01-03 0 0 102 0 0 0 0 0 0 0
2001-01-04 0 0 0 103 0 0 0 0 0 0
2001-01-05 0 0 0 0 104 0 0 0 0 0
2001-01-06 0 0 0 0 0 105 0 0 0 0
2001-01-07 0 0 0 0 0 0 106 0 0 0
2001-01-08 0 0 0 0 0 0 0 107 0 0
2001-01-09 0 0 0 0 0 0 0 0 108 0
2001-01-11 0 0 0 0 0 0 0 0 0 109
--*/

/*-- 常规处理方法
declare @sql nvarchar(4000)
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)')
--*/

/*-- 问题

如果单位很多,这时,@SQL的值就会被截断,从而出错.

下面给出一种解决办法:
--*/

--/*-- 方法. 多个变量处理

--定义变量,估计需要多少个变量才能保存完所有数据
declare @sql0 nvarchar(4000),@sql1 nvarchar(4000)
--,...@sqln nvarchar(4000)

--生成数据处理临时表
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为每组的单位个数

--生成SQL语句处理字符串
--初始化
select @sql0=''
,@sql1=''
-- ...
-- ,@sqln

--得到处理字符串
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)
')

--删除临时表
drop table #temp

/*
优点:比较灵活,数据量大时只需要增加变量就行了.不用改动其他部分
缺点:要自行估计处理的数据,估计不足就会出错
*/
--*/

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧