plsql procedures while executing an error invalid identifier 00904 - oracle

Error message
CREATE TABLE DEPARTMENT(DID INT,DNAME VARCHAR2(20),DLOC VARCHAR2(20));
INSERT INTO DEPARTMENT VALUES(101,'SRINATH','HINDUPUR');
INSERT INTO DEPARTMENT VALUES(102,'SAINATH','ANANTAPUR');
create or replace PROCEDURE ADD_DEPARTMENT
(P_DID IN DEPARTMENT.DID%TYPE,
P_DNAME IN DEPARTMENT.DNAME%TYPE,
P_DLOC IN DEPARTMENT.DLOC%TYPE,
P_ERROR_MSG OUT VARCHAR2)
IS
BEGIN
INSERT INTO DEPARTMENT(DID,DNAME,DLOC)VALUES(P_DID,P_DNAME,P_DLOC);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
P_ERROR_MSG:=SQLERRM;
END ADD_DEPARTMENT;
iam writing a simple procedure try to excute but it shows object_id:invalid identifier how i can solve
complete procedure execution image

One of your column names may be wrong in the parameter list/insert. Try just using varchar2 for all of them and see if that helps
create or replace PROCEDURE ADD_DEPT2
(P_DEPTNO IN VARCHAR2,
P_DNAME IN VARCHAR2,
P_LOC IN VARCHAR2,
P_ERROR_MSG OUT VARCHAR2)
IS
BEGIN
INSERT INTO DEPT1(DEPTNO,DNAME,LOC)VALUES(P_DEPTNO,P_DNAME,P_LOC);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
P_ERROR_MSG :=SQLERRM;
END ADD_DEPT2;

"it will come same error can be displayed"
No, no it won't. Let's prove it by running the code you posted.
Here is your table:
SQL> CREATE TABLE DEPARTMENT(DID INT,DNAME VARCHAR2(20),DLOC VARCHAR2(20));
Table created.
SQL> INSERT INTO DEPARTMENT VALUES(101,'SRINATH','HINDUPUR');
1 row created.
SQL> INSERT INTO DEPARTMENT VALUES(102,'SAINATH','ANANTAPUR');
1 row created.
SQL>
Here is your procedure:
SQL> create or replace PROCEDURE ADD_DEPARTMENT
2 (P_DID IN DEPARTMENT.DID%TYPE,
3 P_DNAME IN DEPARTMENT.DNAME%TYPE,
4 P_DLOC IN DEPARTMENT.DLOC%TYPE,
5 P_ERROR_MSG OUT VARCHAR2)
6 IS
7 BEGIN
8 INSERT INTO DEPARTMENT(DID,DNAME,DLOC)VALUES(P_DID,P_DNAME,P_DLOC);
9 COMMIT;
10 EXCEPTION
11 WHEN OTHERS THEN
12 P_ERROR_MSG:=SQLERRM;
13 END ADD_DEPARTMENT;
14 /
Procedure created.
SQL>
And lo! not only does it compile but it runs without error too.
SQL> set serveroutput on
SQL> declare
2 v varchar2(128);
3 begin
4 add_department(666, 'Area 51', 'Roswell', v);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select * from department;
DID DNAME DLOC
---------- -------------------- --------------------
101 SRINATH HINDUPUR
102 SAINATH ANANTAPUR
666 Area 51 Roswell
SQL>
So once again I ask, what is the problem?
"PLS-00905: object SCOTT.ADD_DEPARTMENT is invalid"
In SQL*Plus:
Connect as SCOTT
alter procedure add_department compile;
show errors
Or apply the equivalent sequence for whatever IDE you use to write your code.

Related

ORACLE - can i pass cursor in a parameter and how to use it?

