Re: using t-sql cursors ...

From: Tamas Bojcan - bojci (bojcan_at_freemail.hu)
Date: 08/19/04


Date: Thu, 19 Aug 2004 11:38:33 +0200

You are right, use WHILE loop... See this from the BOL:

USE pubs
GO

DECLARE @au_lname varchar(40), @au_fname varchar(20)
DECLARE authors_cursor CURSOR FOR SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B%" ORDER BY au_lname, au_fname

OPEN authors_cursor

FETCH NEXT FROM authors_cursor INTO @au_lname, @au_fname

WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT "Author: " + @au_fname + " " + @au_lname
   FETCH NEXT FROM authors_cursor INTO @au_lname, @au_fname
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

"VADOR" <anonymous@discussions.microsoft.com> wrote in message
news:919501c485cf$621f7610$a601280a@phx.gbl...
> Thanks for vary fast reaction. I know that i can't fetch
> multiple rows directly using 'fetch next' but how to
> resolve this problem using t-sql. Maybe using 'while' loop
> with 'fetch next into' and 'table' variable?
> >-----Original Message-----
> >Hi,
> >
> >You can't do this. Maybe you thought for multiple
> columns? It's possible and
> >you can find examples in the BOL.
> >
> >
> >"VADOR" <anonymous@discussions.microsoft.com> wrote in
> message
> >news:8b5501c485bf$d0569740$a501280a@phx.gbl...
> >> Hello,
> >> please show me an example (real t-sql code), how to
> >> retrieve multiple rows in each fetch from rowset using
> t-
> >> sql cursors.
> >> Regards
> >>
> >
> >
> >.
> >



Relevant Pages

  • Re: Bulk Collect without LIMIT
    ... Oracle, with 10g, merged the BULK COLLECT and FETCH to use the same mechanism. ... If you just say OPEN CURSOR ... ... for i in 1..500 loop ...
    (comp.databases.oracle.misc)
  • Re: Calling a SP inside a cursor loop..
    ... every loop iteration of the cursor. ... Fetch next From EffectiveDate_Cursor Into @FLD1,@FLD2 ... and If the Fetch stmt is below the begin Stmt, the loop iterations are ... the proper way to program a cursor loop is: ...
    (comp.databases.ms-sqlserver)
  • Re: Fetch out of sequence in cursor
    ... end loop; ... I have a cursor which loops through a table and does some processing. ... Towards the end of the loop I update another table and I want to COMMIT each ... I'm getting a fetch out of sequence message when i try to ...
    (comp.databases.oracle.misc)
  • Re: What is wrong with this????
    ... cursor c_triggers is ... fetch c_triggers into v_trgname; ... end loop; ... As you can see there is no rocket science in the script. ...
    (comp.databases.oracle.server)
  • Re: Self Joins and optimization
    ... What I'm interested in exploring is your assertion of the ... non-existence of a set oriented solution to the problem, ... while x.ArrivalTime is null and x%FOUND loop ...
    (comp.databases.theory)