使用输入参数从包中调用所需的过程
Posted
技术标签:
【中文标题】使用输入参数从包中调用所需的过程【英文标题】:Calling the required procedure from a package using an input parameter 【发布时间】:2021-12-29 07:27:06 【问题描述】:告诉我如何通过输入参数从包中组织所需过程的调用。我的代码示例,含义清楚,我将附上。我真的需要帮助来完成这项任务。在输入时,我们收到一行,根据该行的文本,我们使用包内的必要程序,删除、清除或复制表。 提前感谢您的帮助!
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;
【问题讨论】:
【参考方案1】:对我来说,你的方法看起来有点太复杂了。为什么不是一个程序?为什么要引用光标?您将需要动态 SQL(因为否则您无法执行 DDL)。请注意,如果硬编码表 test1
不存在或 - 而 正在复制 - 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>
【讨论】:
你一如既往的优秀!感谢您的帮助,祝您一切顺利! 不客气;也祝你一切顺利!以上是关于使用输入参数从包中调用所需的过程的主要内容,如果未能解决你的问题,请参考以下文章
ODP.NET 是存储过程所需的 OracleParameter 字段吗?