Unable to find procedure in DBA_PROCEDURES view - oracle

I have created a procedure :
create or replace procedure gg as
begin
insert into book values ('prashant','prashant','prashant');
commit;
end;
/
Procedure has been created successfully.Now i want to check the package name for the corresponding procedure but i am not able to do so.
I am using the below query :
> SELECT *
FROM SYS.DBA_PROCEDURES
WHERE procedure_name ='gg';
Its giving 0 rows selected.Please help.

NOTE Please look at the UPDATE section for correct answer.
The procedure name cannot be in lower case in the DBA_PROCEDURES view. Use upper case, or apply UPPER function.
SELECT *
FROM SYS.DBA_PROCEDURES
WHERE procedure_name ='GG';
UPDATE
The only case when you could have the name in lower case is if you enclose it within double-quotation marks while compiling.
For example,
SQL> CREATE OR REPLACE
2 PROCEDURE "p"
3 AS
4 BEGIN
5 NULL;
6 END;
7 /
Procedure created.
SQL> SELECT object_name, procedure_name, object_type FROM user_procedures where procedure_name='p';
no rows selected
SQL>
But still the above view will not return any result for PROCEDURE_NAME.
Reason
PROCEDURE_NAME column will only have the procedure name for the procedures which are part of a PACKAGE. For STAND ALONE PROCEDURES you need to use OBJECT_NAME.
SQL> -- stand alone procedure in lower case
SQL> CREATE OR REPLACE
2 PROCEDURE "p"
3 AS
4 BEGIN
5 NULL;
6 END;
7 /
Procedure created.
SQL>
SQL> -- package
SQL> CREATE OR REPLACE
2 PACKAGE test_p
3 IS
4 PROCEDURE p;
5 END test_p;
6 /
Package created.
SQL>
SQL> -- package body with a procedure
SQL> CREATE OR REPLACE
2 PACKAGE BODY test_p
3 IS
4 PROCEDURE p
5 IS
6 BEGIN
7 NULL;
8 END;
9 END test_p;
10 /
Package body created.
SQL>
SQL> SELECT object_name, procedure_name, object_type FROM user_procedures;
OBJECT_NAME PROCEDURE_NAME OBJECT_TYPE
--------------- --------------- ---------------
TEST_P P PACKAGE
p PROCEDURE
TEST_P PACKAGE
SQL>
So, as you can see, the procedure_name is only having the package's procedure, however the stand-alone procedure is only listed under object_name.

The query I use is:
SELECT * FROM User_Procedures WHERE NVL(Procedure_Name,Object_Name) = 'PROCNAME';

Related

How to run select statement after stored procedure run ORACLE

I am new to Oracle, trying to run select statement after my stored procedure and failing. Here I am trying to return rows or any dummy data after my store procedure is executed, because my end process needs a return data for sure to make it consider as successful run. Is there any way I can trick it here is what I tried and failed.
EX:
BEGIN
note.name;
END;
/
SELECT * from hello.table where rownum=1;
Error:
ERROR [HY000] [Microsoft][ODBC Oracle Wire Protocol driver][Oracle]ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "/"
Activity ID: xxxxxx-xxx-xxx-xxx-xxxxxx
Show return a single row after completion of stored procedure
Code you posted looks OK. Demo:
SQL> create or replace procedure p_test as
2 begin
3 null;
4 end;
5 /
Procedure created.
SQL> begin
2 p_test;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> select * from dept where rownum = 1;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
Therefore, what exactly is your problem? What result do you expect?
If "at once" (as you commented) means that everything should be part of a PL/SQL block, then you'd declare a variable which is capable of holding the whole row, and then just select into it. rownum = 1 condition prevents too_many_rows error.
SQL> set serveroutput on
SQL>
SQL> declare
2 l_dept dept%rowtype;
3 begin
4 p_test;
5 select * into l_dept from dept where rownum = 1;
6 dbms_output.put_line(l_dept.deptno ||', '|| l_dept.dname ||', '|| l_dept.loc);
7 end;
8 /
10, ACCOUNTING, NEW YORK
PL/SQL procedure successfully completed.
SQL>

Oracle: create stored procedure to insert results from another stored procedure into a table

