Cursor for dynamically executed SQL strings

  • You can dynamically execute a sql string in a stored procedure:

    SET @SQL = 'SELECT * FROM table'

    EXEC (@SQL)

    Is it possible to declare a cursor for a dynmaically executed SQL string?

    eg:

    SET @SQL = 'SELECT field FROM table'

    DECLARE cursorTest CURSOR FOR

          @SQL

    or something like that because the above doesn't work 😉

     

  • no but you could do

    @sql = 'select * from table'

    @cursor = 'DECLARE cursorTest CURSOR FOR '

    exec(@cursor + @sql)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply