create the pl/sql which returns the list of procedures in Oracle - sql

I am trying to list procedures and triggers in the database, but I have to use procedure or function to do so.
This query returns exactly what I need, but I need to get the same results using PL/SQL.
select *
from all_source
where type = 'PROCEDURE'
this query returns exactly what I need, but I have to get the same results using PL/SQL.
Thank you

Try this
declare record all_source%ROWTYPE
begin
select * into record from all_source where type = ?
end;

Try with the below procedure,
CREATE OR REPLACE
PROCEDURE get_proc_list(
i_type IN VARCHAR2,
o_result OUT sys_refcursor)
IS
l_type VARCHAR2(10) := UPPER(i_type);
BEGIN
OPEN o_result FOR
SELECT * FROM all_source
WHERE TYPE = l_type;
END;
To Execute the procedure,
var o_result refcursor;
EXECUTE get_proc_list('procedure',:o_result);
print o_result;

Related

How to make a function that takes code like "SELECT * FROM SOME_TABLE" as an input and returns a table as an output?

I want to create a function that takes some code as an input (e.g. Select * FROM SOME_TABLE) and returns the result of a query as an output.
I want to use it in procedures in order to return tables as a result.
It should look like this:
BEGIN
--some procedure code
CREATE TABLE SOME_TABLE as Select * FROM ...;
Select * FROM table(my_function('Select * FROM SOME_TABLE'));
END;
Important tips:
The resulting table can have multiple columns, from 1 to +inft
The resulting table can have multiple rows, from 1 to +inft
So the size of a table can be both very small or very large.
The input query can have several where, having, partition, and other Oracle constructions.
I want to have a table as an output, not DBMS_OUTPUT.
I can't install any modules/applications, or use other languages hints. However, I can manually create types, functions, procedures.
I tried to search in the net but could not find a solution that meets all my expectations. The best link I've found was this:
https://sqljana.wordpress.com/2017/01/22/oracle-return-select-statement-results-like-sql-server-sps-using-pipelined-functions/
DBMS_SQL.RETURN_RESULT works if your "code" is a select query
DECLARE
l_cur SYS_REFCURSOR;
l_query VARCHAR2(4000) := 'select * from SOME_TABLE';
BEGIN
OPEN l_cur for l_query;
DBMS_SQL.RETURN_RESULT(l_cur);
END;
/
you can create a function that has a string as parameter and return a cursor.
select statement you should pass as a string. in a function you can open a Cursor.
declare
v_sql varchar2(100) := 'select 1,2,3,4,5 from dual';
cur_ref SYS_REFCURSOR;
function get_data(in_sql in varchar2) return SYS_REFCURSOR
as
cur_ret SYS_REFCURSOR;
begin
OPEN cur_ret FOR in_sql;
return cur_ret;
end;
begin
:cur_ref := get_data(v_sql);
end ;
if your select statement is longer than 32K than you maybe should use a clob instead of varchar2 for your Parameter type. But you have to try that

Create a simple store procedure in oracle 12c

I am looking to create a simple store procedureto return a list of all user names in a table called dba_users.
The select I am using is:
SELECT username FROM dba_users
When I create a PROCEDURE with the following syntax it is created, but fails to execute:
CREATE OR REPLACE PROCEDURE user_list_display
IS
BEGIN
SELECT username FROM dba_users
END;
For this I get
ORA-00900: invalid SQL statement:
EXECUTE user_list_display;
you had better using an implicit cursor with dbms_output.put_line:
SQL> set serveroutput on;
SQL> CREATE OR REPLACE PROCEDURE user_list_display IS
BEGIN
FOR c in ( SELECT username FROM dba_users )
LOOP
dbms_output.put_line(c.username);
END LOOP;
END;
/
SQL> exec user_list_display;
In Oracle12c, You can use DBMS_SQL.RETURN_RESULT.
CREATE OR REPLACE PROCEDURE user_list_display
IS
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR SELECT username FROM dba_users;
DBMS_SQL.RETURN_RESULT ( v_cursor );
END;
/
Then execute it as
EXEC user_list_display;
In lower versions,from SQL* Plus (or execute as script in SQL Developer ), you may use a REFCURSOR OUT variable and PRINT to display the result.
CREATE OR REPLACE PROCEDURE user_list_display( output OUT SYS_REFCURSOR )
IS
BEGIN
OPEN output FOR SELECT username FROM dba_users;
END;
/
You may run these 3 lines whenever you want to see the output.
VARIABLE output REFCURSOR
EXEC user_list_display(:output)
PRINT output

executing a stored proc with output parameter

I am using SQLDEVELOPER TO make a stored procedure. this is my procedure
create or replace PROCEDURE SPP
(
inpt IN VARCHAR2,
opt OUT VARCHAR2
)
AS
BEGIN
SELECT U_ID
INTO opt
FROM TABLE_NAME
WHERE U_ID=inpt;
END;
so it should return the value in opt which i can then print or whatever.
i dont know how to execute it i tried running this script
var opt VARCHAR2;
exec SPP('test_user') := opt
but it doesnt work this is my first time doing stored procs and I'm really confused any help is appreciated
how do i make it run? i can make a stored proc run with input variables but with an output variable i mess up and I cant do it without using the SELECT (item) INTO format.
In order to call the procedure, you'd need to pass in a variable that the procedure can populate. Something like
DECLARE
l_output table_name.u_id%type;
BEGIN
spp( 'test_user', l_output );
END;
Now, that being said, declaring a stored procedure whose only purpose is to query the database and return a value is generally the wrong architectural choice. That's why functions exist.
CREATE OR REPLACE FUNCTION some_function( p_input IN table_name.u_id%type )
RETURN table_name.u_id%type
IS
l_uid table_name.u_id%type;
BEGIN
SELECT u_id
INTO l_uid
FROM table_name
WHERE u_id = p_input;
RETURN l_uid;
END;
which you can then call either in a PL/SQL block
DECLARE
l_output table_name.u_id%type;
BEGIN
l_output := some_function( 'test_user' );
END;
or in a SQL statement
SELECT some_function( 'test_user' )
FROM dual;

