How to write a foreach in SQL Server?

后端 未结 10 2159
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 08:39

I am trying to achieve something along the lines of a for-each, where I would like to take the Ids of a returned select statement and use each of them.

DECLAR         


        
10条回答
  •  天涯浪人
    2020-12-12 09:13

    Suppose that the column PractitionerId is a unique, then you can use the following loop

    DECLARE @PractitionerId int = 0
    WHILE(1 = 1)
    BEGIN
      SELECT @PractitionerId = MIN(PractitionerId)
      FROM dbo.Practitioner WHERE PractitionerId > @PractitionerId
      IF @PractitionerId IS NULL BREAK
      SELECT @PractitionerId
    END
    

提交回复
热议问题