procedure using cursors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frankwu
    New Member
    • Jun 2007
    • 2

    procedure using cursors

    exam is coming.i still feel headache about procedure using cursor.
    here is example:
    Q.Create a cursor stored procedure called PriceChange to read the title id and price from the titles tables which will return the information as a cursor output. Use the PriceChange stored procedure to increase the price in the titles table by 10% using a cursor. If the price is null, set the price to $9.99. (pubs database)
    i try to do this way but fail,can somebody help me?thanks.

    CREATE PROCEDURE PriceChange
    (@titles_cursor CURSOR VARYING OUTPUT)
    AS
    SET @titles_cursor = CURSOR
    FORWARD_ONLY STATIC FOR
    SELECT title_id,
    price
    FROM titles

    OPEN @titles_cursor
    GO

    DECLARE @MyCursor CURSOR,
    @title_id_temp varchar(6),
    @price_temp money

    EXEC PriceChange @titles_cursor= @MyCursor OUTPUT

    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    FETCH NEXT FROM @MyCursor INTO @title_id_temp, @price_temp
    IF @price_temp IS NULL
    SET price=9.99
    ELSE
    SET price=@price_te mp*1.1
    END
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    I am not too big into the use of cursors in SQL, but do you intend to pass a cursor to the stored procedure? Can you do this?

    What error messages do you get when you try to use this?

    Comment

    Working...