If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > cursor repeat the last record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-11-08, 00:07
tiger66 tiger66 is offline
Registered User
 
Join Date: Dec 2007
Posts: 32
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
Reply With Quote
  #2 (permalink)  
Old 04-11-08, 02:59
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
You fetch then print irrespective of weather the fetch above the print resulted in status=2
Reply With Quote
  #3 (permalink)  
Old 04-11-08, 03:22
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
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!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On