Oracle在存储过程中使用Temp表

时间:2018-03-23 18:04:22

标签: sql oracle plsql

我有一个使用临时表的查询,我想将其添加到存储过程。但是在编译时我得到"错误(10,1):PLS-00428:在这个SELECT语句中需要一个INTO子句"

例如

    WITH T1 as
    (
      SELECT ID, CREATED_DATE, LOOKUP_ID
      FROM TEST1
    ),T2 as
    (
      SELECT ID, CREATED_DATE, LOOKUP_ID
      FROM TEST2
    )
    SELECT * from T1
    minus
    SELECT * from T2

    RESULTS
    ID CREATED_D  LOOKUP_ID
---------- --------- ----------
    217322 11-DEC-16          1

作为存储过程添加:

create or replace PROCEDURE "TEST"
(
  T IN OUT SYS_REFCURSOR
) AS
BEGIN

  WITH T1 as
  (
    SELECT ID, CREATED_DATE, LOOKUP_ID
    FROM TEST1
  ), T2 as
  (
    SELECT ID, CREATED_DATE, LOOKUP_ID
    FROM TEST2
  )
  SELECT * from T1
  minus
  SELECT * from T2
end;

END;

错误(7,1):PLS-00428:此SELECT语句中需要INTO子句

我确实看到PLS-00428: an INTO clause is expected in this SELECT statement但是我正在使用INSERTS而我不想这样做。我想只使用临时表。

2 个答案:

答案 0 :(得分:0)

我们需要在PL / SQL变量中处理查询的输出。

create or replace PROCEDURE "TEST"
(
  T IN OUT SYS_REFCURSOR
) AS

cursor c_cur is
 WITH T1 as
  (
    SELECT ID, CREATED_DATE, LOOKUP_ID
    FROM TEST1
  ), T2 as
  (
    SELECT ID, CREATED_DATE, LOOKUP_ID
    FROM TEST2
  )
  SELECT * from T1
  minus
  SELECT * from T2;
BEGIN
for r_cur in c_cur
loop
dbms_output.put_line('ID: '||r_cur.id ||'CREATED_DATE: ' ||r_cur.CREATED_DATE ||' LOOKUP_ID: '||r_cur.lookup_id);
end loop;
end;

答案 1 :(得分:0)

你必须重写它;当程序使用refcursor时,我假设你想使用WITH因子子句作为它的来源。

Scott的DEPT表上的一个工作示例:

SQL> create or replace procedure p_test (t in out sys_refcursor)
  2  as
  3  begin
  4    open t for
  5       select t1.* from (select dname from dept) t1;
  6  end;
  7  /

Procedure created.

SQL>
SQL> var l_rc refcursor
SQL> exec p_test (:l_rc);

PL/SQL procedure successfully completed.

SQL> print :l_rc

DNAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS

SQL>

或者,你的代码以同样的方式重写(希望,我没有写错字):

create or replace procedure test (t in out sys_refcursor)
as
begin
  open t for
    select * from (select * from (select id, created_date, lookup_id
                                  from test1) 
                   minus
                   select * from (select id, created_date, lookup_id
                                  from test2)
                  );
end;