473,758 Members | 4,381 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 5889
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
10211
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
2229
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
3354
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
18821
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
3402
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
3657
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
9489
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
10072
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
9906
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
9885
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
9737
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...
1
7286
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
6562
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();...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
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.