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

来源:互联网 发布:Js动态添加button 编辑:程序博客网 时间:2024/04/29 07:32
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

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

/*--数据测试环境--*/
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[tb]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[tb]
GO

createtabletb(单位名称varchar(10),日期datetime,销售额int)
insertintotb
 select'A单位','2001-01-01',100
 unionallselect'B单位','2001-01-02',101
 unionallselect'C单位','2001-01-03',102
 unionallselect'D单位','2001-01-04',103
 unionallselect'E单位','2001-01-05',104
 unionallselect'F单位','2001-01-06',105
 unionallselect'G单位','2001-01-07',106
 unionallselect'H单位','2001-01-08',107
 unionallselect'I单位','2001-01-09',108
 unionallselect'J单位','2001-01-11',109

/*--要求结果
日期      A单位 B单位C单位D单位E单位 F单位G单位H单位I单位J单位  
-------------------------------------------- ---- ----------
2001-01-01100  0    0    0    0    0    0    0    0    0
2001-01-020    101  0    0    0    0    0    0    0    0
2001-01-030    0    102  0    0    0    0    0    0    0
2001-01-040    0    0    103  0    0    0    0    0    0
2001-01-050    0    0    0    104  0    0    0    0    0
2001-01-060    0    0    0    0    105  0    0    0    0
2001-01-070    0    0    0    0    0    106  0    0    0
2001-01-080    0    0    0    0    0    0    107  0    0
2001-01-090    0    0    0    0    0    0    0    108  01<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>