經常有人提到,用動態生成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 @[email protected]+',['+單位名稱
+']=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)
--,[email protected] 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 @[email protected]+值 from #temp where groupid=0 --第一個變量
select @[email protected]+值 from #temp where groupid=1 --第二個變量
--select @[email protected]+值 from #temp where groupid=n --第n個變量
--查詢
exec('select 日期=convert(varchar(10),日期,120)'
[email protected][email protected]
-- [email protected]
+' 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 '[email protected]+'(值 varchar(8000))
insert into '[email protected]+' values(''create view '
+stuff(@tbname,2,2,'')+' as
select 日期=convert(varchar(10),日期,120)'')'
exec(@sql)
set @sql='insert into '[email protected]+'
select '',[''+單位名稱+'']=sum(case 單位名稱 when ''''''
+單位名稱+'''''' then 銷售額 else 0 end)''
from(select distinct 單位名稱 from tb) a'
exec(@sql)
set @sql='insert into '[email protected]+'
values(''from tb group by convert(varchar(10),日期,120)'')'
exec(@sql)
--生成創建視圖的文件,注意使用了文件:c:/temp.txt
set @sql='bcp "'[email protected]+'" out "c:/temp.txt" /s"'
[email protected]+'" /u"'[email protected]+'" /p"'[email protected]+'" /c'
exec master..xp_cmdshell @sql
--刪除臨時表
set @sql='drop table '[email protected]
exec(@sql)
--調用isql生成數據處理視圖
set @tbname=stuff(@tbname,2,2,'')
set @sql='isql /s"'[email protected]
+case @username when '' then '" /e' else '" /u"'[email protected]+'" /p"'[email protected]+'"' end
+' /d"'+db_name()+'" /i"c:/temp.txt"'
exec master..xp_cmdshell @sql
--調用視圖,顯示處理結果
set @sql='select * from '[email protected]+'
drop view '[email protected]
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),@[email protected]
,@sql1='@'[email protected]+' varchar(8000),'[email protected]
,@[email protected]+'@'[email protected]+'='''','
,@sql3='select @'[email protected]+'[email protected]'[email protected]+'+a from # where gid='[email protected]
+char(13)[email protected]
,@[email protected]+',@'[email protected]
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( @[email protected][email protected]+'
exec('[email protected]+'+'[email protected]+'+'[email protected]+')'
)
--刪除臨時表
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=''