简体   繁体   中英

TEMP table in stored procedure oracle 11g

Is there any way to create a temp table with the stored procedure and use the same in the ref cursor in the same stored procedure. I wrote something like below, it's not working....

    CREATE OR REPLACE PROCEDURE USP_TEST(
    CUR_QUOTE OUT SYS_REFCURSOR) AS 
    BEGIN
    CREATE GLOBAL TEMPORARY TABLE users1 ON COMMIT PRESERVE ROWS
    AS
      SELECT 'rb@bot.com' FROM DUAL;
      OPEN CUR_QUOTE FOR
      SELECT DISTINCT CREATEDBY
      FROM QUOTE
      WHERE TRUNC(DATEOFENQUIRY)=TRUNC(SYSDATE-1) AND CREATEDBY = users1.EMAIL;
    END;

And delete the temp table at the end. Please suggest with some sample code... Keep coding:)

If you drop the table then the cursor is invalidated.

From 18c you can use private temporary tables :

create or replace procedure usp_test
    ( cur_quote out sys_refcursor )
as
begin
    execute immediate
        'create private temporary table ora$ptt_demo' ||chr(10)||
        'on commit drop definition as' ||chr(10)||
        'select sysdate -1 as dateofenquiry, ''rb@bot.com'' as createdby' ||chr(10)||
        'from   dual';

    open cur_quote for
        'select distinct createdby from ora$ptt_demo where trunc(dateofenquiry) = trunc(sysdate - 1)';
end;

Note that the table name must have the prefix defined by the PRIVATE_TEMP_TABLE_PREFIX parameter (default ORA$PTT_ ), and you must commit before calling the procedure a second time.

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