使用输入参数从包中调用所需的过程

Calling the required procedure from a package using an input parameter

告诉我如何通过输入参数组织从包中调用所需的过程。我的代码示例,含义清楚,我将附上。我真的需要帮助来完成这项任务。在输入端,我们收到一行,根据这一行的文本,我们在包内使用必要的程序,删除、清除或复制 table。 在此先感谢您的帮助!

    create  table test1
(
    test1   varchar2(10),
    valtest number(5)
);
insert into test1 values('hello',0110);
insert into test1 values('bye',1010);



CREATE OR REPLACE PACKAGE pck as
TYPE refc IS REF CURSOR;
PROCEDURE p1
(   intake          varchar2, 
    rc    IN OUT refc
);
PROCEDURE p2 (
    intake          varchar2, 
    rc    IN  OUT refc
    
);
PROCEDURE p3 (
  intake          varchar2, 
    rc    IN  OUT refc
);
 
end;
create or replace package body pck as
PROCEDURE pr1 (
    intake          varchar2(10),
    rc    IN OUT mrc
)
IS
BEGIN
    OPEN rc FOR SELECT * FROM test1;
    IF rc IN ('delt')
    THEN DROP TABLE test1;
    dbms_output.put_line ('table droped!');
    END IF;
END;


PROCEDURE pr2 (
    intake          varchar2(10),
    rc    IN  OUT mrc
)
BEGIN
   OPEN rc FOR SELECT * FROM test1;
   IF rc IN ('trunct')
    THEN TRUNCATE TABLE test1;
    dbms_output.put_line ('table truncated!');
    END IF;
END;


PROCEDURE pr3 (
    intake          varchar2(10),
    rc    IN  OUT mrc
)
IS
BEGIN
  OPEN rc FOR SELECT * FROM test1;
   IF rc IN ('copy')
    THEN CREATE TABLE test2 AS SELECT * FROM test1;
    dbms_output.put_line ('table copyed!');
    END IF;
END;


end; 

对我来说,您的方法看起来有点太复杂了。为什么不是一个单一的过程?为什么要引用游标?您将需要动态 SQL(因为否则您无法执行 DDL)。请注意,如果硬编码 table test1 不存在,或者 - 而 copying - test2 已经存在,这将失败。您应该处理错误,并且很可能避免硬编码值。

SQL> CREATE OR REPLACE PACKAGE pck
  2  AS
  3     PROCEDURE p1 (intake VARCHAR2);
  4  END;
  5  /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY pck
  2  AS
  3     PROCEDURE p1 (intake VARCHAR2)
  4     IS
  5     BEGIN
  6        IF intake = 'delt'
  7        THEN
  8           EXECUTE IMMEDIATE 'drop table test1';
  9
 10           DBMS_OUTPUT.put_line ('Table dropped');
 11        ELSIF intake = 'trunct'
 12        THEN
 13           EXECUTE IMMEDIATE 'truncate table test1';
 14
 15           DBMS_OUTPUT.put_line ('Table truncated');
 16        ELSIF intake = 'copy'
 17        THEN
 18           EXECUTE IMMEDIATE 'create table test2 as select * from test1';
 19
 20           DBMS_OUTPUT.put_line ('Table copied');
 21        END IF;
 22     END;
 23  END;
 24  /

Package body created.

测试:

SQL> SELECT * FROM test1;

TEST1         VALTEST
---------- ----------
hello             110
bye              1010

SQL> BEGIN
  2     pck.p1 ('copy');
  3     pck.p1 ('trunct');
  4  END;
  5  /
Table copied
Table truncated

PL/SQL procedure successfully completed.

结果:

SQL> SELECT * FROM test1;

no rows selected

SQL> SELECT * FROM test2;

TEST1         VALTEST
---------- ----------
hello             110
bye              1010

SQL>