I have an Oracle stored procedure spFinTest that I use in an SSRS report.
I wish to create another stored procedure spFinTestInsert that takes the output of spFinTest and inserts it into a table sbceybudget_financial_year.
I can create an all in one stored procedure that inserts the data into the destination table, but what I am looking to achieve is to just have the one stored procedure that can have code updates for data extracts and not to have to update a second separate insert stored procedure that has the same code.
So, update in one place only and reuse the same stored procedure.
The following is a simplified version of the main stored procedure:
create or replace procedure spFINTEST
(s1 OUT SYS_REFCURSOR)
AS
BEGIN
OPEN s1 FOR
SELECT
FIN_YR + 1 AS FIN_YR,
SCHOOL_YEAR_WEEKS
FROM
sbceybudget_financial_year
WHERE
fin_yr = 2021
;
END spFINTEST;
This stored procedure only has an "out" variable.
The final intention is once I can do this, then I will call spFinTestInsert from an "Execute SQL Task" in an SSIS package.
I'm a bit stumped as to how I create this second stored procedure that calls the first and inserts the results into a named table, so if anyone can help I would be most grateful.
I would do this in a package: it allows you to declare cursor type easily to make it more clear.
Test table:
create table sbceybudget_financial_year(
fin_yr int,
SCHOOL_YEAR_WEEKS int
)
/
Package specification:
create or replace package pkg_spFIN as
type spFIN_RowType is record(
FIN_YR sbceybudget_financial_year.FIN_YR%type,
SCHOOL_YEAR_WEEKS sbceybudget_financial_year.SCHOOL_YEAR_WEEKS%type
);
type spFIN_CurType IS REF CURSOR RETURN spFIN_RowType;
type spFIN_tab is table of spFIN_RowType;
procedure spFINTEST (s1 OUT SYS_REFCURSOR);
procedure spFinTestInsert;
end pkg_spFIN;
/
Package body:
create or replace package body pkg_spFIN as
function get_cursor(n int) return spFIN_CurType is
c spFIN_CurType;
begin
open c for
SELECT
t.FIN_YR + 1 AS FIN_YR,
t.SCHOOL_YEAR_WEEKS
FROM
sbceybudget_financial_year t
WHERE
t.fin_yr = n;
return c;
end;
procedure spFINTEST (s1 OUT SYS_REFCURSOR)
is
begin
s1:=get_cursor(2021);
end spFINTEST;
procedure spFinTestInsert
is
cur spFIN_CurType;
tab spFIN_tab;
begin
pkg_spFIN.spFINTEST(cur);
loop
fetch cur bulk collect into tab limit 100;
exit when tab.count()=0;
for i in 1..tab.count loop
dbms_output.put_line(tab(i).FIN_YR);
dbms_output.put_line(tab(i).SCHOOL_YEAR_WEEKS);
-- or insert:
-- insert into sbceybudget_financial_year(fin_yr, SCHOOL_YEAR_WEEKS)
-- values(tab(i).FIN_YR, SCHOOL_YEAR_WEEKS)
-- you can change it to FORALL insert
end loop;
end loop;
end spFinTestInsert;
end pkg_spFIN;
/
Test data:
begin
insert into sbceybudget_financial_year(fin_yr, SCHOOL_YEAR_WEEKS) values(2019,19);
insert into sbceybudget_financial_year(fin_yr, SCHOOL_YEAR_WEEKS) values(2020,20);
insert into sbceybudget_financial_year(fin_yr, SCHOOL_YEAR_WEEKS) values(2021,21);
commit;
end;
/
And finally test call:
call pkg_spFIN.spFinTestInsert();
Full example on DBFiddle: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=c31a5714e5db74eaa3a83fae03964349
I don't have your tables so I'll use Scott's DEPT for illustration.
This is the "target" table; values fetched by ref cursor will be inserted into it:
SQL> create table test_dept as select deptno, dname from dept where 1 = 2;
Table created.
This is data I expect:
SQL> select deptno, dname from dept where deptno <= 20;
DEPTNO DNAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
This is your current procedure:
SQL> create or replace procedure spfintest (s1 out sys_refcursor)
2 as
3 begin
4 open s1 for select deptno, dname from dept where deptno <= 20;
5 end spfintest;
6 /
Procedure created.
This is a procedure which calls spfintest and inserts values into test_dept:
SQL> create or replace procedure spfitestinsert as
2 rc sys_refcursor;
3 --
4 l_deptno dept.deptno%type;
5 l_dname dept.dname%type;
6 begin
7 spfintest(rc);
8 loop
9 fetch rc into l_deptno, l_dname;
10 exit when rc%notfound;
11
12 insert into test_dept (deptno, dname)
13 values (l_deptno, l_dname);
14 end loop;
15 end;
16 /
Procedure created.
Testing:
SQL> exec spfitestinsert;
PL/SQL procedure successfully completed.
SQL> select * from test_dept;
DEPTNO DNAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
SQL>
Everything is here, so I guess it works.

