sql server loop over query

Solutions on MaxInterview for sql server loop over query by the best coders in the world

showing results for - "sql server loop over query"
Max
19 Sep 2019
1DECLARE @id INT
2DECLARE @name NVARCHAR(100)
3DECLARE @getid CURSOR
4
5SET @getid = CURSOR FOR
6SELECT table.id,
7       table.name
8FROM   table
9
10OPEN @getid
11FETCH NEXT
12FROM @getid INTO @id, @name
13WHILE @@FETCH_STATUS = 0
14BEGIN
15    EXEC stored_proc @varName=@id, @otherVarName='test', @varForName=@name
16    FETCH NEXT
17    FROM @getid INTO @id, @name
18END
19
20CLOSE @getid
21DEALLOCATE @getid
22