简体   繁体   中英

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM