Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Posts
    1

    Unanswered: Cursor in SQL Server

    Hi,
    I've discovered the stored procedures lately, and moving almost all my requests to use this.

    I've a question about declaring a cursor.

    For example :
    DECLARE @strSQL varchar(255)
    SELECT @strSQL = 'SELECT au_lname FROM authors'

    I want to declare the cursor like this
    DECLARE csrAuthor
    FOR @strSQL
    READ ONLY

    I'm always getting an error. Is there a solution to make a "dynamic" cursor.

    Thanks
    Frank

  2. #2
    Join Date
    Feb 2003
    Location
    Brisbane, Australia
    Posts
    110

    Re: Cursor in SQL Server

    Try putting your DECLARE CURSOR statement inside an execute statement. Like this:

    EXEC ('DECLARE Oprid_Cursor CURSOR FOR ' + @VAR1)

    Then you can OPEN the cursor etc.

    Hope this helps.

  3. #3
    Join Date
    Jan 2003
    Location
    London, England
    Posts
    106
    It might be possible to do something like this also but I haven't tried it:

    DECLARE csrAuthor
    FOR exec sp_executesql @strSQL
    READ ONLY
    Frettmaestro
    "Real programmers don't document, if it was hard to write it should be hard to understand!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •