Alternatives for temporary tables in Oracle

前端 未结 2 1550
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-06 16:59
  1. Create a temporary table inside a stored procedure, say \'#Temp\'.
  2. Insert values into \'Temp\' table using a select statement, eg. Insert Into #Temp Select * from
相关标签:
2条回答
  • 2021-02-06 17:31

    What is the business problem you are trying to solve? It is exceptionally rare that you need to use temporary tables in Oracle. Why wouldn't you simply

    SELECT *
      FROM employees
     WHERE id = p_id_passed_in;
    

    In other databases, you often create temporary tables because readers block writers so you want to create a separate copy of the data in order to avoid blocking any other sessions. In Oracle, however, readers never block writers, so there is generally no need to save off a separate copy of the data.

    In other databases, you create temporary tables because you don't want to do dirty reads. Oracle, however, does not allow dirty reads. Multi-version read consistency means that Oracle will always show you the data as it existed when the query was started (or when the transaction started if you've set a transaction isolation level of serializable). So there is no need to create a temporary table to avoid dirty reads.

    If you really wanted to use temporary tables in Oracle, you would not create the table dynamically. You would create a global temporary table before you created the stored procedure. The table structure would be visible to all sessions but the data would be visible only to the session that inserted it. You would populate the temporary table in the procedure and then query the table. Something like

    CREATE GLOBAL TEMPORARY TABLE temp_emp (
      empno number,
      ename varchar2(10),
      job   varchar2(9),
      mgr   number,
      sal   number(7,2)
    )
    ON COMMIT PRESERVE ROWS;
    
    CREATE OR REPLACE PROCEDURE populate_temp_emp
    AS
    BEGIN
      INSERT INTO temp_emp( empno,
                            ename,
                            job,
                            mgr,
                            sal )
        SELECT empno, 
               ename,
               job,
               mgr,
               sal
          FROM emp;
    END;
    /
    
    SQL> begin
      2    populate_temp_emp;
      3  end;
      4  /
    
    PL/SQL procedure successfully completed.
    
    SQL> select *
      2    from temp_emp;
    
         EMPNO ENAME      JOB              MGR        SAL
    ---------- ---------- --------- ---------- ----------
          7623 PAV        Dev
          7369 smith      CLERK           7902        800
          7499 ALLEN      SALESMAN        7698       1600
          7521 WARD       SALESMAN        7698       1250
          7566 JONES      MANAGER         7839       2975
          7654 MARTIN     SALESMAN        7698       1250
          7698 BLAKE      MANAGER         7839       2850
          7782 CLARK      MANAGER         7839       2450
          7788 SCOTT      ANALYST         7566       3000
          7839 KING       PRESIDENT                  5000
          7844 TURNER     SALESMAN        7698       1500
          7876 ADAMS      CLERK           7788       1110
          7900 SM0        CLERK           7698        950
          7902 FORD       ANALYST         7566       3000
          7934 MILLER     CLERK           7782       1300
          1234 BAR
    
    16 rows selected.
    

    As I said, though, it would be very unusual in Oracle to actually want to use a temporary table.

    0 讨论(0)
  • 2021-02-06 17:52

    Create a global temporary table.

    CREATE GLOBAL TEMPORARY TABLE <your_table>
    ON COMMIT PRESERVE ROWS   # If needed.  Depends on your needs.
    AS SELECT <your_select_query>;
    

    You can then select from the table as needed for the duration of your procedure.

    http://www.oracle-base.com/articles/8i/TemporaryTables.php

    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:15826034070548

    0 讨论(0)
提交回复
热议问题