Here is one of oracle procedure. There is a cursor called r_ba.
DECLARE
r_ba SYS_REFCURSOR;
BEGIN
OPEN r_ba FOR
SELECT head.*,
line.jenis_pekerjaan,
line.deskripsi_pekerjaan,
line.akun,
line.rate_ppn,
line.dpp,
line.ppn,
line.total_realisasi,
line.major,
line.minor,
line.sla
FROM idm_ap_mtc_acl_header_tmp head, idm_ap_mtc_acl_lines_tmp line
WHERE head.no_ba = line.no_ba AND head.req_id = line.req_id AND head.req_id = n_req_id;
create_invoice_ap2 (
p_org_id => p_org_id,
p_user_id => p_user_id,
p_branch_code => v_branch_code,
r_ba => r_ba);
END;
and how call that cursor in this procedure ?
PROCEDURE create_invoice_ap2 (p_org_id IN NUMBER,
p_user_id IN NUMBER,
p_branch_code IN VARCHAR2,
r_ba IN SYS_REFCURSOR)
IS
r_tax_info rt_tax_info;
rt_ba SYS_REFCURSOR;
BEGIN
------------ setup ------------
r_tax_info := get_tax_code (
p_date => rt_ba.tgl_ba,
p_group_tax_name => rt_ba.tax_name,
p_ppn_rate => rt_ba.ppn_rate,
p_dc_code => rt_ba.dc_code);
END;
oracle give me error
[Error] PLS-00487 (707: 64): PLS-00487: Invalid reference to variable 'RT_BA'
Anyone could suggest me a way to accomplish this?
Thanks
If you're passing refcursor into a parameter named r_ba, then - I believe - you should use it, not declare another (rt_ba):
PROCEDURE create_invoice_ap2 (p_org_id IN NUMBER,
p_user_id IN NUMBER,
p_branch_code IN VARCHAR2,
r_ba IN SYS_REFCURSOR)
IS
r_tax_info rt_tax_info;
-- rt_ba SYS_REFCURSOR; --> you don't need it
BEGIN
------------ setup ------------
r_tax_info := get_tax_code (
p_date => r_ba.tgl_ba,
p_group_tax_name => r_ba.tax_name,
p_ppn_rate => r_ba.ppn_rate,
p_dc_code => r_ba.dc_code);
END; ----
^
|
r_ba, not rt_ba
Also, note that you should generally avoid select * because it can lead to ambiguity if there are columns with the same name as columns in other tables, so Oracle doesn't know which one you want to use.
The same goes for typos; for example, in anonymous PL/SQL block you're selecting line.rate_ppn, while procedure uses p_ppn_rate => r_ba.ppn_rate (rate_ppn vs. ppn_rate). Maybe it is OK because there's ppn_rate column in head table (which is in select head.*) but - once again - that's difficult to debug.
Furthermore, you can't use refcursor that way - you have to fetch from it. Have a look at example based on Scott's sample schema. Here's a procedure that accepts refcursor as a parameter and does something with it:
SQL> create or replace procedure p_test (r_ba in sys_refcursor)
2 is
3 l_dname varchar2(20);
4 begin
5 l_dname := r_ba.dname;
6 end;
7 /
Warning: Procedure created with compilation errors.
SQL> show err
Errors for PROCEDURE P_TEST:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/3 PL/SQL: Statement ignored
5/19 PLS-00487: Invalid reference to variable 'R_BA'
SQL>
See? The same error you got. Therefore, fetch:
SQL> create or replace procedure p_test (r_ba in sys_refcursor)
2 is
3 l_row dept%rowtype;
4 begin
5 loop
6 fetch r_ba into l_row;
7 exit when r_ba%notfound;
8 dbms_output.put_line(l_row.deptno ||', '|| l_row.dname ||', '|| l_row.loc);
9 end loop;
10 end;
11 /
Procedure created.
SQL> declare
2 r_ba sys_refcursor;
3 begin
4 open r_ba for select deptno, dname, loc from dept;
5 p_test (r_ba);
6 end;
7 /
10, ACCOUNTING, NEW YORK
20, RESEARCH, DALLAS
30, SALES, CHICAGO
40, OPERATIONS, BOSTON
PL/SQL procedure successfully completed.
SQL>
Now it is OK.
I don't know what you're doing with r_tax_info, how many rows refcursor contains (whether you need to use a loop or not), but - that's the general idea. Fetch first, use it next.

