Cursor not declared Error

  • Hi

     

    I am getting this error in SQL 2005 that Cursor is not declared?

    Please help!!

  • I would love to help, but it turns out that the Psyche class I took wasn't about mind reading after all.

    In all seriousness, all I can tell you from what you posted is that you did not declare a cursor before trying to use it.

    If you would be so kind as to tell us what you did to get this error and post the code you were using, if any, we may be able to actually help you. Also, when posting about an error, tell us the exact error message. Slight differences in the error can make a huge difference in its meaning.


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

  • Most often this error crops up when you are referencing a variable and you do not prefix it with the "@".  Cursors are "variables" whose naming convention does NOT call for the "@"

  • Yep, you haven't declared it, or haven't declared it properly. If you're just using a straight cursor you need to do something like:

    DECLARE myCursor CURSOR

    FOR SELECT ... {your query goes here}

    OPEN myCursor

    and then you can use it.

    If you want to use a cursor variable, that's slightly different. You need to first declare the variable and then assign a cursor to it. Something like

    DECLARE @myVariable CURSOR

    DECLARE myCursor CURSOR

    FOR SELECT ...

    SET @myVariable = myCursor

    or

    DECLARE @myVariable CURSOR

    SET @myVariable CURSOR

    FOR SELECT ...

    See BOL for more detailed information:

    ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/5a3a27aa-03e8-4c98-a27e-809282379b21.htm

    ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/a5f0904e-0171-44fa-b516-14c6dc91ccd0.htm

    Cheers,

    Bart

Viewing 4 posts - 1 through 3 (of 3 total)

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