使用临时表的Oracle存储过程

时间:2014-12-17 09:59:00

标签: sql oracle stored-procedures plsql temp-tables

主要思想:我想创建3个表(作为临时表)和一些选择,取1和2并进行比较。

结果插入3表。

我的程序如下:

create or replace PROCEDURE CHECK_PARAMS
AS 
TYPE row IS RECORD (id NUMBER,         
                    count_id NUMBER ); 
TYPE my_table_type IS TABLE OF row;

first my_table_type;
second my_table_type;
third my_table_type;    
BEGIN
  SELECT id, count_id bulk collect -- GOOD SELECT
  INTO first
  FROM 
    (SELECT test1.A_ID id, 
            COUNT(test1.A_ID) count_id
     FROM MY_OTHER_TABLE1 test1, 
          MY_OTHER_TABLE2 test2
     WHERE test1.A_ID = test2.A_ID
     GROUP BY test1.A_ID);

  SELECT id, count_id bulk collect -- GOOD SELECT
  INTO first
  FROM 
    (SELECT test1.A_ID id, 
            COUNT(test1.A_ID) count_id
     FROM MY_OTHER_TABLE3 test1, 
          MY_OTHER_TABLE4 test2
     WHERE test1.A_ID = test2.A_ID
     GROUP BY test1.A_ID);

  SELECT id, count_id bulk collect -- ORACLE CAN'T FIND MY TEMP TABLES
  INTO third
  FROM frist, second; -- **HERE I GOT ERROR: ORA-00942 table or view does not exist**

怎么了?

1 个答案:

答案 0 :(得分:1)

您必须在程序之外定义类型

create TYPE t_row as object  (id NUMBER,         
                    count_id NUMBER ); 

create TYPE my_table_type IS TABLE OF t_row;

而且你必须使用table

SELECT id, count_id bulk collect -- ORACLE CAN'T FIND MY TEMP TABLES
  INTO third
  FROM table(first), table(second);