OracleEE 11g WITH clause causing error procedure won't compile - sql

This is my first run through with PL SQL so I might be making some sort of silly error.
I am trying to write a procedure in Oracle Express Edition 11g. I am running into an error that has to do with my WITH clause in the procedure body.
Whenever I try and run it I see two errors.
Error report:
ORA-06550: line 14, column 50:
PL/SQL: ORA-00918: column ambiguously defined
ORA-06550: line 12, column 7:
PL/SQL: SQL Statement ignored
ORA-06550: line 29, column 3:
PLS-00306: wrong number or types of arguments in call to 'FIND_HIGH_AVG'
ORA-06550: line 29, column 3:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Code for the procedure is found below.
DECLARE
myTerm courses.term%type;
myLine courses.lineno%type;
procedure find_high_avg (term IN courses.term%type,
line IN courses.lineno%type,
s_fname OUT students.fname%type,
s_lname OUT students.lname%type,
s_sid OUT students.side%type,
s_avg OUT number) is
begin
WITH grades as (select * from components co --line 12 here
join scores sc on co.term = sc.term and co.lineno = sc.lineno and CO.COMPNAME = SC.COMPNAME
where sc.lineno = line and sc.term = term) --line 14 here
select *
into s_fname, s_lname, s_sid, s_avg
from (
select s.fname, s.lname, s.sid, round(sum(points/maxpoints * weight),0) as AV
from grades, students
join students s on grades.sid = s.sid
group by s.sid, s.fname, s.lname
order by AV)
where rownum = 1;
end;
BEGIN
myTerm:='F12';
myLine:='1031';
find_high_avg(myTerm, myLine); --line 29 here
END;

The error at line 10 occurs because parameters to stored procedures do not take a length. The procedure declaration should be something like
procedure find_high_avg (term IN courses.term%type,
line IN courses.lineno%type,
s_fname OUT students.fname%type,
s_lname OUT students.lname%type,
s_sid OUT students.side%type,
average OUT number)
is
The error at line 14 is likely because the parameters to your procedure have the same name as columns in your table. In a SQL statement's scope resolution rules, column names take precedence over local PL/SQL variables. So when you code something like
sc.term = term
Oracle tries to resolve the unqualified TERM using a column in one of the tables. If both tables have a column named TERM, that generates an ambiguous column reference-- Oracle doesn't know which of the two tables to use. Of course, in reality, you don't want it to use the column from either table, you want it to use the parameter. The most common approach to this problem is to add a prefix to your parameter names to ensure that they do not collide with the column names. Something like
procedure find_high_avg (p_term IN courses.term%type,
p_line IN courses.lineno%type,
s_fname OUT students.fname%type,
s_lname OUT students.lname%type,
s_sid OUT students.side%type,
p_average OUT number)
is
The error on line 29 occurs because the procedure takes 6 parameters-- 2 IN and 4 OUT. In order to call it, therefore, you would need to use 6 parameters. Something like
DECLARE
myTerm courses.term%type;
myLine courses.lineno%type;
l_fname students.fname%type;
l_lname students.lname%type;
l_sid students.side%type;
l_avg number;
BEGIN
myTerm:='F12';
myLine:='1031';
find_high_avg(myTerm, myLine,l_fname,l_lname, l_sid, l_avg);
END;

I think it should be end find_high_avg; instead of end; after the select statement.

Related

hierarchical query task with pl/sql

It's my first question.I have such task in school to do.We have table with columns NAME,PARENT,MONEY,CITY.The task is to produce output names,money and average money of descendants for whom it is true, that the average money
of the descendants is greater than the person's money.
I write this code but can't understand error...
CREATE OR REPLACE PROCEDURE rich_avg_descendant IS
cnt INTEGER;
BEGIN
FOR rec IN (SELECT name, money,AVG(money) FROM ourtable) loop
SELECT count(*) INTO cnt FROM ourtable
GROUP BY name
HAVING AVG(money)> rec.money
START WITH name = rec.name CONNECT BY PRIOR name = parent;
IF cnt > 0 THEN dbms_output.put_line(name,money,AVG(money)); END IF;
END loop;
END;
/
Error is in START WITH clause that is START underlined.
Error starting at line : 14 in command -
BEGIN rich_avg_descendant(); END;
Error report -
ORA-06550: line 1, column 7:
PLS-00905: object myschoolcode.RICH_AVG_DESCENDANT is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Can you please tell my mistake in this code?
Thanks in advance.
the connect by clause should be preceding the start with clause

PL/SQL Not Exists Query

