Oracle subroutine parameter mode, IN, OUT, NOCOPY

Source: Internet
Author: User
The parameter modes of Oracle subprograms include IN, OUT, NOCOPY, IN and OUT, and OUT and NOCOPY.

The parameter modes of Oracle subprograms include IN, OUT, NOCOPY, IN and OUT, and OUT and NOCOPY.

IN is mainly used to input parameters, which can be variables, constants, and expressions. The value cannot be changed within the subroutine.
The Code is as follows:
DECLARE
N NUMBER: = 10;
PROCEDURE do_something (
N1 in number) IS
BEGIN
Dbms_output.put_line (n1); -- prints 10
-- N1: = 20; -- illegal assignment.
END;
BEGIN
Do_something (n );
Do_something (20 );
END;

The OUT mode is used to return values. variables must be called. The initial values of variables are not passed to formal parameters, as shown in <1>.
The parameter value is copied to the real parameter only when the subroutine is returned (not when the form parameter is changed), as shown in <2>. If an exception occurs before the return, the actual parameter value is not changed.
The Code is as follows:
DECLARE
N NUMBER: = 10;
PROCEDURE do_something (
N1 out number) IS
BEGIN
Dbms_output.put_line ('before assign: '| n1); -- prints none <1>
N1: = 20;
Dbms_output.put_line ('before return: '| n); -- prints 10 <2>
END;
BEGIN
Do_something (n );
Dbms_output.put_line ('after return: '| n); -- prints 20
END;

NOCOPY mode is used to determine whether the OUT mode is implemented by reference (it is just a compiler suggestion and does not always work). By default, the OUT mode parameter is called by passing the value.
IN is mainly used to pass IN parameters. Although n2: = 20 is called, it takes effect only when it is returned, as shown IN <1>.
NOCOPY is a reference and will take effect immediately when the value is assigned, as shown in <2>. If an exception occurs before the return, the actual parameter value will also be changed.
Since the OUT parameter will copy the value returned by the subroutine to the actual parameter, the value of n after the call is 20, as shown in <3>.
The Code is as follows:
DECLARE
N NUMBER: = 10;
PROCEDURE do_something (
N1 in number,
N2 in out number,
N3 in out nocopy number) IS
BEGIN
N2: = 20;
Dbms_output.put_line (n1); -- prints 10 <1>
N3: = 30;
Dbms_output.put_line (n1); -- prints 30 <2>
END;
BEGIN
Do_something (n, n, n );
Dbms_output.put_line (n); -- prints 20 <3>
END;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.