I am trying to insert data into a table through procedure but an error occur i.e.' ORA-00955: name is already used by an existing object'

CREATE PROCEDURE proc_insert (p_emp_id IN munber,
p_emp_ename IN VARCHAR2,
p_emp_job IN VARCHAR2,
p_emp_dept IN NUMBER,
p_emp_hiredate IN DATE,
p_emp_loc IN VARCHAR2)
IS
BEGIN
INSERT INTO emp
VALUES (p_emp_id,
p_emp_ename,
p_emp_job,
p_emp_dept,
p_emp_hiredate,
p_emp_loc);
END;
EXECUTE proc_insert(33,'rohit','web developer',10,'12-apr-2020','kanpur');
It is not the insert that's causing problem, but the fact that object named proc_insert already exists in this schema.
Find out which one is it and - if you don't need it - drop it. How? Query user_objects.
Or, maybe you actually wanted to use
create OR REPLACE procedure proc_insert ...
----------
which would do the job.
As of code you posted: note that p_emp_hiredate's datatype is date, while you're passing a string to it. '12-apr-2020' is a string, not a date. You'd rather not rely on current NLS settings and Oracle's implicit datatype conversion - pass date, e.g.
a date literal: date '2022-04-12'
or use to_date function: to_date('12-apr-2020', 'dd-mon-yyyy', 'nls_date_language=english')
To illustrate it:
SQL> CREATE OR REPLACE PROCEDURE proc_insert (p_emp_id IN NUMBER,
2 p_emp_ename IN VARCHAR2,
3 p_emp_job IN VARCHAR2,
4 p_emp_dept IN NUMBER,
5 p_emp_hiredate IN DATE,
6 p_emp_loc IN VARCHAR2)
7 IS
8 BEGIN
9 INSERT INTO temp
10 VALUES (p_emp_id,
11 p_emp_ename,
12 p_emp_job,
13 p_emp_dept,
14 p_emp_hiredate,
15 p_emp_loc);
16 END;
17 /
Procedure created.
SQL> EXECUTE proc_insert(33,'rohit','web developer',10,date '2020-04-12','kanpur');
PL/SQL procedure successfully completed.
SQL> SELECT * FROM temp;
ID ENAME JOB DEPT HIREDATE LOC
---------- ---------- --------------- ---------- ---------- ----------
33 rohit web developer 10 12.04.2020 kanpur
SQL>

SQL statement ignored and missing opening parenthesis

