473,769 Members | 4,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a SP inside a cursor loop..

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...

declare EffectiveDate_C ursor cursor for
select field1,fld2 from tab1,tab2 where tab1.effectived ate<Getdate()
---/////Assuming the above query would result in 3 records
Open EffectiveDate_C ursor
Fetch next From EffectiveDate_C ursor Into @FLD1,@FLD2
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
While @@Fetch_Status = 0
Fetch next From EffectiveDate_C ursor 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.
Any Help would be appreciated.

Satish

Jun 29 '06 #1
2 3404
(sa************ **@gmail.com) 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_C ursor 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, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 29 '06 #2
>> I have SP, which has a cursor iterations. Need to call another SP for
every loop iteration of the cursor. <<

No. You need to learn to program in SQL. All you are doing is
mimicing a 1960's 3GL magnetic tape file system . In your pseudo
code, you even refer to fields instead of columns! You put the "sp_"
prefix on procedure names!

Don't you understand that SQL is a non-procedurdal language? You
should write only a few cursors in 20 years, not two in one
application.

Your whole approach to the problem is **fundamentally ** wrong.
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. <<
It is very hard to de-bug code that you will not show us. But when
pseudo code is this awful, I bet that the real code is a total mess.
More cursors? Dynamic SQL? Badly written procedural code with poor
coupling and cohesion?
Any Help would be appreciated. <<


You have no idea what you are doing. What you will get on Newsgroups
is a quick kludge to get rid of you, but not any real help. You need
to stop programming and get some education; then get some training.

Jun 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5147
by: robert | last post by:
Mr. Kyte's article doesn't use cursors, while Mr. Feuerstein's book examples do. my recollection of conventional wisdom is to avoid using cursors. is this difference merely a question of style, or is one approach right and the other wrong? oracle 8.1.7
4
2071
by: R. Z. | last post by:
I 've have a stored procedure that compares fields across databases. In order to do so it requires 2 values it acquires from 2 tables. The search is based on the ID of the data owner and a subject: proc_evaluate_results @StudentId = '222222', Course = 'PSY101' In order to obtain those values I run a cursor accross my records and SELECT THEM INTO 2 @variables, which then replace 222222 and PSY101 with dynamic values eg.
3
3355
by: r rk | last post by:
I am trying to write a utility/query to get a report from a table. Below is the some values in the table: table name: dba_daily_resource_usage_v1 conn|loginame|dbname|cum_cpu|cum_io|cum_mem|last_batch ------------------------------------------------------------ 80 |farmds_w|Farm_R|4311 |88 |5305 |11/15/2004 11:30 80 |abcdes_w|efgh_R|5000 |88 |4000 |11/15/2004 12:30 45 |dcp_webu|DCP |5967 |75 |669 |11/16/2004...
0
366
by: satishchandra999 | last post by:
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... declare EffectiveDate_Cursor cursor for select field1,fld2 from tab1,tab2 where tab1.effectivedate<Getdate()
0
11791
debasisdas
by: debasisdas | last post by:
SAMPLE CODE USING RECORD =========================== declare cursor c1 is select * from dept; type drec is record (a dept.deptno%type, b dept.dname%type, c dept.loc%type); type ttype is table of drec index by binary_integer; dr drec;
0
18041
debasisdas
by: debasisdas | last post by:
RESTRICTIONS ON CURSOR VARIABLES ================================= Currently, cursor variables are subject to the following restrictions: Cannot declare cursor variables in a package spec. CREATE PACKAGE emp_stuff AS TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE; emp_cv EmpCurTyp; -- not allowed END emp_stuff;
0
4676
debasisdas
by: debasisdas | last post by:
This thread contains some useful tips/samples regarding some advance concepts in cursors. FEW MORE EXAMPLES =================== declare er emp%rowtype; cursor c1 is select * from emp; begin open c1;
0
4862
debasisdas
by: debasisdas | last post by:
Sample example to show FOR UPDATE CURSOR ----------------------------------------------------------------------------- DECLARE CURSOR EMPREC IS SELECT * FROM EMP FOR UPDATE OF SAL; MYREC EMPREC%ROWTYPE; NUM NUMBER(4); BEGIN OPEN EMPREC; LOOP
0
8043
debasisdas
by: debasisdas | last post by:
Cursor Variable Returning %ROWTYPE ----------------------------------------------------------- DECLARE TYPE TmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE; tmp_cv TmpCurTyp;TYPE EmpCurTyp IS REF CURSOR RETURN tmp_cv%ROWTYPE; emp_cv EmpCurTyp; BEGIN NULL;
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.