我有如下代码想实现对i的最后两个数值进行判断,从而得到相应的值来限制J的取值,不知该如何实现,能否用CASE进行判断,还望大家赐教
declare @i int
declare @j int
declare @k int
declare @sql nvarchar(4000)
set @i=201101
set @j=1
while @i<=201103
begin
******
******
******//能否赐教如何在次通过使用CASE来根据@i的末两位给@k赋值,
判断的依据为:若末两位为01、02、06、11则@k为26;若末两位为03、04、10则@k为27;其余为@k为28
while @j<=@k
begin
select @sql=isnull(@sql+' union all','')+' select * from '+str(@i,6)+'.dbo.'+str(@i*100+@j,8)
set @j=@j+1
end
set @i=@i+1
end
select @sql
exec(@sql)
望到家给予帮助,谢谢。
------解决方案--------------------
可以.
------解决方案--------------------
- SQL code
select @k = case right(@i,2) when 1,2,6,11 then 26 when 3,4,10 then 27 else 28 end
------解决方案--------------------
我觉得用if else会更好点
------解决方案--------------------
- SQL code
select 100+number as col1, case when right(100+number,2) in (1,2,6,11) then 26 when right(100+number,2) in (3,4,10) then 27 else 28 end as col2from master..spt_values where type='p' and number <15/*col1 col2----------- -----------100 28101 26102 26103 27104 27105 28106 26107 28108 28109 28110 27111 26112 28113 28114 28*/