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

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

/*-- 问题: 如果单位很多,这时,@SQL的值就会被截断,从而出错.*/

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

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

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

--生成数据处理临时表
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

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

--/*--方法2. bcp+isql

--因为要用到bcp+isql,所以需要这些信息
declare @servername varchar(250),@username varchar(250),@pwd varchar(250)
select @servername='zj'  --服务器名
 ,@username=''    --用户名
 ,@pwd=''     --密码

declare @tbname varchar(50),@sql varchar(8000)

--创建数据处理临时表
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)

/*
优点:程序自动处理,不存在判断错误的问题
缺点:复杂,经过的步骤多,容易出错,而且需要一定的操作员权限
*/
--*/

--/*-- 方法3. 多个变量处理,综合了方法1及方法2的优点, 解决了方法1中需要人为判断的问题,自动根据要处理的数据量进行变量定义,同时又避免了方法2的繁琐

declare @sqlhead varchar(8000),@sqlend varchar(8000)
 ,@sql1 varchar(8000),@sql2 varchar(8000),@sql3 varchar(8000),@sql4 varchar(8000)
 ,@i int,@ic varchar(20)

--生成数据处理临时表
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

select @sql1='declare '+left(@sql1,len(@sql1)-1)+char(13)
 ,@sql2=left(@sql2,len(@sql2)-1)+char(13)
 ,@sql3=left(@sql3,len(@sql3)-1)
 ,@sql4=substring(@sql4,2,8000)

--执行
exec( @sql1+@sql2+@sql3+'
exec('+@sqlhead+'+'+@sql4+'+'+@sqlend+')'
)

--删除临时表
drop table #
--*/

方法3中,关键要做修改的是下面两句,其他基本上不用做改变:

--生成数据处理临时表,修改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=''

转载于:https://www.cnblogs.com/wayne-ivan/archive/2007/06/29/800176.html

化解字符串不能超过8000的方法及交叉表的处理相关推荐

  1. 解字符串不能超过8000的方法及交叉表的处理

    经常有人提到,用动态生成SQL语句的方法处理数据时,处理语句超长,无法处理的问题 下面就讨论这个问题: /*-- 数据测试环境 --*/ if exists (select * from dbo.sy ...

  2. 动态SQL字符长度超过8000

    动态SQL字符长度超过8000,我记得SQL SERVER 2008中用SP_EXECUTESQL打破了这个限制. 平常用动态SQL,可能都会用EXEC(),但是有限制,就是8000字符串长度.自从S ...

  3. php判断字符串里有英文,PHP针对中英文混合字符串长度判断及截取方法示例

    本文实例讲述了PHP针对中英文混合字符串长度判断及截取方法.分享给大家供大家参考,具体如下: /** * * 中英混合字符串长度判断 * @param unknown_type $str * @par ...

  4. 判断字符串不超过20个字符_如何阻止超过140个字符的推文(如果确实需要)

    判断字符串不超过20个字符 After over a decade of staunchly restricting users to 140 characters in each message, ...

  5. 字符串的常用内置方法

    字符串的常用内置方法 capitalize() 将字符串的第一个字符转换为大写. lower() 转换字符串中所有大写字符为小写. upper() 转换字符串中的小写字母为大写. swapcase() ...

  6. sqlserver sp_executesql 动态SQL字符长度超过8000

    动态SQL字符长度超过8000,我记得SQL SERVER 2005中用SP_EXECUTESQL打破了这个限制. 平常用动态SQL,可能都会用EXEC(),但是有限制,就是8000字符串长度.自从S ...

  7. java怎么定义字符长度_java – 当字符串长度超过列长度定义时,如何以静默方式截断字符串?...

    我有一个Web应用程序,使用EclipseLink和MySQL存储数据. 其中一些数据是字符串,即DB中的varchars. 在实体代码中,字符串具有如下属性: @Column(name = &quo ...

  8. python的四种内置数字类型_浅析Python数字类型和字符串类型的内置方法

    一.数字类型内置方法 1.1 整型的内置方法 作用 描述年龄.号码.id号 定义方式 x = 10 x = int('10') x = int(10.1) x = int('10.1') # 报错 内 ...

  9. java字符串转json取集合_Java中Json字符串直接转换为对象的方法(包括多层List集合)...

    使用到的类:net.sf.json.JSONObject 使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:commons-b ...

  10. java集合转字符串拼接_关于集合和字符串的互转实现方法

    今天在写项目的时候遇到一个问题,就是要把得到的一个集合转换成字符串,发现 import org.apache.commons.lang.stringutils; 有这么一个简单的方法:string s ...

最新文章

  1. C语言 使用指针对两个变量的数值进行互换
  2. 数字图像处理:第十三章 图象复原
  3. Cocos2d-x内置粒子系统
  4. python3 连接数据库
  5. boost::log相关用法的测试程序
  6. Linux下的第一个驱动程序
  7. python邮件发送csv附件_Python2.7 smtplib发送带附件邮件报错STARTTLS解决方法
  8. FZU 1894 志愿者选拔
  9. 上传文件到ftp服务器
  10. vue项目实践教程3:中间大的五选项底部切换卡制作及相关问题解决
  11. CSDN创作的markdown语法
  12. Xshell 登录 AWS CentOS 出现“所选择的用户秘钥未在远程主机上注册“,最终解决办法!...
  13. OCT-模拟电路设计八边形法则的探讨
  14. s8 android 8.0变化,等待很长时间!三星S8系列手机现在可以升级到Android 8.0系统的稳定版本!...
  15. iVX和其它低代码平台没啥好比的 (一)
  16. 全国计算机等级考试监考培训,全国计算机等级考试 (NCRE) 监考培训
  17. 【C++】%c,%s分别代表什么意思
  18. 【肥海豹】-网络安全等级保护(等保)-WINDOWS类服务器配置
  19. 【PMP考试最新解读】第七版《PMBOK》应该如何备考?(含最新资料)
  20. android配置阿里云仓库

热门文章

  1. Cannot delete or update a parent row: a foreign key constraint fails
  2. java多线程知识点之wait和sleep的区别
  3. 【C面试】一道简单的C语言面试题的思考——打印星阵
  4. 使用cardview和recycleview时碰到的一些问题
  5. vue项目history路由的配置
  6. sqlite数据库主键自增_你绝对不可错过的数据库入门全套内容
  7. 能够支持python开发的环境_Windows上使用virtualenv搭建Python+Flask开发环境
  8. python怎么用matplotlib_python-无法在我的程序中使用matplotlib函数
  9. python json转换为dict的编码问题_python中json和字符编码的转换
  10. php 数组 utf8,PHP数组编码gbk与utf8互相转换的两种方法实例分享