Getting error while compiling the procedure - oracle

I have declared a array inside the procedure. When i write create or replace PROCEDURE myprocedure , its gets compiled but when i write procedure myprocedure it gives the below error.I have to keep this inside a pl sql package. I am new to procedure . Please help.
PROCEDURE myprocedure
IS
type namesarray IS VARRAY(5) OF VARCHAR2(10);
type grades IS VARRAY(5) OF INTEGER;
names namesarray;
marks grades;
total integer;
BEGIN
names := namesarray('Kavita', 'Pritam', 'Ayan', 'Rishav', 'Aziz');
marks:= grades(98, 97, 78, 87, 92);
total := names.count;
dbms_output.put_line('Total '|| total || ' Students');
FOR i in 1 .. total LOOP
dbms_output.put_line('Student: ' || names(i) || '
Marks: ' || marks(i));
END LOOP;
END;
error
RA-06550: line 2, column 4:
PLS-00201: identifier 'NAMES' must be declared
ORA-06550: line 2, column 4:
PL/SQL: Statement ignored
ORA-06550: line 3, column 4:
PLS-00201: identifier 'MARKS' must be declared
ORA-06550: line 3, column 4:
PL/SQL: Statement ignored
ORA-06550: line 4, column 4:
PLS-00201: identifier 'TOTAL' must be declared
ORA-06550: line 4, column 4:
PL/SQL: Statement ignored
ORA-06550: line 5, column 36:
PLS-00201: identifier 'TOTAL' must be declared
ORA-06550: line 5, column 4:
PL/SQL: Statement ignored
ORA-06550: line 6, column 18:
PLS-00201: identifier 'TOTAL' must be declared
ORA-06550: line 6, column 4:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

write procedure myprocedure it gives the below error.I have to keep
this inside a pl sql package.
Here is the package working fine for me:
CREATE OR REPLACE PACKAGE my_test
AS
PROCEDURE myprocedure;
END;
/
CREATE OR REPLACE PACKAGE BODY my_test
AS
PROCEDURE myprocedure
IS
TYPE namesarray IS VARRAY (5) OF VARCHAR2 (10);
TYPE grades IS VARRAY (5) OF INTEGER;
names namesarray;
marks grades;
total INTEGER;
BEGIN
names :=
namesarray ('Kavita',
'Pritam',
'Ayan',
'Rishav',
'Aziz');
marks :=
grades (98,
97,
78,
87,
92);
--total := names.COUNT;
DBMS_OUTPUT.put_line ('Total ' || names.COUNT || ' Students');
FOR i IN 1 .. names.COUNT
LOOP
DBMS_OUTPUT.put_line (
'Student: ' || names (i) || ' Marks: ' || marks (i));
END LOOP;
END;
END;
Execution:
SQL> EXEC my_test.MYPROCEDURE;
Total 5 Students
Student: Kavita Marks: 98
Student: Pritam Marks: 97
Student: Ayan Marks: 78
Student: Rishav Marks: 87
Student: Aziz Marks: 92
PL/SQL procedure successfully completed.

Related

PL/SQL -- substitution variable

I am using SQL Developer and writing this PL/SQL code, but I am getting error. Please help.
SET SERVEROUTPUT ON
ACCEPT name prompt 'Name of employee'
declare
v_sal NUMBER;
BEGIN
v_sal := &name;
SELECT
salary
INTO v_sal
FROM
employees
WHERE
first_name = '&name';
dbms_output.put_line('Employee ' || &name || ' has the salary' || v_sal);
END;
////
Error report -
ORA-06550: linia 4, coloana 14:
PLS-00201: identifier 'MOZHE' must be declared
ORA-06550: linia 4, coloana 5:
PL/SQL: Statement ignored
ORA-06550: linia 12, coloana 54:
PLS-00201: identifier 'MOZHE' must be declared
ORA-06550: linia 12, coloana 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
You want:
SET SERVEROUTPUT ON
ACCEPT name prompt 'Name of employee'
DECLARE
v_sal NUMBER;
BEGIN
SELECT salary
INTO v_sal
FROM employees
WHERE first_name = '&&name';
dbms_output.put_line('Employee &&name has the salary' || v_sal);
END;
/

How do I declare a query in an oracle pl/sql statement?

I'm fairly new to Oracle but Ive been tasked to write this statement .
BEGIN
FOR partition IN 1..32 LOOP
lQuery := 'UPDATE CORE.tbl PARTITION(tbl' || LPAD(partition, 2, '0') || ') SET p = NULL '
|| 'WHERE p IS NOT NULL';
EXECUTE IMMEDIATE lQuery;
END LOOP;
END;
However, I get the following error
Error starting at line 1 in command:
BEGIN
FOR partition IN 1..32 LOOP
lQuery := 'UPDATE CORE.user_login PARTITION(user_login' || LPAD(partition, 2, '0') || ') SET password_md5 = NULL '
|| 'WHERE password_md5 IS NOT NULL';
EXECUTE IMMEDIATE lQuery;
END LOOP;
END;
Error report:
ORA-06550: line 3, column 5:
PLS-00201: identifier 'LQUERY' must be declared
ORA-06550: line 3, column 5:
PL/SQL: Statement ignored
ORA-06550: line 6, column 23:
PLS-00201: identifier 'LQUERY' must be declared
ORA-06550: line 6, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
when I try to run it. I understand that this might be because of not declaring lQuery but googling online showed me examples of declaring variables of a certain type such as (integer, varchar.. etc.) but none of them showed how to declare a query as this statement expects.
Your statement may be simply declared as a varchar2. For example:
declare
vsql varchar2(1000);
n integer;
begin
vsql := 'select 1 from dual';
execute immediate vsql into n;
dbms_output.put_line(n);
end;

plsql comparing table and view

I need to get comparison of columns between table and a view i choose to see if there are matching columns.(in PLSQL)
The columns that don't match need to be outputed as: column1 in view1 is missing in table1 etc.
This is so far i have done, but it gives me an error:
DECLARE
CURSOR c_col
IS
SELECT T.TABLE_NAME,
T.COLUMN_NAME,
V.TABLE_NAME,
V.COLUMN_NAME
FROM ALL_TAB_COLUMNS T
FULL JOIN ALL_TAB_COLUMNS V
ON T.column_name=V.column_NAME
AND v.table_name='EMP_V'
AND v.owner ='HR'
WHERE T. OWNER ='HR'
and T.TABLE_NAME='EMPLOYEES';
v_table c_col%rowtype;
begin
OPEN C_col;
LOOP
FETCH C_col into V_TABLe;
EXIT when C_col%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (v_table.table_name||' '||V_table.column_name);
end LOOP;
close c_col;
end;
this is the error i keep getting:
Error report -
ORA-06550: line 16, column 13:
PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names
ORA-06550: line 16, column 13:
PL/SQL: Item ignored
ORA-06550: line 20, column 22:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 20, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 22, column 27:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 22, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Try this, for the error cause see the #Multisync comment
declare
cursor c_col is
select T.TABLE_NAME, T.COLUMN_NAME, V.TABLE_NAME VIEW_NAME,
V.COLUMN_NAME VIEW_COLUMN_NAME
from (select *
from ALL_TAB_COLUMNS T
where OWNER = 'HR'
and TABLE_NAME = 'EMPLOYEES') T
full outer join (select *
from ALL_TAB_COLUMNS
where owner = 'HR'
and table_name = 'EMP_V') V on T.column_name =
V.column_NAME
order by t.column_name, v.column_name;
v_table c_col%rowtype;
begin
open C_col;
loop
fetch C_col
into V_TABLe;
exit when C_col%notfound;
DBMS_OUTPUT.PUT_LINE(rpad(v_table.table_name || ' ' ||
V_table.column_name, 30, ' ') || '| ' ||
v_table.VIEW_NAME || ' ' ||
V_table.VIEW_COLUMN_NAME);
end loop;
close c_col;
end;

'[Error] Execution (8: 3): ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 5' in the following plsql

thanks for all,i had modified from varchar2(10) to varchar2(20) and removing the column keyword in execute immediate statement then program is executing
declare
coldate varchar2(20);
colname varchar2(20);
begin
coldate :='varchar2(10)';
colname :='smaple';
execute immediate 'alter table smap1 add column '||colname ||' '||coldate ;
end;
if i want to take the values dynamically i had used the following code
declare
coldate varchar2(20):=&coldate;
colname varchar2(20):=&colname;
begin
execute immediate 'alter table smap1 add '||colname ||' '||coldate ;
end;
then i am getting errors
[Error] Execution (11: 23): ORA-06550: line 2, column 23:
PLS-00330: invalid use of type name or subtype name
ORA-06550: line 2, column 9:
PL/SQL: Item ignored
ORA-06550: line 3, column 23:
PLS-00201: identifier 'SMAPLE' must be declared
ORA-06550: line 3, column 9:
PL/SQL: Item ignored
ORA-06550: line 6, column 45:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 6, column 1:
PL/SQL: Statement ignored
declare
coldate varchar2(20);
colname varchar2(20);
begin
coldate :='varchar2(10)';
colname :='smaple';
execute immediate 'alter table smap1 add column '||colname ||' '||coldate ;
end;
This query wont work as syntactically its not correct.
Please try this as it will help you definitely
declare
coldate varchar2(20):='varchar2(20)';
colname varchar2(20):='Newadd';
begin
execute immediate 'alter table <tablename> add '||colname ||' '||coldate ;
end;
-- And to use dynamically input values Please try this
SET DEFINE ON;
declare
coldate varchar2(20):='&DATATYPE';
colname varchar2(20):='&COL_NAME';
begin
execute immediate 'alter table emp add '||colname ||' '||coldate ;
end;
The below will solve it:
coldate varchar2(12);
colname varchar2(20);
begin
coldate :='varchar2(10)';

Missing keyword error in Oracle anonymous PL/SQL block

An anonymous PL/SQL block:
DECLARE
CURSOR employees_in_10_cur
IS
SELECT *
FROM ad_week_table
BEGIN
FOR employee_rec
IN employees_in_10_cur
LOOP
DBMS_OUTPUT.put_line(employee_rec.ad_no || ',' || employee_rec.week_no);
END LOOP;
END;
I get a missing keyword error when I execute this block. What am I doing wrong?
put a semi-colon after defining cursor query;
DECLARE
CURSOR employees_in_10_cur
IS
SELECT *
FROM ad_week_table;
BEGIN
FOR employee_rec
IN employees_in_10_cur
LOOP
DBMS_OUTPUT.put_line (
employee_rec.ad_no || ',' || employee_rec.week_no );
END LOOP;
END;
You're missing a semi-colon at the end of your cursor:
CURSOR employees_in_10_cur
IS
SELECT *
FROM ad_week_table;
The error message will always give you the line number of the error occurring. Please always check this and look in that area.
For instance; if I change the table so I can run this I get the following:
SQL> declare
2
3 cursor employees_in_10_cur is
4 select *
5 from user_tables
6
7 begin
8 for employee_rec in employees_in_10_cur loop
9 dbms_output.put_line (
10 employee_rec.ad_no || ',' || employee_rec.week_no );
11 end loop;
12 end;
13 /
for employee_rec in employees_in_10_cur loop
*
ERROR at line 8:
ORA-06550: line 8, column 8:
PL/SQL: ORA-00905: missing keyword
ORA-06550: line 4, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 11, column 4:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
You have 3 errors; one at line 8 because the cursor is invalid, one at line 11 because the loop doesn't happen because the cursor is invalid and one at line 4, which says "statement ignored".

Resources