PLS-00201 - identifier must be declared - oracle

I executed a PL/SQL script that created the following table
TABLE_NAME VARCHAR2(30) := 'B2BOWNER.SSC_Page_Map';
I made an insert function for this table using arguments
CREATE OR REPLACE FUNCTION F_SSC_Page_Map_Insert(
p_page_id IN B2BOWNER.SSC_Page_Map.Page_ID_NBR%TYPE,
p_page_type IN B2BOWNER.SSC_Page_Map.Page_Type%TYPE,
p_page_dcpn IN B2BOWNER.SSC_Page_Map.Page_Dcpn%TYPE)
I was notified I had to declare B2BOWNER.SSC_Page_Map prior to it appearing as an argument to my function. Why am I getting this error?
EDIT: Actual error
Warning: compiled but with compilation errors
Errors for FUNCTION F_SSC_PAGE_MAP_INSERT
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/48 PLS-00201: identifier 'SSC_PAGE_MAP.PAGE_ID_NBR' must be declared
0/0 PL/SQL: Compilation unit analysis terminated
EDIT: Complete PL/SQL Function
RETURN INTEGER
IS
TABLE_DOES_NOT_EXIST exception;
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942
BEGIN
INSERT INTO
B2BOWNER.SSC_Page_Map VALUES(
p_page_id,
p_page_type,
p_page_dcpn);
RETURN 0;
EXCEPTION
WHEN TABLE_DOES_NOT_EXIST THEN
RETURN -1;
WHEN DUP_VAL_ON_INDEX THEN
RETURN -2;
WHEN INVALID_NUMBER THEN
RETURN -3;
WHEN OTHERS THEN
RETURN -4;
END;
SHOW ERRORS PROCEDURE F_SSC_Page_Map_Insert;
GRANT EXECUTE ON F_SSC_Page_Map_Insert TO B2B_USER_DBROLE;
RETURN INTEGER
EDIT: I change the arguments and received a new error related to the insert command
CREATE OR REPLACE FUNCTION F_SSC_Page_Map_Insert(
p_page_id IN INTEGER,
p_page_type IN VARCHAR2,
p_page_dcpn IN VARCHAR2)
RETURN INTEGER
IS
TABLE_DOES_NOT_EXIST exception;
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942
BEGIN
INSERT INTO
B2BOWNER.SSC_Page_Map VALUES(
p_page_id,
p_page_type,
p_page_dcpn);
The error
Errors for FUNCTION F_SSC_PAGE_MAP_INSERT
LINE/COL ERROR
-------- -----------------------------------------------------------------
17/18 PL/SQL: ORA-00942: table or view does not exist
16/5 PL/SQL: SQL Statement ignored
The tables has been verified within the correct schema and with the correct attribute names and types
EDIT: I executed the following command to check if I have access
DECLARE
count_this INTEGER;
BEGIN
select count(*) into count_this
from all_tables
where owner = 'B2BOWNER'
and table_name = 'SSC_PAGE_MAP';
DBMS_OUTPUT.PUT_LINE(count_this);
END;
The output I received is
1
PL/SQL procedure successfully completed.
I have access to the table.
EDIT:
So I finally conducted an insert into the table via the schema using PL/SQL and it worked fine. It appears I simply do not have authority to create functions but that is an assumption.
EDIT:
Actual table DDL statement
v_create := 'CREATE TABLE ' || TABLE_NAME || ' (
PAGE_ID_NBR NUMERIC(10) NOT NULL Check(Page_ID_NBR > 0),
PAGE_TYPE VARCHAR2(50) NOT NULL,
PAGE_DCPN VARCHAR2(100) NOT NULL,
PRIMARY KEY(Page_ID_NBR, Page_Type))';
EXECUTE IMMEDIATE v_create;
COMMIT WORK;
COMMIT COMMENT 'Create Table';

When creating the TABLE under B2BOWNER, be sure to prefix the PL/SQL function with the Schema name; i.e. B2BOWNER.F_SSC_Page_Map_Insert.
I did not realize this until the DBAs pointed it out. I could have created the table under my root USER/SCHEMA and the PL/SQL function would have worked fine.

The procedure name should be in caps while creating procedure in database.
You may use small letters for your procedure name while calling from Java class like:
String getDBUSERByUserIdSql = "{call getDBUSERByUserId(?,?,?,?)}";
In database the name of procedure should be:
GETDBUSERBYUSERID -- (all letters in caps only)
This serves as one of the solutions for this problem.

you should give permission on your db
grant execute on (packageName or tableName) to user;

Related

is there a better way to write avg procedure?