Single quote at then end is missing in create view in procedure

I have a stored procedure where a string parameter need to be passed to a create a view, I am facing difficulty to enclose the string in single quotes
EXECUTE IMMEDIATE
'CREATE VIEW view_Exec_Data as
Select * from Employees
where exec_id='''' ||To_NChar(EID)||''''; --EID is input parameter and value will be 9DE4D0106D1F390EE0
Above query is generated as
EXECUTE IMMEDIATE
'CREATE VIEW view_Exec_Data as
Select * from Employees
where exec_id='9DE4D0106D1F390EE0;
Single quote at the end is missing, not sure where I am doing wrong.
We lack some info; for example, I wonder why you used TO_NCHAR ... do you really need it?
Here's an example which presumes that employees table looks like this:
SQL> create table employees (exec_id varchar2(30), name varchar2(30));
Table created.
SQL> insert into employees values ('9DE4D0106D1F390EE0', 'Littlefoot');
1 row created.
SQL> select * From employees;
EXEC_ID NAME
------------------------------ ------------------------------
9DE4D0106D1F390EE0 Littlefoot
A procedure which creates a view. I'd suggest NOT to do that. Do you really really want to have zillion views, one per each EXEC_ID someone uses as a parameter? What's the purpose of doing that? Why don't you simply
select * from employees where exec_id = :par_eid;
Anyway, here you go: in order to make that many single quotes simpler, I used q-quoting mechanism.
SQL> create or replace procedure p_crv (par_eid in employees.exec_id%type)
2 is
3 l_str varchar2(200);
4 begin
5 l_str := q'[CREATE or replace VIEW view_Exec_Data as
6 Select * from Employees
7 where exec_id= to_nchar(']' || par_eid || q'[')]';
8
9 -- when using dynamic SQL, **ALWAYS** check whether command is properly written
10 dbms_output.put_line(l_str);
11
12 -- if it looks OK, then execute it
13 execute immediate l_str;
14 end;
15 /
Procedure created.
SQL> set serveroutput on
SQL> exec p_crv('9DE4D0106D1F390EE0');
CREATE or replace VIEW view_Exec_Data as
Select * from Employees
where exec_id= to_nchar('9DE4D0106D1F390EE0')
PL/SQL procedure successfully completed.
SQL> select * From view_exec_data;
EXEC_ID NAME
------------------------------ ------------------------------
9DE4D0106D1F390EE0 Littlefoot
SQL>
If you don't need to_nchar, it gets somewhat simpler:
SQL> create or replace procedure p_crv (par_eid in employees.exec_id%type)
2 is
3 l_str varchar2(200);
4 begin
5 l_str := q'[CREATE or replace VIEW view_Exec_Data as
6 Select * from Employees
7 where exec_id= ']' || par_eid || q'[']';
8
9 -- when using dynamic SQL, **ALWAYS** check whether command is properly written
10 dbms_output.put_line(l_str);
11
12 -- if it looks OK, then execute it
13 execute immediate l_str;
14 end;
15 /
Procedure created.
SQL> exec p_crv('9DE4D0106D1F390EE0');
CREATE or replace VIEW view_Exec_Data as
Select * from Employees
where exec_id= '9DE4D0106D1F390EE0'
PL/SQL procedure successfully completed.
SQL> select * From view_exec_data;
EXEC_ID NAME
------------------------------ ------------------------------
9DE4D0106D1F390EE0 Littlefoot
SQL>
You can use:
EXECUTE IMMEDIATE
'CREATE VIEW view_Exec_Data as
Select * from Employees
where exec_id=To_NChar(''' || EID ||''')';
-- ^ 3 (') ^ 3 + 1 (')
Update
Working for me. See this:
SQL> set serverout on
SQL> declare
2 EID varchar2(100) := '9DE4D0106D1F390EE0';
3 begin
4 DBMS_OUTPUT.PUT_LINE('CREATE VIEW view_Exec_Data as
5 Select * from Employees
6 where exec_id=To_NChar(''' || EID ||''')');
7 end;
8 /
CREATE VIEW view_Exec_Data as
Select * from Employees
where
exec_id=To_NChar('9DE4D0106D1F390EE0')
PL/SQL procedure successfully completed.
SQL>

What is the difference between a "procedure name IS' and a "Procedure name AS" in Oracle?

PROCEDURE sample (p_num1 IN TABLE%TYPE,
p_num2 OUT NUMBER) IS
OR
PROCEDURE sample (p_num1 IN TABLE%TYPE,
p_num2 OUT NUMBER) AS
The difference is only one letter: I vs. A. You can use both, there won't be any other difference. The effect of executing both is the same except for the same minor difference stored in the data dictionary tables [CDB|DBA|ALL|USER]_SOURCE.
SQL> create or replace procedure p_test is begin null; end;
2 /
Procedure created.
SQL> create or replace procedure p_test as begin null; end;
2 /
Procedure created.
SQL>
Note that it doesn't work for e.g. views:
SQL> create or replace view v_emp as select * from emp;
View created.
SQL> create or replace view v_emp is select * from emp;
create or replace view v_emp is select * from emp
*
ERROR at line 1:
ORA-00905: missing keyword

call pl/sql from another one in oracle database problem

i have 2 stored procedures 1 is called A with the following impl
PROCEDURE A(p_id IN NUMBER, lic_cat_2 OUT varchar2,lic_cat_1 OUT varchar2,traffic_code OUT varchar2,lic_type OUT varchar2,emp_num OUT varchar2)
// Some LOGIC
end A ;
and PROCEDURE B which is a wrapper to proc A but i need to get other value with a query
PROCEDURE B(ph_id IN NUMBER, lic_cat_2 OUT varchar2,lic_cat_1 OUT varchar2,traffic_code OUT varchar2,lic_type OUT varchar2,emp_num OUT varchar2)
declare number phone_id
begin
select into phone_id parent_id from per_phones where phone_id= p_id
exec A(phone_id,lic_cat_2 OUT varchar2,lic_cat_1 OUT varchar2,traffic_code OUT varchar2,lic_type OUT varchar2,emp_num OUT varchar2);
END B;
but it gives me PLS-00103: Encountered the symbol “CREATE”
You're calling the A procedure in a wrong manner:
omit EXEC, it is a SQL*Plus command
omit parameters' description (IN/OUT, datatype) - pass only values
omit DECLARE; you need it in triggers or anonymous PL/SQL blocks, but not in stored procedures
by the way, variable name comes first, datatype next (for phone_id)
I'd suggest you to prefix parameters and variables with p_ (or par_) and l_ respectively (or any other prefix you want) to distinguish them from column names. Otherwise, it is easy to get confused.
also, use table aliases in your queries for the same reason
So:
CREATE OR REPLACE PROCEDURE B (p_ph_id IN NUMBER,
p_lic_cat_2 OUT VARCHAR2,
p_lic_cat_1 OUT VARCHAR2,
p_traffic_code OUT VARCHAR2,
p_lic_type OUT VARCHAR2,
p_emp_num OUT VARCHAR2)
IS
l_phone_id NUMBER;
BEGIN
SELECT p.parent_id
INTO l_phone_id
FROM per_phones p
WHERE p.phone_id = p_ph_id;
A (l_phone_id,
p_lic_cat_2,
p_lic_cat_1,
p_traffic_code,
p_lic_type,
p_emp_num);
END B;
As I don't have your tables, for example (to show how to do it) I used Scott's sample schema:
SQL> create or replace procedure a (par_deptno in number, par_dname out varchar2)
2 is
3 begin
4 select dname into par_dname from dept where deptno = par_deptno;
5 end;
6 /
Procedure created.
SQL>
SQL> create or replace procedure b (par_empno in number, par_dname out varchar2) is
2 l_deptno emp.deptno%type;
3 begin
4 select deptno into l_deptno from emp where empno = par_empno;
5
6 a(l_deptno, par_dname);
7 end;
8 /
Procedure created.
SQL>
SQL> set serveroutput on
SQL> declare
2 l_dname dept.dname%type;
3 begin
4 b (7654, l_dname);
5 dbms_output.put_line('Dname = ' || l_dname);
6 end;
7 /
Dname = SALES
PL/SQL procedure successfully completed.
SQL>

Resources