473,769 Members | 6,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EXEC Inside a cursor iteration problem

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_r esults @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.

--define a cursor etc.etc.

WHILE @@cursor_fetch = 0
BEGIN
--do the cursor call INTO @vars
EXEC proc_evaluate_r esults @StudentId = @studentID, @Course =
@CourseCode
END

Now,the vars are being passed to the stored procedure and executed OK,
but the cursor gets stuck on the last record and continues to evaluate
it until stopped manually.

If I comment out the EXEC and replace it with eg. PRINT @Course + ' |
' + @CourseCode it runs fine, exiting after the last record.
Thanks
R>
Jul 20 '05 #1
6 5892
Hi and thank you for your reply.

The problem was logic actually and I should have caught that one
before posting. The PRINT statement was in the wrong place printing
the @count after it was incremented.

I will implement the SELECT instead of printing variables in my
debugging from now on though. It shaves off code required to maintain
the variables and is more direct eliminating problems like this one.

R>
Jul 20 '05 #2
Hi,

i don't know why it runs okay with PRINT but not with EXEC
but if your program is like your description then you check @@cursor_fetch
before the cursor call - maybe this is your problem?

i would change this to:

--define a cursor etc.etc.
--do the first cursor call INTO @vars
WHILE @@cursor_fetch = 0
BEGIN
EXEC proc_evaluate_r esults @StudentId = @studentID, @Course =
@CourseCode
--do the cursor call INTO @vars
END

bye,
helmut
Jul 20 '05 #3

"hwoess" <in****@iis-edv.at> wrote in message
news:40******** *************** @newsreader02.h ighway.telekom. at...
Hi,

i don't know why it runs okay with PRINT but not with EXEC
but if your program is like your description then you check @@cursor_fetch
before the cursor call - maybe this is your problem?

i would change this to:

--define a cursor etc.etc.
--do the first cursor call INTO @vars
WHILE @@cursor_fetch = 0

[...]

The preferred option is:

@@FETCH_STATUS , not @@CURSOR_FETCH

You also haven't indicated what is inside the WHILE loop (e.g. the structure
of your FETCH statements)

Steven
Jul 20 '05 #4
OK, thanks, putting the EXEC statement before the cursor call to
advance does work. Thank you,

New problem now however is that the first record does not get
processed while the last record is processed twice

TIA

Here's the code, PRINT statements and @count are for debug purposes:

DECLARE @CurEmployeeID CHAR(10), @CurCourseCode CHAR(10), @count AS
INT

SET @count = 1

DECLARE user_results_cu rsor CURSOR FOR
SELECT EmployeeID, CourseCode
FROM CourseUserData
ORDER BY EmployeeID

OPEN user_results_cu rsor
FETCH NEXT FROM user_results_cu rsor
INTO @CurEmployeeID, @CurCourseCode

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC proc_evaluate_r esults @EmployeeID = @CurEmployeeID, @CourseCode
= @CurCourseCode
FETCH NEXT FROM user_results_cu rsor
INTO @CurEmployeeID, @CurCourseCode
PRINT 'FETCH STATUS: ' + CAST(@@FETCH_ST ATUS AS VARCHAR) + ' | ' +
@CurEmployeeID + ' | ' + @CurCourseCode + ' | ' + CAST(@count AS
VARCHAR) + '* * * * * '
SET @count = @count + 1
END

CLOSE user_results_cu rsor
DEALLOCATE user_results_cu rsor
Jul 20 '05 #5
i allways try to keep testing routines as simple as possible
so i would suggest to try the following:

DECLARE @CurEmployeeID CHAR(10), @CurCourseCode CHAR(10)
DECLARE user_results_cu rsor CURSOR FOR
SELECT EmployeeID, CourseCode FROM CourseUserData ORDER BY EmployeeID

OPEN user_results_cu rsor
FETCH NEXT FROM user_results_cu rsor INTO @CurEmployeeID, @CurCourseCode
WHILE @@FETCH_STATUS = 0 BEGIN
select @CurEmployeeID, @CurCourseCode
FETCH NEXT FROM user_results_cu rsor INTO @CurEmployeeID, @CurCourseCode
END
CLOSE user_results_cu rsor
DEALLOCATE user_results_cu rsor

so what do you see? No values from the first record but the last twice?
Then i would check the content of table CourseUserData!
(any difference if you use varchar(10) instead of char(10) for your
variables?)

bye,
Helmut
Jul 20 '05 #6
Hi and thank you for your reply.

The problem was logic actually and I should have caught that one
before posting. The PRINT statement was in the wrong place printing
the @count after it was incremented.

I will implement the SELECT instead of printing variables in my
debugging from now on though. It shaves off code required to maintain
the variables and is more direct eliminating problems like this one.

R>
Jul 20 '05 #7

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

Similar topics

0
10212
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means "Specified partition does not exist". Does anybody know if it is a database problem or a problem of
4
11261
by: sci | last post by:
Could someone help me by answering the questions below? What's a cursor? What's difference between Query and View? Is a RecordSet just part of a table? Can it be part of a query of view? If the content in a table changed, is it necessary for a old recordset to renew itself by do "Requery()"? Thanks for your help!
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.
7
2230
by: Philip Mette | last post by:
Does anyone have any good references they could recommend on Cursor based SQL writing? I have to create SQL that can loop though records simular to VB loops and I have been told that this is the way to go. Any recommendations would be helpful.
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...
5
2436
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
7
6576
by: P. Adhia | last post by:
Sorry for quoting an old post and probably I am reading out of context so my concern is unfounded. But I would appreciate if I can get someone or Serge to confirm. Also unlike the question asked in the post below, my question involves non-partitioned table loads. I want to know if, in general, loading from cursor is slower than loading from a file? I was thinking cursor would normally be faster, because of DB2's superior buffer/prefetch...
8
18823
by: johnlichtenstein | last post by:
I am using cx_Oracle and MySQLdb to pull a lot of data from some tables and I find that the cursor.execute method uses a lot of memory that never gets garbage collected. Using fetchmany instead of fetchall does not seem to make any difference, since it's the execute that uses memory. Breaking the query down to build lots of small tables doesn't help, since execute doesn't give its memory back, after reading enough small tables execute...
2
3404
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()
26
3663
by: warth33 | last post by:
Hello I have a php site. Some page needs to call an external program. The programs are home made c# applications. It uses to work without problem. For a while. Maybe it work for some hour. Or for a day. Or even for a week. At a certain point, when a php script calls the exe file, the application freezes. The following happens:
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
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...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.