好的我有以下命令,它带有一些带有一些订单号的数据。问题是当数据集多于一个时,订单值保持不变而不是自动递增。
例如:
如果按照以下逻辑逐个获取表中 Order 值的数据,但如果一次取出多个数据,则订单保持相同,即自动递增对于所有以相同值获取的行。也许某些循环我获取数据并逐个更新可以帮助我。帮助我在我的存储过程中实现循环/数组条件。我的命令如下:
declare @MaxOrder1 int=0
select @MaxOrder1 = max([order]) from [dbo].[XYZ_ABC_Table1] where [Id]=@Id
if (@MaxOrder1 is null)
begin
set @MaxOrder1 =0
end
set @MaxOrder1 = @MaxOrder1+1
UPDATE STPC SET STPC.[IsIncluded]= PT.[Value] FROM [dbo].[XYZ_ABC_Table1] STPC join @Table2 PT on (PT.Id = STPC.Id)
UPDATE STPC SET STPC.[Order]= case
when STPC.[IsIncluded] = 1 then @MaxOrder1
else ''
END
FROM [dbo].[XYZ_ABC_Table1] STPC join @Table2 PT on (PT.Id = STPC.Id)
答案 0 :(得分:0)
尝试使用do while循环。
declare @MaxOrder1 int=0
select @MaxOrder1 = max([order]) from [dbo].[XYZ_ABC_Table1] where [Id]=@Id
if (@MaxOrder1 is null)
begin
set @MaxOrder1 =0
end
---while loop start
WHILE (@intFlag <=5) -- replace 5 with your limit
BEGIN
set @MaxOrder1 = @MaxOrder1+1
UPDATE STPC SET STPC.[IsIncluded]= PT.[Value] FROM [dbo].[XYZ_ABC_Table1] STPC join @Table2 PT on (PT.Id = STPC.Id)
UPDATE STPC SET STPC.[Order]= case
when STPC.[IsIncluded] = 1 then @MaxOrder1
else ''
END
FROM [dbo].[XYZ_ABC_Table1] STPC join @Table2 PT on (PT.Id = STPC.Id)
END -- while loop end here
答案 1 :(得分:0)
declare @MaxOrder1 int=0
declare @MinOrder int=0
select top 1 @MaxOrder1 = [order] from [dbo].[XYZ_ABC_Table1] where [Id]=@Id order by [order]
select top 1 @MinOrder = [order] from [dbo].[XYZ_ABC_Table1] where [Id]=@Id order by [order] desc
while(@MinOrder<>@MaxOrder1)
BEGIN
//rest of your code
set @MinOrder=@MinOrder+1
end
假设您的订单是序列1,2,3,4,5而不是1,3,5,7,8