English 中文(简体)
甲骨文中临时表格的替代物
原标题:Alternatives for temporary tables in Oracle
  • 时间:2011-09-29 14:12:32
  •  标签:
  • oracle
  • plsql
  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 Employees.
  3. Now extract data from this Temp table, eg. Select * from #Temp where #Temp.Id = @id & so on.

How to do this in Oracle inside a stored procedure?

问题回答

什么是你试图解决的商业问题? 非常罕见的是,你需要在Oracle中使用临时表格。 为什么要 t你,只是

SELECT *
  FROM employees
 WHERE id = p_id_passed_in;

在其他数据库中,由于读者将编组作者,你往往制作临时表格,因此希望制作一份单独的数据副本,以避免阻碍任何其他会议。 然而,在Oracle,读者从来不阻挡作者,因此,一般没有必要从数据单上提取数据副本。

在其他数据库中,你创建临时表格是因为你不想干.。 但是,Oracle不允许 reads脏。 多变式改为一致性意味着,Oracle将始终显示在查询开始时(或者如果你确定可序列号的交易孤立程度,交易开始时)存在的数据。 因此,没有必要设立一个临时表格,以避免拖延。

如果你really希望用Oracle的临时表格,你不会动态地制作表格。 在你建立所储存的程序之前,你将设立一个全球临时表格。 所有届会都可以看到表格结构,但数据只见到列入表的会议。 您将把临时表格填入程序,然后询问表格。 类似于

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.

然而,正如我所说的那样,甲骨文中实际上想要使用临时表格是非常罕见的。

建立全球临时表格。

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

然后,你可以按照需要从桌上挑选,以完成您的程序。

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





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签