Error while executing PL/SQL block - sql

set serveroutput on;
begin
for rec in (select * from emp) loop
insert into emp (stars) values (rpad(' ', salary/1000, '*'));
end loop;
end;
/
Returns the following errors:
Error report:
ORA-06550: line 3, column 45:
PL/SQL: ORA-00984: column not allowed here
ORA-06550: line 3, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I want to add in the stars column of the emp table (same as employees), a string of asterisks one for each 1000 in salary.
Could someone please explain what the error is about. Thanks

Sorry just spotted my mistake.
update emp set stars = rpad('*', salary/1000, '*');
is the right 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...

Select Object Table Into Variable

I've got a problem with trying to select something from a object table into my own local variable. Here's some basic code.
create type my_obj as object
(
my_number number
)
/
create table my_table of my_obj;
/
insert into my_table values (my_obj(123))
/
declare
my_holder my_obj;
begin
select * into my_holder from my_table where rownum = 1;
dbms_output.put_line(my_holder.my_number);
end;
The error I'm getting out of it is
Error starting at line : 86 in command -
declare
my_holder my_obj;
begin
select * into my_holder from my_table where rownum = 1;
end;
Error report -
ORA-06550: line 4, column 10:
PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got NUMBER
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Any clues on why this might be failing?
The select returns the variables from the object not the object itself so if you wanted to get it to work the way you have it you would need to declare that you want the object returned as below.
select my_obj(my_number) into my_holder from my_table where rownum = 1;
Hope this helps

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.

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

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.