database-cursor

Cursor For Loop with dynamic SQL-Statement

怎甘沉沦 提交于 2019-11-26 21:24:59
问题 Is there a way to perform a Cursor For Loop with an dynamic SQL-statement? If I don't want to declare a record I can do something like this (only if I declared the cursor..): For I in cuSelect Loop dbms_output.put_line(I.NAME); End Loop; And I can open a cursor for a dynamic SQL-statement: Open cuSelect For 'Select * From TAB_X'; Fetch ceSelect Into recSelect; Close cuSelect; But to do that I have to first declare the Record. Now my problem is that I have to open the Cursor for a very big and

Using a cursor with dynamic SQL in a stored procedure

点点圈 提交于 2019-11-26 10:26:59
问题 I have a dynamic SQL statement I\'ve created in a stored procedure. I need to iterate over the results using a cursor. I\'m having a hard time figuring out the right syntax. Here\'s what I\'m doing. SELECT @SQLStatement = \'SELECT userId FROM users\' DECLARE @UserId DECLARE users_cursor CURSOR FOR EXECUTE @SQLStatment --Fails here. Doesn\'t like this OPEN users_cursor FETCH NEXT FROM users_cursor INTO @UserId WHILE @@FETCH_STATUS = 0 BEGIN EXEC asp_DoSomethingStoredProc @UserId END CLOSE