Re: Calling a SP inside a cursor loop..
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Thu, 29 Jun 2006 21:55:14 +0000 (UTC)
(satishchandra999@xxxxxxxxx) writes:
I have SP, which has a cursor iterations. Need to call another SP for
every loop iteration of the cursor. The pseudo code is as follows..
Create proc1 as
Begin
Variable declrations...
...
While @@Fetch_Status = 0
Fetch next From EffectiveDate_Cursor Into @FLD1,@FLD2
/* Assume If loop count is 3.
and If the Fetch stmt is below the begin Stmt, the loop iterations are
4 else the loop iterations are 2*/
begin
/*Calling my second stored proc with fld1 as a In parameter and Op1
and OP2 Out parameters*/
Exec sp_minCheck @fld1, @OP1 output,@OP2 output
Do something based on Op1 and Op2.
end
The problem I had been facing is that, the when a stored proc is called
within the loop, the proc is getting into infinite loops.
May I guess: the inner process also uses cursors?
Anyway, the proper way to program a cursor loop is:
DECLARE cur INENSITIVE CURSOR FOR
SELECT ...
-- Error handling goes here
OPEN cur
WHILE 1 = 1
BEGIN
FETCH cur INTO @x, @y, ....
IF @@fetch_status <> 0
BREAK
-- Do stuff
END
DEALLOCATE cur
By using only one FETCH statements you avoid funny errors, when you change
the cursor and forgets to change the cursor at the end of the loop. And by
checl @@fetch_status directly after the FETCH, you know that @@fetch_status
relates to that FETCH.
.... and in case no one ever told you before: avoid iterations as much as
you can, and try to always work set-based. Yes, I can understand that you
want to reuse code, and if the oomplexity is high enough it may be
warranted if the number of rows in the cursor is moderate. But the cost
in performance for iterative solutions can be *enourmous*. A database
engine is simply not designed for this type of processing.
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.
- References:
- Calling a SP inside a cursor loop..
- From: satishchandra999
- Calling a SP inside a cursor loop..
- Prev by Date: Re: Update statement, then insert what wasn't available to be updated.
- Next by Date: SQL Server 2005 Express
- Previous by thread: Calling a SP inside a cursor loop..
- Next by thread: Re: Calling a SP inside a cursor loop..
- Index(es):
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 SP from a Cursor Loop
... which has a cursor iterations. ... >Fetch next From EffectiveDate_Cursor
Into @FLD1,@FLD2 ... > and If the Fetch stmt is below the begin Stmt, the loop iterations
are ... SQL Server newsgroup, ... (microsoft.public.windows.server.sbs) - Re: Calling SP from a Cursor Loop
... every loop iteration of the cursor. ... Fetch next From EffectiveDate_Cursor
Into @FLD1,@FLD2 ... Exec sp_minCheck @fld1, @OP1 output,@OP2 output ... and If
the Fetch stmt is below the begin Stmt, the loop iterations are ... (microsoft.public.windows.server.sbs) - 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)