日期:2014-05-18 浏览次数:20732 次
if object_id('tempdb.dbo.#') is not null drop table #
create table #
(
i int identity(0,1),
id as right('0'+ltrim(i/90000+1),2)+right('0'+ltrim(i/3000%30+1),2)+right('0'+ltrim(i/200%15+1),2)+right('0'+ltrim(i/20%10+1),2)+right('0'+ltrim(i%20+1),2),
data varchar(1)
)
insert # select 'a'
insert # select 'b'
select * from #
/*
i id data
----------- -------------------- ----
0 0101010101 a
1 0101010102 b
*/
------解决方案--------------------
declare
@i int -- loop
declare @n table (n int)
set @i=1
while @i<100 begin
insert into @n values (@i)
set @i=@i+1
end
select
right(cast(100+t as varchar),2) -- 楼
+ right(cast(100+r as varchar),2) -- 房间
+ right(cast(100+b as varchar),2) -- 架子
+ right(cast(100+x as varchar),2) -- 行
+ right(cast(100+y as varchar),2) -- 列
from
(select t=n from @n where n <= 20) as t, -- 20栋楼
(select r=n from @n where n <= 30) as r, -- 30个房间
(select b=n from @n where n <= 15) as b, -- 15个架子
(select x=n from @n where n <= 10) as x, -- 10行
(select y=n from @n where n <= 20) as y -- 20列