Is it possible to execute a stored procedure over a set without using a cursor?

前端 未结 7 923
遥遥无期
遥遥无期 2020-12-15 18:55

I would like to execute a stored procedure over each row in a set without using a cursor with something like this:

SELECT EXEC dbo.Sproc @Param1 = Table1.i

7条回答
  •  春和景丽
    2020-12-15 19:13

    9 out of 10 times you can do what you want without a cursor or a while loop. However, if you must use one, I have found that while loops tend to be faster.

    Also if you do not want to delete or update the table, you can use something like this:

    DECLARE @id [type]
    SELECT @id = MIN([id]) FROM [table]
    WHILE @id IS NOT NULL
    BEGIN
        EXEC [sproc] @id
        SELECT @id = MIN([id]) FROM [table] WHERE [id] > @id
    END
    

提交回复
热议问题