日期:2014-05-19  浏览次数:20531 次

这样的功能在SQL语句中如何实现?
Allocation_Tab   表中
    有一个字段为   Number_Of_Container   (int   4),如果发现某条记录的该字段的值为n> 1时就将该条记录复制   n-1次,并将新由复制产生的n-1条记录中的   Number_Of_Container字段的内容设为0,   但保留最初记录中   Number_Of_Container
字段中的值不变。



------解决方案--------------------
create table Allocation_Tab(Number_Of_Container int)
insert Allocation_Tab select 3
union all select 1
union all select 4

declare @i int
select @i=max(Number_Of_Container) from Allocation_Tab

set rowcount @i
select ID=identity(int, 1, 1) into #T from syscolumns, sysobjects
set rowcount 0

select Number_Of_Container, Number_Of_Container from Allocation_Tab
union all
select Number_Of_Container, 0 from Allocation_Tab a, #T b
where a.Number_Of_Container> b.ID and a.Number_Of_Container> 1
order by 1, 2

--result
Number_Of_Container Number_Of_Container
------------------- -------------------
1 1
3 0
3 0
3 3
4 0
4 0
4 0
4 4

(8 row(s) affected)