I wrote this procedure but it keeps showing "WARNING: Procedure creates with compilation errors" and I don't know why, here's the table I created
create table EnrolledInClasses (
St_Id char(9) primary key,
C_Id char(6),
GradeN Number(2),
constraint FK_StId Foreign key (St_Id) references Student (St_Id),
constraint FK_CoID Foreign key (C_Id) references course (C_Id)
constraint CheckGrade check (Grade>-1)
);
and here's the procedure:
CREATE OR REPLACE PROCEDURE Avg_grades
IS
avg_grades NUMBER := 0;
BEGIN
SELECT AVG (Grade)
INTO avg_grades
FROM EnrolledInClasses
dbms_output.put_line('The average of grades is :'||avg_grades);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line ('No Data Is Found..');
END;
/
Since you are using PL/SQL Developer, make sure you are writing your code in a Procedure window. Then the compilation error will be clearly highlighted. In this case it's the missing semicolon at the end of the select into statement.
Or in SQL*Plus you can use show errors:
Warning: Procedure created with compilation errors.
SQL> show errors
Errors for PROCEDURE AVG_GRADES:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PL/SQL: SQL Statement ignored
9/12 PL/SQL: ORA-00933: SQL command not properly ended
or the full syntax, show errors procedure avg_grades.
Or you can directly query dba|all|cdb|user_errors:
select * from user_errors e
where e.name = 'AVG_GRADES'
and e.attribute = 'ERROR';
Also, the table definition could be written more succinctly as:
create table enrolledinclasses
( st_id primary key constraint fk_stid references student(st_id)
, c_id constraint fk_coid references course (c_id)
, grade number(2) constraint checkgrade check (grade>-1)
);
student.st_id and course.c_id should probably be numbers or integers, or at least varchar2, and not char.
It is good practice to use code indentation to align code into blocks showing their dependency structure. That would give something like this:
create or replace procedure avg_grades as
avg_grades number := 0;
begin
select avg(grade) into avg_grades
from enrolledinclasses;
dbms_output.put_line('The average grade is: ' || avg_grades);
exception
when no_data_found then
dbms_output.put_line('No Data Is Found.');
end;
You might want to round() that average before displaying it, as by default it could be displayed with a lot more decimal places than you might want.
Also, it's not possible to get a no_data_found exception from a single-group aggregate query like this. If there are no rows in the table you will get a null value as the result, not an error.

Oracle Pl/Sql Procedure Help Merge

I have a procedure created and i am using a merge query inside it.
when i compile the procedure, there is no error and when i try to execute it giving me
below error. Can someone help in solving this.
Code:
CREATE OR REPLACE PROCEDURE DEVICE.Check1
IS
BEGIN
MERGE INTO DEVICE.APP_C_CATEGORY A
USING (SELECT market_segment_id,
market_segment_name,
UPDATE_USER,
UPDATE_DATE
FROM CUST_INTEL.MSE_MARKET_SEGMENT_MASTER#SOURCE_CUST_INTEL.ITG.TI.COM
WHERE market_segment_id NOT IN ('120', '130', '100')) B
ON (A.APP_CATEGORY_ID = B.market_segment_id
AND A.APP_CATEGORY_NAME = B.market_segment_name)
WHEN MATCHED
THEN
UPDATE SET A.DESCRIPTION = B.market_segment_name,
A.PARENT_APP_AREA_ID = NULL,
A.RECORD_CHANGED_BY = B.UPDATE_USER,
A.RECORD_CHANGE_DATE = B.UPDATE_DATE
WHEN NOT MATCHED
THEN
INSERT (A.APP_CATEGORY_NAME,
A.DESCRIPTION,
A.TYPE,
A.PARENT_APP_AREA_ID,
A.RECORD_CHANGED_BY,
A.RECORD_CHANGE_DATE)
VALUES (B.market_segment_name,
B.market_segment_name,
1,
NULL,
B.UPDATE_USER,
B.UPDATE_DATE);
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE (SQLERRM);
DBMS_OUTPUT.PUT_LINE (SQLCODE);
END;
/
Error: when i am executing
BEGIN
DEVICE.CHECK1;
COMMIT;
END;
the following errors occur:
ORA-06550: line 2, column 10:
PLS-00302: component 'CHECK1' must be declared
ORA-06550: line 2, column 3:
PL/SQL: Statement ignored
This is nothing to do with the merge, it isn't getting as far as actually executing your procedure. From the create procedure statement you have a schema called DEVICE. Since the error message is only complaining about CHECK1, not DEVICE.CHECK1, you also appear to have a package called DEVICE. Your anonymous block is trying to find a procedure called CHECK1 within that package, not at schema level.
If you are connected as the device schema owner (user) when you execute this, just remove the schema prefix:
BEGIN
CHECK1;
COMMIT;
END;
/

Creation of table and insertion within the same procedure in pl/sql

I'm trying to create a table and then insert some values in it within the same procedure in pl/sql. I tried to run the following query without success:
create or replace Procedure insertval8(id_no in number,e_name in char)
is
begin
execute immediate 'create table edu2(id number(20), name char(12))';
insert into edu2 values(&id_no,&e_name);
end;
displays
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/1 PL/SQL: SQL Statement ignored
5/13 PL/SQL: ORA-00942: table or view does not exists
The error persists until I remove the insert code.
The procedure cannot be compiled because the table is not present at compile time.
Wrap the insert in execute immediate also, or use a global temporary table (generaly the preferred solution for temporary data).
create or replace procedure insertval8 (id in number,
name in char )
is
begin
execute immediate 'create table edu2(id number(20), name char(12))';
execute immediate 'insert into edu2(id, name) values (:1, :2)'
using id, name;
end;

