Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Posts
    58

    Question Unanswered: Why need to open CURSOR in the procedure?

    Hi,everyone.

    Why need to open CURSOR in the part in which a procedure is created?

    The following codes are listed in the Microsoft SQL Server 2005 BOOKS ONLINE.

    (1)

    USE AdventureWorks;
    GO
    IF OBJECT_ID ( 'dbo.currency_cursor', 'P' ) IS NOT NULL
    DROP PROCEDURE dbo.currency_cursor;
    GO
    CREATE PROCEDURE dbo.currency_cursor
    @currency_cursor CURSOR VARYING OUTPUT
    AS
    SET @currency_cursor = CURSOR
    FORWARD_ONLY STATIC FOR
    SELECT CurrencyCode, Name
    FROM Sales.Currency;
    OPEN @currency_cursor;

    GO


    (2)

    USE AdventureWorks;
    GO
    DECLARE @MyCursor CURSOR;
    EXEC dbo.currency_cursor @currency_cursor = @MyCursor OUTPUT;
    WHILE (@@FETCH_STATUS = 0)
    BEGIN;
    FETCH NEXT FROM @MyCursor;
    END;
    CLOSE @MyCursor;
    DEALLOCATE @MyCursor;
    GO

    Please help me. Thank you in advance!

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    what's the problem? that is just the way it goes. you need to declare, open, fetch from, close and then deallocate your cursor. that is just how it works.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Aug 2006
    Posts
    58
    Thank you,Thrasymachus.

    I have created the following procedure:

    USE AdventureWorks;
    GO
    IF OBJECT_ID ( 'dbo.currency_cursor', 'P' ) IS NOT NULL
    DROP PROCEDURE dbo.currency_cursor;
    GO
    CREATE PROCEDURE dbo.currency_cursor
    @currency_cursor CURSOR VARYING OUTPUT
    AS
    SET @currency_cursor = CURSOR
    FORWARD_ONLY STATIC FOR
    SELECT CurrencyCode, Name
    FROM Sales.Currency;
    GO


    (2)

    USE AdventureWorks;
    GO
    DECLARE @MyCursor CURSOR;
    EXEC dbo.currency_cursor @currency_cursor = @MyCursor OUTPUT;
    OPEN @MyCursor;
    WHILE (@@FETCH_STATUS = 0)
    BEGIN;
    FETCH NEXT FROM @MyCursor;
    END;
    CLOSE @MyCursor;
    DEALLOCATE @MyCursor;
    GO

    But DBMS give error message. The difference between the two procedure:
    (a) In the first one , open cursor statement is included in creation part of the procedure;
    (b) In the second one, open cursor statement is included in the execution part.

    Why? Please give me some advice.Thank you very much!

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    Cursors are not returned in SQL Server as they are in Oracle. What are you trying to do?

  5. #5
    Join Date
    Aug 2006
    Posts
    58
    Thank you, Mcrowley.

    The first example is ok, This example is from Microsoft SQL Server 2005 BOOKS ONLINE. It shows that cursor can be returned.

    But, in the second example, DBMS give me error. Why?

    Thanks!

  6. #6
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    So, what is the error you get?

Posting Permissions

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