I get these errors during a creation of a trigger but I don't understand why
create or replace NONEDITIONABLE TRIGGER magazzino_bef_ins
BEFORE INSERT ON MAGAZZINO
FOR EACH ROW
DECLARE
tempcodice varchar2(8);
cacc char(3);
ogg int;
CURSOR cursore_disp IS
SELECT cacciatore, oggetto
FROM TABLE EDDY.disponibilita
WHERE ID_DISPONIBILITA = :NEW.disponibilità;
BEGIN
CURSOR cursore_disp IS
SELECT cacciatore, oggetto
FROM TABLE DISPONIBILITA
WHERE ID_DISPONIBILITA = :NEW.disponibilità;
open cursore_disp;
fetch cursore_disp into cacc,ogg;
temp:= pk_gestione_magazzino.genera_catalogo(cacc,ogg);
:new.codcatalogo:=temp;
END;
The errors are:
Error(5,9): PL/SQL: SQL Statement ignored
Errore(6,20): PL/SQL: ORA-00906: missing opening parenthesis
Errore(9,8): PLS-00103: encountered symbol "CURSORE_DISP" instead of one of the following: := . ( # % ;
I can't understand these errors, I'm just trying to take the values from a table in where the id inserted is equal in the other table.
Here's how. I created test environment which is kind of stupid, but it makes the following code work.
SQL> create table magazzino (disponibilita number, codcatalogo varchar2(8));
Table created.
SQL> create table disponibilita (cacciatore char(3), oggetto number, id_disponibilita number);
Table created.
SQL> create or replace package pk_gestione_magazzino as
2 function genera_catalogo (cacc number, ogg number) return varchar2;
3 end;
4 /
Package created.
SQL> create or replace package body pk_Gestione_magazzino as
2 function genera_catalogo (cacc number, ogg number) return varchar2 is
3 begin
4 return 'Little';
5 end;
6 end;
7 /
Package body created.
Trigger which causes you trouble: no need for a cursor; moreover, you have two of them (was it just a typo)? It looks as if you used it to avoid no data found error. If so, don't do it - it just makes confusion. Handle exceptions properly. By the way, you "forgot" to close the cursor.
SQL> create or replace trigger magazzino_bef_ins
2 before insert on magazzino
3 for each row
4 declare
5 cacc char(3);
6 ogg int;
7 begin
8 select cacciatore, oggetto
9 into cacc, ogg
10 from disponibilita
11 where id_disponibilita = :new.disponibilita;
12
13 :new.codcatalogo := pk_gestione_magazzino.genera_catalogo (cacc, ogg);
14 end;
15 /
Trigger created.
Testing:
SQL> insert into disponibilita values (1, 1, 1);
1 row created.
SQL> insert into magazzino (disponibilita) values (1);
1 row created.
SQL> select * From magazzino;
DISPONIBILITA CODCATAL
------------- --------
1 Little
SQL>
Works, kind of.

Using synonyms with Oracle package

I created a synonym for a package, but I can't do anything with it, including a simple desc command. Check out the codes:
Create package sql (executed on owner user)
create or replace PACKAGE pkg_elmah$log_error
IS
PROCEDURE LogError
(
v_ErrorId IN elmah$error.errorid%TYPE,
v_Application IN elmah$error.application%TYPE,
v_Host IN elmah$error.host%TYPE,
v_Type IN elmah$error.type%TYPE,
v_Source IN elmah$error.source%TYPE,
v_Message IN elmah$error.message%TYPE,
v_User IN elmah$error.username%TYPE,
v_AllXml IN elmah$error.allxml%TYPE,
v_StatusCode IN elmah$error.statuscode%TYPE,
v_TimeUtc IN elmah$error.timeutc%TYPE
);
END pkg_elmah$log_error;
Create synonym and test sql (executed on not owner user)
CREATE SYNONYM pkg_elmah$log_error FOR DRSP.pkg_elmah$log_error;
desc pkg_elmah$log_error;
---------RESULT: object does not exist
execute pkg_elmah$log_error.logerror;
---------RESULT: identifier pkg_elmah$log_error must be declared
These codes run fine when I log in with the owner of these objects.
Grant was given as follows (I seem unable to give to the procedure only, as it gives an ORA-04042, so I gave it to the package):
grant execute on PKG_ELMAH$LOG_ERROR to not_owner
Environment: WINDOWS 7 64bits; SQL DEVELOPER 4.0.3.16; ORACLE CLIENT 11.2
Details of the synonym:
Details of the package:
You are doing something in the wrong order or have otherwise omitted to tell us something. Because the instructions carried out in the right order work as expected:
SQL> conn a/a
Connected.
USER is "A"
SQL> create or replace PACKAGE pkg_elmah$log_error
2 IS
3 PROCEDURE LogError
4 (
5 v_ErrorId IN number,
6 v_Application IN varchar2,
7 v_Host IN varchar2,
8 v_Type IN varchar2,
9 v_Source IN varchar2,
10 v_Message IN varchar2,
11 v_User IN varchar2,
12 v_AllXml IN varchar2,
13 v_StatusCode IN varchar2,
14 v_TimeUtc IN date
15 );
16
17 END pkg_elmah$log_error;
18 /
Package created.
SQL> grant execute on PKG_ELMAH$LOG_ERROR to b
2 /
Grant succeeded.
SQL> conn b/b
Connected.
USER is "B"
SQL> CREATE SYNONYM pkg_elmah$log_error FOR A.pkg_elmah$log_error;
Synonym created.
SQL> desc pkg_elmah$log_error
PROCEDURE LOGERROR
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
V_ERRORID NUMBER IN
V_APPLICATION VARCHAR2 IN
V_HOST VARCHAR2 IN
V_TYPE VARCHAR2 IN
V_SOURCE VARCHAR2 IN
V_MESSAGE VARCHAR2 IN
V_USER VARCHAR2 IN
V_ALLXML VARCHAR2 IN
V_STATUSCODE VARCHAR2 IN
V_TIMEUTC DATE IN
SQL> exec pkg_elmah$log_error.logerror
BEGIN pkg_elmah$log_error.logerror; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'LOGERROR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL>

Create Oracle procedure error - declare custom type

I'm attempting to create a procedure in Oracle Express Server (Application Express 2.1.0.00.39) using the web interface.
This is the SQL I'm running via the SQL Commands option in the web interface
CREATE OR REPLACE PROCEDURE my_procedure (listOfNumbers num_list,
v_value varchar2)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
UPDATE my_table
SET my_column = v_value
WHERE my_row_id IN (SELECT column_value
FROM TABLE(listOfNumbers));
COMMIT;
END;
UPDATE:
Changed SELECT column_value FROM TABLE to SELECT column_value FROM TABLE(listOfNumbers) but now I get the following error:
PLS-00201: identifier 'num_list' must
be declared
UPDATE 2:
Here is how I created my type:
CREATE OR REPLACE TYPE "num_list" as table of NUMBER(38,1)
/
Seems the error is being caused on the parameter declaration line:
(listOfNumbers num_list, v_value varchar2)
Below is the object details as displayed by the Oracle Database Express Edition web interface.
Try ...TABLE(CAST(listOfNumbers AS num_list)).
The SQL parser simply sees a bind placeholder in place of listOfNumbers, and since it's a custom type you need to tell it what type it is.
This will only work if num_list has been defined as a type in the schema, not just declared as a type in a PL/SQL block.
Your code works - providing the array type has been declared correctly (see below). As you are still having a problem I suspect that is where you are going wrong. But you need to post the code you are using to create the NUM_LIST type in order for us to correct it.
My test data:
SQL> select * from my_table
2 /
MY_COLUMN MY_ROW_ID
-------------------- ----------
APC 1
XYZ 2
JFK 3
SQL>
In order to use a type in a SQL statement we must create it as a SQL object:
SQL> create type num_list as table of number;
2 /
Type created.
SQL>
SQL>
SQL> create or replace procedure my_procedure
2 (listofnumbers num_list,
3 v_value varchar2)
4 is
5 begin
6
7 update my_table
8 set my_column = v_value
9 where my_row_id in (select column_value
10 from table(listofnumbers));
11
12 end;
13 /
Procedure created.
SQL>
Executing the procedure:
SQL> declare
2 n num_list := num_list(1,3);
3 begin
4 my_procedure (n , 'FOX IN SOCKS');
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
And lo!
SQL> select * from my_table
2 /
MY_COLUMN MY_ROW_ID
-------------------- ----------
FOX IN SOCKS 1
XYZ 2
FOX IN SOCKS 3
SQL>
Apparently I was creating the type with quotes around the name:
The following didn't work:
CREATE OR REPLACE TYPE "NUMBER_T" as table of NUMBER(38,1)
When I did it without the quotes and then created the procedure, it was able to recognize it.
The following did work:
CREATE OR REPLACE TYPE NUMBER_T as table of NUMBER(38,1)
I'm not sure why, but it worked.

Resources