Oracle returns 'Table does not exist' for a valid object

Package mypackage.bdy owned by user mq has a public procedure CALCTAX. This procedure refers to a table TAXINFO owned by another mr.
code
PROCEDURE CALCTAX(P_TAX_END_DAT IN DATE,
P_CODE IN VARCHAR2,
P_DEFER OUT NUMBER) IS
BEGIN
IF (P_TAX_END_DAT <= V_FECHA_FIN_PERIODO) THEN
P_DEFER := 15;
ELSE
BEGIN
SELECT 15
INTO P_DEFER
FROM MR.taxinfo T
WHERE SUBSTR(P_MINOR_CODE, 2, 5) IN T.TAX_CODE_NAME;
EXCEPTION
when not found
....
I get the error PL/SQL: SQL Statement ignored PL/SQL: ORA-00942: table or view does not exist while trying to compile the package.
Can anyone help me solve this issue?
Try this DCL and then your procedure
GRANT SELECT ON MR.TAXINFO TO MQ;

Variable in UPDATE oracle in procedure : invalid identifier

I don't understand why service is complaining with
Fehler(36,11): PL/SQL: ORA-00904: "FOUND_VP": invalid identifier
Variable is declared in the first begin...
Is it not possible to use variable directly in queries ?
when trying store following procedure :
create or replace PROCEDURE fpwl_update_vp(
my_zn IN NUMBER, my_verwaltung IN VARCHAR2 , my_variante IN NUMBER, my_vp IN NUMBER
) IS
BEGIN
DECLARE
search_VP IFT_INFO_LAUF.VP%TYPE;
found_VP IFT_INFO_LAUF.VP%TYPE;
INFOversion number := 25;
BEGIN -- search SYFA_VP
SELECT SYFA_VP
INTO found_VP
FROM FPWL_VP_MAPPING
WHERE INFO_VP=search_VP ;
exception
when no_data_found then
dbms_output.put_line ('Kein SYFA VP : Importiere aus SYFA');
--found_VP:=:=cus_info25.pa_info_data.fn_insert_syfa_vp(my_vp,25);
WHEN OTHERS THEN
ROLLBACK;
RETURN;
END; -- SYFA VP
-- Update VP
UPDATE IFT_INFO_LAUF
SET vp = found_VP
WHERE id_kopf IN
(SELECT id_kopf
FROM ift_info_kopf
WHERE fahrtnummer= my_zn
AND verwaltung= my_verwaltung
AND variante = my_variante
)
;
--COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END ;
Your problem is that found_VP is going out of scope.
Move the contents of the "DECLARE" block to just after the "IS":
create or replace PROCEDURE fpwl_update_vp(
my_zn IN NUMBER, my_verwaltung IN VARCHAR2 , my_variante IN NUMBER, my_vp IN NUMBER
) IS
search_VP IFT_INFO_LAUF.VP%TYPE;
found_VP IFT_INFO_LAUF.VP%TYPE;
INFOversion number := 25;
BEGIN
BEGIN -- search SYFA_VP
etc
Make sure that
FPWL_VP_MAPPING.SYFA_VP
is the same type with
IFT_INFO_LAUF.VP
and make sure that
SELECT SYFA_VP INTO found_VP FROM FPWL_VP_MAPPING WHERE INFO_VP=search_VP ;
does not return multiple rows.
But I doubt that is the case with the error that you have given.
Since the error message refers to line 36 and the reference to found_VP in your code sample is on line 18, you've omitted the part of the code that actually has the problem.
It looks like you have a scope problem; you're declaring found_VP in an inner block (one level of DECLARE/BEGIN/END) and referring to it outside that block, either in the parent block or another one at the same level. The issue isn't where you're selecting into found_VP, it's (I think) that you're referring to it again later on, beyond the code you've posted, and therefore outside the block the variable is declared in.
To demonstrate, I'll declare l_name in an inner block, as you seem to have done:
create or replace procedure p42 is
begin
declare
l_name all_tables.table_name%TYPE;
begin
select table_name
into l_name -- this reference is OK
from all_tables
where table_name = 'EMPLOYEES';
end;
select table_name
into l_name -- this reference errors
from all_tables
where table_name = 'JOBS';
end;
/
Warning: Procedure created with compilation errors.
show errors
Errors for PROCEDURE P42:
LINE/COL ERROR
-------- -----------------------------------------------------------------
12/2 PL/SQL: SQL Statement ignored
13/7 PLS-00201: identifier 'L_NAME' must be declared
14/2 PL/SQL: ORA-00904: : invalid identifier
Notice that the error is reported against line 13, which is in the outer block; it doesn't complain about it in the inner block because it is in-scope there.
So, you need to declare the variable at the appropriate level. As Colin 't Hart says that is probably right at the top, between the IS and the first BEGIN, as that is the procedure-level DECLARE section (it doesn't need an explicit DECLARE keyword).

Resources