stored PL/sql function using select statement

Example i have a stored pl/sql
CREATE OR REPLACE PROCEDURE number( test in NUMBER )
........
// rest of code
isn't possible that i don't want run this execute
execute number(2);
i want run with
select * from number(2);
isn't possible to run stored pl/sql script with select statement to call the function instead of execute?
You can't execute a PROCEDURE from SQL; however, you can execute a FUNCTION from SQL.
First, redefine NUMBER as a FUNCTION:
CREATE OR REPLACE FUNCTION NUMBER(pTest IN NUMBER) RETURN NUMBER IS
someValue NUMBER := pTest * 100;
BEGIN
-- whatever
RETURN someValue;
END;
Then execute it from a SELECT statement as
SELECT NUMBER(2) FROM DUAL;
Share and enjoy.

trouble defining weakly defined ref cursor

I'm attempting to write a stored proc that takes in a number, n, and returns the first n results for a given query, exclusively locking those n rows. I'm a little new to SQL and I'm having a bit of difficulty matching data types correctly.
My package spec looks like this:
PACKAGE package IS
Type out_result_type is REF CURSOR;
PROCEDURE stored_proc
(in_n IN NUMBER DEFAULT 10,
out_list IN OUT out_result_type);
I then define the cursor in the procedure body, like so:
CURSOR OUT_RESULT_TYPE IS
SELECT a.id
FROM schema.table a
WHERE (some conditions) AND rownum <= in_n;
A bit later on I then try to extract the results of the cursor into the output variable:
OPEN OUT_RESULT_TYPE;
FETCH OUT_RESULT_TYPE INTO out_list; -- error on this line
CLOSE OUT_RESULT_TYPE;
But alas this code doesn't compile; oracle complains that out_list has already been defined with a conflicting data type. Any idea how I can resolve this issue? It's driving me crazy!
Thanks in advance.
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE tt_cur IS REF CURSOR;
PROCEDURE prc_cur (retval OUT tt_cur);
END;
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE prc_cur (retval OUT tt_cur)
AS
BEGIN
OPEN retval
FOR
SELECT *
FROM dual;
END;
END;
If you want to lock, use:
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE prc_cur (retval OUT tt_cur)
AS
BEGIN
OPEN retval
FOR
SELECT a.id
FROM schema.table a
WHERE (some conditions)
AND rownum <= in_n
ORDER BY
column
-- Never forget ORDER BY!
FOR UPDATE;
END;
END;
Two remarks:
A cursor doesn't lock.
You don't have to do Type out_result_type is REF CURSOR;, use default type sys_refcursor. See here: Oracle - How to have an out ref cursor parameter in a stored procedure?
Your out_list must be of wrong type. Consider (script run on 10.2.0.3):
CREATE TABLE t AS SELECT ROWNUM ID FROM all_objects WHERE ROWNUM <= 100;
CREATE OR REPLACE PACKAGE cursor_pck AS
TYPE out_result_type is REF CURSOR;
PROCEDURE stored_proc (p_in IN NUMBER DEFAULT 10,
p_out_list IN OUT out_result_type);
END cursor_pck;
/
If you want to select and lock the rows at the same time you would use the FOR UPDATE clause:
CREATE OR REPLACE PACKAGE BODY cursor_pck AS
PROCEDURE stored_proc (p_in IN NUMBER DEFAULT 10,
p_out_list IN OUT out_result_type) IS
BEGIN
OPEN p_out_list FOR SELECT a.id FROM t a WHERE ROWNUM <= p_in FOR UPDATE;
END stored_proc;
END cursor_pck;
/
With the following setup, you will call the procedure like this:
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
2 l_cursor cursor_pck.out_result_type;
3 l_id t.id%TYPE;
4 BEGIN
5 cursor_pck.stored_proc(3, l_cursor);
6 LOOP
7 FETCH l_cursor INTO l_id;
8 EXIT WHEN l_cursor%NOTFOUND;
9 dbms_output.put_line(l_id);
10 END LOOP;
11 END;
12 /
1
2
3
PL/SQL procedure successfully completed
This is not going to work the way it's written, because
out_list expects a cursor, not a cursor result.
The name out_result_type is already used for a type, so you can't redefine it to be a cursor in the same scope.
Oracle provides a pre-defined weak reference cursor: sys_refcursor. In usage it would look like:
CREATE OR REPLACE PACKAGE pkg_test
AS
PROCEDURE prc_cur (p_retval OUT sys_refcursor,
p_lookup IN VARCHAR2);
END pkg_test;
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE prc_cur(p_retval OUT sys_refcursor
p_lookup IN VARCHAR2)
IS
BEGIN
OPEN retval FOR SELECT a.value
FROM tblname a
WHERE a.id <= p_lookup;
END prc_cur;
END pkg_test;
This saves you the trouble of needing to declare a type. The sys_refcursor is a pointer to a result set from an open cursor. If you are familiar with Java, it's the same concept as the java.sql.ResultSet object which provides a way to get at the results of a query.