/* пример (немножко не изящно, "в лоб") */
create table idq (I_D int, Quantity int, somefield varchar(100),
CONSTRAINT ID_PK PRIMARY KEY (I_D, Quantity))
insert idq values (1, 12, '') /* insert test data */
insert idq values (5, 24, '')
insert idq values (9, 31, '')
insert idq values (3, 18, '')
create table #qty (fld1 int)
declare @maxqty as int
select @maxqty = max(Quantity) from idq
while @maxqty > 0
begin
insert into #qty values(@maxqty)
SET @maxqty = @maxqty - 1
end
declare @id int
declare @qt int
DECLARE t_cursor CURSOR FAST_FORWARD FOR
select I_D, Quantity from idq order by Quantity desc
OPEN t_cursor
FETCH NEXT FROM t_cursor
INTO @id, @qt
SET NOCOUNT ON
SET QUOTED_IDENTIFIER OFF
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO idq
SELECT I_D, fld1 as Quantity, '' as somefield
FROM (SELECT @id AS I_D) a
cross join (SELECT fld1 from #qty WHERE fld1 < @qt) b
FETCH NEXT FROM t_cursor
INTO @id, @qt
END
SET NOCOUNT OFF
SET QUOTED_IDENTIFIER ON
drop table #qty
SELECT * FROM idq order by I_D, Quantity -- check results
CLOSE t_cursor
DEALLOCATE t_cursor
drop table idq
GO |