简体   繁体   English

Oracle中临时表的替代方案

[英]Alternatives for temporary tables in Oracle

  1. Create a temporary table inside a stored procedure, say '#Temp'. 在存储过程中创建一个临时表,比如'#Temp'。
  2. Insert values into 'Temp' table using a select statement, eg. 使用select语句将值插入'Temp'表中,例如。 Insert Into #Temp Select * from Employees. 插入#Temp从员工中选择*。
  3. Now extract data from this Temp table, eg. 现在从这个Temp表中提取数据,例如。 Select * from #Temp where #Temp.Id = @id & so on. 从#Temp中选择*,其中#Temp.Id = @id等等。

How to do this in Oracle inside a stored procedure ? 如何在Oracle内部存储过程中执行此操作?

What is the business problem you are trying to solve? 您要解决的业务问题是什么? It is exceptionally rare that you need to use temporary tables in Oracle. 您需要在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. 但是,在Oracle中,读者永远不会阻止编写者,因此通常无需保存单独的数据副本。

In other databases, you create temporary tables because you don't want to do dirty reads. 在其他数据库中,您可以创建临时表,因为您不想进行脏读。 Oracle, however, does not allow dirty reads. 但是,Oracle不允许脏读。 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). 多版本读取一致性意味着Oracle将始终显示查询启动时存在的数据(或者如果您设置了可序列化的事务隔离级别,则事务启动时)。 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. 如果您真的想在Oracle中使用临时表,则不会动态创建表。 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. 正如我所说,在Oracle中实际想要使用临时表是非常不寻常的。

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://www.oracle-base.com/articles/8i/TemporaryTables.php

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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