oracle 包中的重载过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13217166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
overloading procedure in package
提问by Pravin Agre
Below is the HEADER for the Package TASK5
下面是包 TASK5 的 HEADER
CREATE OR REPLACE PACKAGE TASK5
AS
PROCEDURE TASK5APROCEDURE (
REG_NO IN NUMBER,
CERT_TITLE OUT VARCHAR2,
E_DATE OUT DATE,
C_MARKS OUT INTEGER);
PROCEDURE TASK5BPROCEDURE (
CERT_ID IN CHAR,
C_T OUT CHAR) ;
END TASK5;
The BODY for the PACKAGE TASK5
PACKAGE TASK5的BODY
CREATE OR REPLACE PACKAGE BODY TASK5
AS
PROCEDURE TASK5APROCEDURE (
REG_NO IN NUMBER,
CERT_TITLE OUT VARCHAR2,
E_DATE OUT DATE,
C_MARKS OUT INTEGER)
IS
BEGIN
SELECT
O.PCP_TITLE,
C.CERT_EXAMDATE,
C.CERT_MARKS
INTO
CERT_TITLE,
E_DATE,
C_MARKS
FROM
PROFCERTPROGRAM O
INNER JOIN
CERTIFICATION C
ON O.PCP_ID = C.PCP_ID
WHERE
C.S_REGNO LIKE REG_NO;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('NO DATA FOUND');
END TASK5APROCEDURE;
PROCEDURE TASK5BPROCEDURE (
CERT_ID IN CHAR, C_T OUT CHAR)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE ('COURSE NAMES: ');
FOR R IN (
SELECT O.C_TITLE C_T
FROM
COURSE O
INNER JOIN
CERTIFICATIONREQUIREMENT C
ON O.C_ID = C.C_ID
WHERE
C.PCP_ID LIKE '%'||CERT_ID||'%')
LOOP
DBMS_OUTPUT.PUT_LINE (R.C_T);
END LOOP ;
END TASK5BPROCEDURE;
END TASK5;
I wrote the package with two different procedure for 2 different input. But, I want to rewrite the header and body with overloading procedure, any suggestions?
我为 2 个不同的输入编写了两个不同的程序包。但是,我想用重载程序重写标题和正文,有什么建议吗?
回答by Ben
Overloading means creating multiple procedures or functions of the same name in a package, which take different numbers of arguments and / or where the arguments have different datatypes. This enables you to call a procedure and have different things happen depending on the arguments given.
重载意味着在一个包中创建多个同名的过程或函数,它们采用不同数量的参数和/或参数具有不同的数据类型。这使您可以调用过程并根据给定的参数发生不同的事情。
The answer to your question, therefore, is that simple. Rename TASK5BPROCEDURE
to TASK5APROCEDURE
in both the package specification and the package body. Alternatively, rename them both to something different. As an example your specification might look like this afterwards:
因此,您的问题的答案就是这么简单。在包规范和包正文中重命名TASK5BPROCEDURE
为TASK5APROCEDURE
。或者,将它们重命名为不同的名称。例如,您的规范之后可能如下所示:
create or replace package task5 as
procedure task5procedure (
, reg_no in number
, cert_title out varchar2
, e_date out date
, c_marks out integer);
procedure task5procedure (
, cert_id in char
, c_t out char);
end task5;
On a little side note using dbms_output.put_line
in a caught exception isn't really best practice. If you're going to catch an exception you should do something with it.
顺便提一下dbms_output.put_line
,在捕获的异常中使用并不是真正的最佳实践。如果您要捕获异常,则应该对其进行处理。
As APC notes in the comment it would be normal to overload a procedure when you are doing highly related things. For example if you're sending an e-mail using a procedure and you're passing the e-mail addresses either as a string or as an array. You don't seem to be doing the same thing in your procedures here and may want to reconsider the necessity of doing this.
正如 APC 在评论中指出的那样,当您做高度相关的事情时,程序过载是正常的。例如,如果您使用过程发送电子邮件,并且将电子邮件地址作为字符串或数组传递。您在这里的程序中似乎没有做同样的事情,可能需要重新考虑这样做的必要性。