0

我有一个 500 行的表,其中 orderid 列作为主键,其中的值是随机的,比如 10248,10250,10256,10258...... 等等,现在我有了复制前 50 行的代码2的循环,但orderid列将增加1,代码是

create procedure USP_CopyingDuplicateRows @loop int, @top int
as 
Begin
Declare  @i int = 1,@Maxid int = 0
while(@i <= @loop)
Begin
select @Maxid = max([OrderID]) from [T_MyOrders]
insert into [T_MyOrders]([OrderID],[CustomerID] ,[EmployeeID],[OrderDate],[RequiredDate] ,[ShippedDate] ,[ShipVia],[Freight] ,[ShipName] ,[ShipAddress],[ShipCity] ,[ShipRegion] ,[ShipPostalCode],[ShipCountry] )
select top (@top) @Maxid+(row_number() over( order by [OrderID])) as [OrderID],[CustomerID] ,[EmployeeID],[OrderDate],[RequiredDate] ,[ShippedDate] ,[ShipVia],[Freight] ,[ShipName] ,[ShipAddress],[ShipCity] ,[ShipRegion] ,[ShipPostalCode],[ShipCountry] 
 from T_myorders
set @i=@i+1
END
End

但是现在我希望输出说如果表以 10800 作为最后一个 orderid 结尾,如果我复制前 10 行,那么它应该给出 orderid 以及它们所在的 orderid 的间隙,比如前 10 个 orderid 是 10248,10250,10251, 10256,10258.... 现在输出应该显示为 10801,10803,10804,10808,10810...... 这样,orderid 之间的间隙 i 也应该添加并显示在输出中。任何人都可以在这方面提供帮助

4

0 回答 0