Home » SQL & PL/SQL » SQL & PL/SQL » Setting SELECT Count(*) Value Into Output Variable (Oracle Developer 11g)
Setting SELECT Count(*) Value Into Output Variable [message #503060] |
Tue, 12 April 2011 11:05  |
 |
BennyBoy76
Messages: 4 Registered: March 2011 Location: UK
|
Junior Member |
|
|
I'm trying to return the number of records in my link table that contains the excursion_id I pass in by counting them. It doesn't seem to like the select count(*) into my output variable. Has anyone got any ideas?
Thanks.............Ben
create or replace
PROCEDURE BOOK_PASSENGER(
EXCURSION_ID IN excursion_booking.excursion_id%TYPE,
PASSENGER_ID IN excursion_booking.passenger_id%TYPE,
NUMBER_BOOKED OUT NUMBER)
AS
BEGIN
INSERT INTO EXCURSION_BOOKING VALUES(EXCURSION_ID, PASSENGER_ID, NULL);
SELECT COUNT(*) INTO NUMBER_BOOKED FROM EXCURSION_BOOKING WHERE EXCURSION_ID = EXCURSION_ID;
END BOOK_PASSENGER;
[Updated on: Tue, 12 April 2011 11:10] Report message to a moderator
|
|
|
|
|
Re: Setting SELECT Count(*) Value Into Output Variable [message #503074 is a reply to message #503060] |
Tue, 12 April 2011 11:52   |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
BennyBoy76 wrote on Tue, 12 April 2011 12:05
SELECT COUNT(*) INTO NUMBER_BOOKED FROM EXCURSION_BOOKING WHERE EXCURSION_ID = EXCURSION_ID;
This is just poor code here. Either this will return every row in the table as it is the same as saying where 1=1, or how do you think Oracle is supposed to know the difference between a variable and a table column?
|
|
|
|
|
Re: Setting SELECT Count(*) Value Into Output Variable [message #503209 is a reply to message #503104] |
Wed, 13 April 2011 09:51  |
 |
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
yes, this is an excellent example of how scoping rules work. Notice that the sql is not invalid. The code is presented with to objects of different types with the same name both of which are valid in the context being used (aka. a parameter name vs. a column name). How is oracle to decide which one to use. Thus scoping rules.
This is an all too common practice: to not name parameters with some kind of prefix or suffix or otherwise make them so that they do not match column names. The other side of the fence is to remember that if you are generally going to use a prefix or suffix to name parameters in your code, don't name columns using the same kind of strategy (hehe).
Good luck, Kevin
|
|
|
Goto Forum:
Current Time: Fri May 16 11:19:25 CDT 2025
|