客戶須要將表中統計好的數據還原成統計前的原始記錄測試
例如:spa
ID Name QTYCount
100 Name1 1
101 Name2 2
102 Name3 3
103 Name4 4
104 Name5 5code
還原爲:blog
ID Name QTY
100 Name1 1
101 Name2 1
102 Name2 1
103 Name3 1
104 Name3 1
105 Name3 1
106 Name4 1
107 Name4 1
108 Name4 1
109 Name4 1
110 Name5 1
111 Name5 1
112 Name5 1
113 Name5 1
114 Name5 1qt
原始數據已經找不到,只能經過現有數據反推class
--統計結果表 CREATE TABLE [dbo].[CopyTest]( [ID] [int] IDENTITY(100,1) NOT NULL, [Name] [nvarchar](50) NULL, [QTY] [float] NULL ) ON [PRIMARY] --將統計結果還原爲原始記錄表 CREATE TABLE [dbo].[CopyTestResult]( [ID] [int] IDENTITY(100,1) NOT NULL, [Name] [nvarchar](50) NULL, [QTY] [float] NULL ) ON [PRIMARY] --生成測試數據 Declare @n bigint Declare @Sql nvarchar(225) set @n=1 while @n<6 begin Set @Sql='Insert into CopyTest Values(''Name'+ convert(varchar(5),@n) +''','+convert(varchar(5),@n)+')' Exec (@Sql) set @n=@n+1 End
因爲數據量比較大,並且只是臨時的需求,想一想遊標最合適。循環
Declare CopyRowByNumber Cursor For Select * from CopyTest Open CopyRowByNumber declare @id int declare @name nvarchar(50) declare @qty float declare @allCount int set @allCount=0 Fetch Next From CopyRowByNumber into @id,@name,@qty while @@FETCH_STATUS=0 Begin --根據Qty循環生成數據 while @qty>=1 begin set @allCount=@allCount+1 insert into CopyTestResult Values(@name,1) set @qty=@qty-1 end Fetch Next From CopyRowByNumber into @id,@name,@qty End Close CopyRowByNumber DEALLOCATE CopyRowByNumber
ok。float