sql transact create cursor with dynamic tables

Solutions on MaxInterview for sql transact create cursor with dynamic tables by the best coders in the world

showing results for - "sql transact create cursor with dynamic tables"
Paola
17 Apr 2020
1Declare @UserID varchar(100)
2declare @sqlstatement nvarchar(4000)
3--move declare cursor into sql to be executed
4set @sqlstatement = 'Declare  users_cursor CURSOR FOR SELECT userId FROM users'
5
6exec sp_executesql @sqlstatement
7
8
9OPEN users_cursor
10FETCH NEXT FROM users_cursor
11INTO @UserId
12
13WHILE @@FETCH_STATUS = 0
14BEGIN
15Print @UserID
16EXEC asp_DoSomethingStoredProc @UserId
17
18FETCH NEXT FROM users_cursor --have to fetch again within loop
19INTO @UserId
20
21END
22CLOSE users_cursor
23DEALLOCATE users_cursor