I need help to solve this issue. The Driver_ID exists in two tables, Driver and Driver_Deliveries. I need it to display the First_Name, Surname & Driver_ID for the Driver ID's that do not appear in the Driver_Deliveries table. I'm using Oracle SQL Developer with Oracle 11G.
DECLARE
FIRSTNAME VARCHAR2(20);
SUR_NAME VARCHAR2(20);
DRIVERID VARCHAR2(5);
BEGIN
FOR i IN
(
SELECT
FIRST_NAME,
SURNAME,
a.DRIVER_ID
INTO
FIRSTNAME, SUR_NAME, DRIVERID
FROM
DRIVER_DELIVERIES,
DRIVER a,
WHERE NOT EXISTS(SELECT * FROM DRIVER_DELIVERIES WHERE DRIVER_DELIVERIES.DRIVER_ID = a.DRIVER_ID);
)
LOOP
DBMS_OUTPUT.PUT_LINE('FIRST NAME :' ||I.FIRST_NAME);
DBMS_OUTPUT.PUT_LINE('SURNAME :' || I.SURNAME);
DBMS_OUTPUT.PUT_LINE('DELIVERY JOB REQUIRED: YES ');
END LOOP;
END;
Here's the error stack:
Error report -
ORA-06550: line 23, column 9:
PL/SQL: ORA-00903: invalid table name
ORA-06550: line 10, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 23, column 106:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
loop
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
You need to select into something with a simple query in a PL/SQL block, but you've taken that rule too far; because your cursor loop variable i is an implicit record type that is already available to take the query results.
You also have an extra comma in your cursor query's select list, and the from clause. And an extra semicolon after the where clause.
So with that loop syntax you need:
...
FOR i IN
(
SELECT
FIRST_NAME,
SURNAME,
a.DRIVER_ID
FROM
DRIVER_DELIVERIES,
DRIVER a
WHERE
...
Though you should really use explicit join syntax, which would make it more obvious that you currently have no link between the two tables in your where clause. But you don't actually want to refer to driver_deliveries there at all - you're getting all the data from the driver table. You only need to refer to that within the exists clause. (And I've just noticed you've spelled the table name incorrectly in that clause too).
So modifying that further:
need:
...
FOR i IN
(
SELECT
FIRST_NAME,
SURNAME,
DRIVER_ID
FROM
DRIVER a
WHERE NOT EXISTS(
SELECT null FROM DRIVER_DELIVERIES
WHERE DRIVER_DELIVERIES.DRIVER_ID = a.DRIVER_ID)
)
LOOP
...
You don't appear to need PL/SQL for this, but if you have to...

Error report: ORA-06550: line 7, column 134: PL/SQL: ORA-00913: too many values ORA-06550: line 7,

> set serveroutput on
> set autoprint on;
> declare
> v_first_name employees.first_name%type;
> v_street_address locations.street_address%type;
> v_city locations.city%type;
> v_postal_code locations.postal_code%type;
> begin
> select employee_id first_name,street_address,city,postal_code into:b_employee_id,v_first_name,v_street_address,v_city,v_postal_code
>
> from employees natural join locations
> where employee_id=156; // how to get employee_id stored in b_employee_ud
> dbms_output.put_line('the employee'||v_first_name ||' is located at:'||v_street_address|| v_city ||v_postal_code );
> end;
> /
getting error
Error report:
ORA-06550: line 7, column 134:
PL/SQL: ORA-00913: too many values
ORA-06550: line 7, column 1:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
b_employee_id
156
i want to use employee_id which is stored in b_employee_id
First of all, you're missing a comma after employee_id in your SELECT clause.
select employee_id first_name,street_address,city,postal_code
into :b_employee_id,v_first_name,v_street_address,v_city,v_postal_code
should be
select employee_id, first_name,street_address,city,postal_code
into :b_employee_id,v_first_name,v_street_address,v_city,v_postal_code
Now, coming back to the part where you want to use employee_id = 156
i want to use employee_id which is stored in b_employee_id
If your intent is not hard coding the employee_id for which you want to run your query (I am guessing this by reading the commented line at the end of your WHERE clause), then you need to use substitution variable in the WHERE clause, something like this:
WHERE employee_id = :b_emp_id
Another assumption here will be there, there is one record for one employee in the table from where you are trying to retrieve the records. You should not use a substitution variable in the variables of INTO clause
In case you want to override the value being returned in the INTO clause to some other value for any reason, you can use another variable later on in your program to do that.

stored functions execution issue

I have a table from where I want get next lowest priority DVDid, give me that as output and delete it from table. I'm doing this by creating a Package...code gets compiled but when I execute it give me error. Any suggestion?
table:
RENTALQUEUE (MEMBERID, DVDID, DATEADDEDINQUEUE, PRIORITY)
Here is the package:
create or replace
PACKAGE pk_GET_TITLEID AS
procedure sp_GET_TITLEID(memberid_arg IN NUMBER,
titleid_arg out number) ;
end pk_GET_TITLEID;
create or replace
package body pk_GET_TITLEID as
procedure sp_GET_TITLEID
(memberid_arg IN NUMBER,
titleid_arg out number)
IS
v_priority number;
begin
SELECT MIN(PRIORITY)into v_priority
FROM RENTALQUEUE
WHERE memberid = memberid_arg;
DBMS_OUTPUT.PUT_LINE (titleid_arg);
DELETE FROM RENTALQUEUE
WHERE MEMBERID=memberid_arg AND dvdid=titleid_arg;
END sp_GET_TITLEID;
END PK_GET_TITLEID;
Here is how I call it:
execute pk_get_titleid.sp_get_titleid('1');
Here is the error:
Error:
Error starting at line 403 in command:
execute pk_get_titleid.sp_get_titleid('1')
Error report:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'SP_GET_TITLEID'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
The error is:
PLS-00306: wrong number or types of arguments in call to 'SP_GET_TITLEID'
How many parameters does your procedure have in its signature?
procedure sp_GET_TITLEID
(memberid_arg IN NUMBER
, titleid_arg out number)
Two. How many parameters are you passing?
execute pk_get_titleid.sp_get_titleid('1')
One.
An out parameter is still a parameter. You need to include it in the call. Specifically you need a variable in the calling environment which can receive the answer from the called procedure. The fact that your code doesn't actually populate the titleid_arg parameter doesn't affect this.
The non-population of the titleid_arg parameter just highlights the confusion in your code. Perhaps it should be an IN parameter? Perhaps you need to select it from another table? What is the point of v_priority?

Using variables in PLSQL SELECT statement

I have a query that queries on ReportStartDate and ReportEndDate so I thought I would use variables in PLSQL. Not sure what I am missing here, but I get an error:
CLEAR;
DECLARE
varReportStartDate Date := to_date('05/01/2010', 'mm/dd/yyyy');
varReportEndDate Date := to_date('05/31/2010', 'mm/dd/yyyy');
BEGIN
SELECT
'Value TYPE',
1 AS CountType1,
2 AS CountType2,
3 AS CountType3
FROM DUAL;
SELECT COUNT (*)
FROM CDR.MSRS_E_INADVCH
WHERE 1=1
AND ReportStartDate = varReportStartDate
AND ReportEndDate = varReportEndDate
;
END;
/
The Error is:
Error starting at line 2 in command:
Error report:
ORA-06550: line 6, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
ORA-06550: line 8, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This happens in Toad as well as in SQL Developer.
What is the proper way of using the variables in my WHERE clause?
You cannot use SQL statements directly in a PL/SQL block ( unless you use EXECUTE IMMEDIATE). The columns will need to be fetched into variables ( which is what PL/SQL is telling you with PLS-00428: an INTO clause is expected in this SELECT statement error). So you'll have to rewrite your statements as below.
SELECT
'Value TYPE',
1 AS CountType1,
2 AS CountType2,
3 AS CountType3
INTO
V_VALUE_TYPE,
V_CountType1,
V_CountType2,
V_CountType3
FROM DUAL;
SELECT COUNT(*)
INTO V_COUNT
FROM CDR.MSRS_E_INADVCH
WHERE 1=1
AND ReportStartDate = varReportStartDate
AND ReportEndDate = varReportEndDate
Be sure to add Exception Handlers, since PL/SQL expects only 1 row to be returned. If the statement returns no rows, you'll hit a NO_DATA_FOUND exception - and if the statement fetches too many rows, you'll hit a TOO_MANY_ROWS exception.
The question you have to answer is what do you want to do with the data that has been selected?
Sathya gave you one approach - declare variables in your PL/SQL block and select the columns INTO those variables. Note that this requires that the SELECT statement returns exactly one row - any more or less rows will throw an error. Another way is to declare collection types using the BULK COLLECT option: http://oracletoday.blogspot.com/2005/11/bulk-collect_15.html
Yet another option is to have the procedure return a cursor. This is useful in the case where the calling code expects to be able to fetch the data that the procedure has selected:
PROCEDURE GET_MY_REPORT( varReportStartDate in date, varReportEndDate in date, cur out sys_refcursor) is
begin
OPEN cur FOR SELECT *
FROM CDR.MSRS_E_INADVCH
WHERE 1=1
AND ReportStartDate = varReportStartDate
AND ReportEndDate = varReportEndDate;
END GET_MY_REPORT;