Oracle SQL:创建存储过程,将一个表作为输入。

问题描述 投票:0回答:1

我想创建一个存储过程,将一个表(例子中的T)作为输入,但是我不想将表名作为一个字符,而是做一些 "动态 "的事情,如

begin
    execute immediate ('delete from '||T||' where ...');
end;

因为我所处理的实际任务涉及到函数的几个输入和一个长长的查询,这个查询有很多的触点......要执行的字符串变得非常混乱和难以调试。我希望有一些简单的东西,如下面的(它可以工作)。

CREATE or replace PROCEDURE fun (x NUMBER) AS
   BEGIN
      DELETE FROM MyTable WHERE MyTable.var= fun.x;
   END;
/

然而,将表作为过程的输入(我需要这样做,因为否则,在实际任务中,我需要在过程中多次改变 "T")是行不通的。

CREATE or replace PROCEDURE fun (x NUMBER, T table) AS
   BEGIN
      DELETE FROM T WHERE T.var= fun.x;
   END;
/

Errors for PROCEDURE FUN
----------------------------
L:1 C:32       PLS-00103: Encountered the symbol "TABLE" when expecting one of the following:
                  in out <an identifier> <a double-quoted delimited-identifier>
                  ... long double ref char time timestamp interval date binary
                  national character nchar
               The symbol "<an identifier> was inserted before "TABLE" to continue.
1 statement failed.

有没有可能有接近我想要的东西?

sql oracle function procedure
1个回答
0
投票

我设计了一个不优雅的方法,但根据评论,这似乎是避免大段动态 "立即执行 "代码的唯一方法。

CREATE or replace PROCEDURE fun (T_in char, T_out char, x number) 
as
   BEGIN

    /* I create a temporary replica with a view */
   execute immediate 'create or replace view tempview as select * from '||T_in;

    /* DO STUFF, for example: */
   delete from tempview where ente_segn = fun.x;

    /* Save the results in a new table */
   execute immediate 'create or replace view '||T_out||'  as select * from tempview';

   END;
/
© www.soinside.com 2019 - 2024. All rights reserved.