Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2007
    Posts
    32

    Unanswered: cursor repeat the last record

    Hi
    I try to use cursor to print the data in a table.
    My problem is, the last record always get printed twice.
    Is this a known sybase problem? How can I fix this?

    For example.
    testTable

    Name
    ---------
    test1
    test2

    Code:
    create procedure test
    as
    begin
       declare @name char(5)
       declare crsr cursor for select Name from testTable
    
       open crsr
       while (@@sqlstatus != 2)
       begin
          fetch crsr into @name
          print 'name = %1!', @name
       end
    end
    go
    The result is
    name = test1
    name = test2
    name = test2

    How can I fix it, so it prints
    name = test1
    name = test2


    Thanks in advance for any help in this

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    You fetch then print irrespective of weather the fetch above the print resulted in status=2

  3. #3
    Join Date
    Jan 2004
    Posts
    545
    Provided Answers: 4
    I usually use the following structure for cursors:
    Code:
    declare cursorname cursor for /selectstatement/
    
    open cursorname
    fetch cursorname into @variables
    
    while (@@sqlstatus = 0)
    begin
     /process current row/
     fetch cursorname into @variables
    end
    
    close cursorname
    deallocate cursor cursorname
    I'm not crazy, I'm an aeroplane!

Posting Permissions

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