Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

when to use NOCOPY and when to avoid

hi guys

I am trying to understand when to use NOCOPY and when to avoid it. The documentation says

Requests that the compiler pass the corresponding actual parameter by reference instead of value (for the difference, see "Subprogram Parameter Passing Methods"). Each time the subprogram is invoked, the optimizer decides, silently, whether to obey or disregard NOCOPY.

i am writing a store procedure and my package specification is like this ::

create or replace PACKAGE STORE_DATA
AS

TYPE store_cur IS REF CURSOR;
 
PROCEDURE get_store_data(in_year NUMBER
                                            ,out_store_cursor OUT NOCOPY  store_cur);
END STORE_DATA

This procedure is called from a java program.
so now going back to the documentation'Requests that the compiler pass the corresponding actual parameter by reference instead of value '
it means the compiler passes the cursor 'out_store_cursor '  by reference instead of by value back to the java program, right?
so what is the author trying to say?

thx
Avatar of Sean Stuber
Sean Stuber

by reference means NOCOPY

This means any changes made to the variable within your procedure will be applied to the host variable of the calling procedure (in your case a java variable)

this will happen regardless of whether the procedure succeeds or fails.

simple example...
run this... you'll see procedure "one" does NOT update the variable, but "two" does


DECLARE
    v   VARCHAR2(3) := '---';

    PROCEDURE one(p IN OUT VARCHAR2)
    IS
    BEGIN
        p  := 'one';
        RAISE PROGRAM_ERROR;
    END;

    PROCEDURE two(p IN OUT NOCOPY VARCHAR2)
    IS
    BEGIN
        p  := 'two';
        RAISE PROGRAM_ERROR;
    END;
BEGIN
    DBMS_OUTPUT.put_line(v);

    BEGIN
        one(v);
    EXCEPTION
        WHEN OTHERS
        THEN
            NULL;
    END;

    DBMS_OUTPUT.put_line(v);

    BEGIN
        two(v);
    EXCEPTION
        WHEN OTHERS
        THEN
            NULL;
    END;

    DBMS_OUTPUT.put_line(v);
END;
Avatar of Jay Roy

ASKER

>>This means any changes made to the variable within your procedure will be applied to the host variable of the calling procedure (in your case a java variable)
this will happen regardless of whether the procedure succeeds or fails.

ok got it.
and if a variable is not defined as NOCOPY does it mean that
the variable within the  procedure will not be applied to the host variable if the procedure fails ?

thx
yes, run the sample I provided, procedure "one" is exactly that scenario
Avatar of Jay Roy

ASKER

ok ranthe example..clear now
one question:
so actually in procedure one() --the parameter which is not NOCOPY will never be applied to the host variable, right ?
as it is written,  yes, because the procedure has an embedded error  

if you take the RAISE line out, the procedure will complete successfully and value should be returned
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

thanks.
Avatar of Jay Roy

ASKER

one last question
what does RAISE PROGRAM_ERROR; mean ?

thx