473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

--define a cursor etc.etc.

WHILE @@cursor_fetch = 0
BEGIN
--do the cursor call INTO @vars
EXEC proc_evaluate_results @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 5858
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_results @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.highwa y.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_cursor CURSOR FOR
SELECT EmployeeID, CourseCode
FROM CourseUserData
ORDER BY EmployeeID

OPEN user_results_cursor
FETCH NEXT FROM user_results_cursor
INTO @CurEmployeeID, @CurCourseCode

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

CLOSE user_results_cursor
DEALLOCATE user_results_cursor
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_cursor CURSOR FOR
SELECT EmployeeID, CourseCode FROM CourseUserData ORDER BY EmployeeID

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

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
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...
4
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...
4
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:...
7
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...
3
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...
5
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
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...
8
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...
2
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... ...
26
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.