如何循环选择结果?

时间:2018-02-09 16:04:37

标签: sql-server loops

我有这个选择的结果

enter image description here

我试图使用此代码,但我没有int id。

DECLARE @LoopCounter INT = 1, @MaxLAB INT  , 
        lib NVARCHAR(100)
 set @MaxLAB=(select count(LAB_UM) from STARE_LAB_DEMANDE )
WHILE(@LoopCounter <= @MaxLAB)
BEGIN
   SELECT lib = LAB_UM
   FROM Mytable WHERE Id = @LoopCounter
 
   PRINT lib  
   SET @LoopCounter  = @LoopCounter  + 1        
END

我想循环播放这些记录。该ID为lab_um

1 个答案:

答案 0 :(得分:0)

您可以轻松地以LAB_UM顺序循环:

declare @lib varchar(50)
set @lib=''
while exists(select * from table where lab_um>@lib)
begin
    set @lib=(select min(lab_um) from table where lab_um>@lib)
    /*do stuff here */
end