473,395 Members | 1,468 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,395 software developers and data experts.

Cursor, Fetch, and while...

Hi guys,

I am having a comprehension problem for which I cannot seem to find an
answer (by banging my head on my keyboard.)

I have the following code which yields no results. I expect one result to
come out of it as I am using a while loop on a cursor set to retrieve one
single tuple.

Please be nice, it is my first day seeing cursors ;-)

Tia,

jeremy
CREATE TABLE people(
id int,
fname VARCHAR2(20),
lname VARCHAR2(20)
);

insert into people values (0, 'lois', 'lane');
insert into people values (1, 'clark', 'kent');
insert into people values (2, 'jimmy', 'olsen');

set serveroutput on;
CREATE OR REPLACE PROCEDURE show(curr_id INT)
AS
CURSOR curr_in IS SELECT fname, lname FROM people WHERE id=curr_id;
curr_fname VARCHAR2(20) := null;
curr_lname VARCHAR2(20) := null;

BEGIN
OPEN curr_in;

while curr_in%found loop
FETCH curr_in INTO curr_fname, curr_lname;
dbms_output.put_line('done');
dbms_output.put_line(curr_fname || ' ' || curr_lname);
end loop;

CLOSE curr_in;

EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlcode || ' ' || sqlerrm);
END;

/
Jul 19 '05 #1
2 22684
your WHILE loop never executes

-- mcs

"Jeremy Moncho" <je***********@bluewin.ch> wrote in message
news:BBF542FD.14A4%je***********@bluewin.ch...
| Hi guys,
|
| I am having a comprehension problem for which I cannot seem to find an
| answer (by banging my head on my keyboard.)
|
| I have the following code which yields no results. I expect one result to
| come out of it as I am using a while loop on a cursor set to retrieve one
| single tuple.
|
| Please be nice, it is my first day seeing cursors ;-)
|
| Tia,
|
| jeremy
|
|
| CREATE TABLE people(
| id int,
| fname VARCHAR2(20),
| lname VARCHAR2(20)
| );
|
| insert into people values (0, 'lois', 'lane');
| insert into people values (1, 'clark', 'kent');
| insert into people values (2, 'jimmy', 'olsen');
|
| set serveroutput on;
|
|
| CREATE OR REPLACE PROCEDURE show(curr_id INT)
| AS
| CURSOR curr_in IS SELECT fname, lname FROM people WHERE id=curr_id;
| curr_fname VARCHAR2(20) := null;
| curr_lname VARCHAR2(20) := null;
|
| BEGIN
| OPEN curr_in;
|
| while curr_in%found loop
| FETCH curr_in INTO curr_fname, curr_lname;
| dbms_output.put_line('done');
| dbms_output.put_line(curr_fname || ' ' || curr_lname);
| end loop;
|
| CLOSE curr_in;
|
| EXCEPTION
| WHEN OTHERS THEN
| dbms_output.put_line(sqlcode || ' ' || sqlerrm);
| END;
|
| /
|
|
Jul 19 '05 #2
Jeremy Moncho <je***********@bluewin.ch> wrote in message news:<BBF542FD.14A4%je***********@bluewin.ch>...
Hi guys,

I am having a comprehension problem for which I cannot seem to find an
answer (by banging my head on my keyboard.)

I have the following code which yields no results. I expect one result to
come out of it as I am using a while loop on a cursor set to retrieve one
single tuple.

Please be nice, it is my first day seeing cursors ;-)

Tia,

jeremy
CREATE TABLE people(
id int,
fname VARCHAR2(20),
lname VARCHAR2(20)
);

insert into people values (0, 'lois', 'lane');
insert into people values (1, 'clark', 'kent');
insert into people values (2, 'jimmy', 'olsen');

set serveroutput on;
CREATE OR REPLACE PROCEDURE show(curr_id INT)
AS
CURSOR curr_in IS SELECT fname, lname FROM people WHERE id=curr_id;
curr_fname VARCHAR2(20) := null;
curr_lname VARCHAR2(20) := null;

BEGIN
OPEN curr_in;

while curr_in%found loop
FETCH curr_in INTO curr_fname, curr_lname;
dbms_output.put_line('done');
dbms_output.put_line(curr_fname || ' ' || curr_lname);
end loop;

CLOSE curr_in;

EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlcode || ' ' || sqlerrm);
END;

/


You should not test curr_in%found until you have tried to FETCH a row.
Since you have not yet fetched a row, curr_in%found will be NULL not
TRUE and so the WHILE loop never executes.

You can avoid making these kind of bugs, and drastically simplify your
code, by avoiding the use of OPEN, FETCH, %FOUND and CLOSE altogether
like this:

CREATE OR REPLACE PROCEDURE show(curr_id INT)
AS
CURSOR curr_in IS SELECT fname, lname FROM people WHERE
id=curr_id;
BEGIN
for rec in curr_in loop
dbms_output.put_line('done');
dbms_output.put_line(rec.fname || ' ' || rec.lname);
end loop;
END;

I also removed the EXCEPTION section because it added nothing useful,
in fact if used in code where you were updating the database it would
constitute a bug!
Jul 19 '05 #3

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

Similar topics

3
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,...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
1
by: teddysnips | last post by:
SQL Server 2000 I have a stored procedure that uses an extended SPROC to send an email notification to my customers when a document is distributed. However, the SPROC has an unexpected side...
1
by: traceable1 | last post by:
SQL Server 2000 SP4 with AWE hotfix. Windows 2003 SP1. I have a stored procedure which is not working the way I think it should be. I have a CURSOR which has a variable in the WHERE clause: ...
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... ...
2
by: Chris Zopers | last post by:
Hello, I've created a stored procedure that loops through a cursor, with the following example code: DECLARE curPeriod CURSOR LOCAL for SELECT * FROM tblPeriods DECLARE @intYear smallint...
0
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...
0
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...
2
by: Jeremy Moncho | last post by:
Hi guys, I am having a comprehension problem for which I cannot seem to find an answer (by banging my head on my keyboard.) I have the following code which yields no results. I expect one...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...
0
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...
0
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...

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.