Skip to content
Advertisement

How to create a Procedure with specific SQL Select Output in Oracle?

I use an Oracle database and currently I have a problem by creating a Procdure which should create some SQL SELECT Outputs. The procedure was successfully completed but I get no output.

It is necessary the work with some temporary variables and another cursor? And it is possible to complement the SQL SELECT Statments with an ORDER BY clause at the end?

Advertisement

Answer

Perhaps a procedure which returns sys_refcursor would be a better idea. Because, if you dynamically (execute immediate) do some selects, those results have to be stored somewhere.

This is a simple example, but it might give you idea.

SQL> create or replace procedure p_test (par_rc out sys_refcursor) is
  2  begin
  3    open par_rc for
  4      select 'emp' table_name, count(*) from emp union all
  5      select 'dept'          , count(*) from dept union all
  6      select 'bonus'         , count(*) from bonus;
  7  end;
  8  /

Procedure created.

SQL> var rc refcursor
SQL> exec p_test(:rc);

PL/SQL procedure successfully completed.

SQL> print rc

TABLE_NAME   COUNT(*)
---------- ----------
emp                14
dept                4
bonus               0

SQL>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement