Oracle 子程序参数模式,IN,OUT,IN OUT
IN :用于传入参数。
在调用过程的时候,实参传到该过程中。
在过程内部,形参只读且不能更改。
在过程执行完毕,返回调用环境时候,实参到的值也不会改变
--带IN参数的过程,赋值。 create or replace proceduce ModeIn( p_InParameter in number )as v_LocalVariable number:=0; begin DBMS_OUTPUT.PUT('Inside ModeIn:'); if(p_InParameter is null)then --in 传入参数,实参传到该过程中。 DBMS_OUTPUT.PUT_LINE('p_InParameter is null'); else DBMS_OUTPUT.PUT_LINE('p_InParameter='||p_InParameter); end if; v_LocalVariable:=p_InParameter; --和法赋值,只读 -- p_InParameter:=v_LocalVariable; --不能赋值,形参只读且不能更改 DBMS_OUTPUT.PUT('At end of ModeIn:'); if(p_InParameter is null) then DBMS_OUTPUT.PUT_LINE('p_InParameter is null'); else DBMS_OUTPUT.PUT_LINE('p_InParameter='||p_InParameter); end if; end ModeIn; ----调用ModeIn过程,观察IN参数的使用 declare v_in number :=1; begin DBMS_OUTPUT.PUT_LINE('Before calling ModeIn, v_in='||v_in); ModeIn(v_in); DBMS_OUTPUT.PUT_LINE('After calling ModeIn, v_in='||v_in); end; / Before calling ModeIn, v_in=1 --实参初始值为1 Inside ModeIn:p_InParameter=1 --形参接收传入的实参值为1 At end of ModeIn:p_InParameter=1 --形参只读且不能更改,形参值为1 After calling ModeIn, v_in=1 --返回调用环境时候,实参到的值也不会改变,实参值为1
OUT参数:用于返回值。
在调用过程时候,会忽略传入的形参的值,形参像未初始化的变量,值为null。
形参可以读取和写入。
在过程执行之后,返回调用环境(只有程序正常结束时),会将形参赋值给实参。
--带OUT参数的过程,赋值。 create or replace procedure ModeOut( p_OutParameter out number; )as v_LocalVariable:=0; begin DBMS_OUTPUT.PUT('Inside ModeOut:'); if(p_OutParameter is null)then -- 在调用过程时候,会忽略传入的形参的值,形参像未初始化的变量,值为null。 DBMS_OUTPUT.PUT_LINE('p_OutParameter is null'); else DBMS_OUTPUT.PUT_LINE('p_OutParameter='||p_OutParameter); end if; v_LocalVariable:=p_OutParameter; --和法赋值,形参可读 p_OutParameter:=7; --形参可以读取和写入 DBMS_OUTPUT.PUT('At end of ModeOut:'); if(p_OutParameter is null) then DBMS_OUTPUT.PUT_LINE('p_OutParameter is null'); else DBMS_OUTPUT.PUT_LINE('p_OutParameter='||p_OutParameter); end if; ----调用ModeOut过程,观察Out参数的使用 declare v_out number :=1; begin DBMS_OUTPUT.PUT_LINE('Before calling ModeOut, v_out='||v_out); ModeIn(v_out); DBMS_OUTPUT.PUT_LINE('After calling ModeOut, v_out='||v_out); end; / Before calling Modeout, v_out=1 --实参初始值为1 Inside ModeOut:p_OutParameter is null --在调用过程时候,会忽略传入的形参的值,形参像未初始化的变量,值为null。 At end of ModeOut:p_OutParameter=7 --形参可以读取和写入,值为改变后的7 After calling ModeOut, v_out=7 -- 返回调用环境时候,返回调用环境(只有程序正常结束时),会将形参赋值给实参。
IN OUT参数: 用于传入参数和返回值
在调用过程的时候,实参值会传递到过程中。
在过程执行之后,返回调用环境(只有程序正常结束时),会将形参复制给实参。
create or replace procedure ModeInOut( p_InOutParameter in out number; )as v_LocalVariable:=0; begin DBMS_OUTPUT.PUT('Inside ModeInOut:'); if(p_InOutParameter is null)then -- 在调用过程的时候,实参值会传递到过程中 DBMS_OUTPUT.PUT_LINE('p_InOutParameter is null'); else DBMS_OUTPUT.PUT_LINE('p_InOutParameter='||p_InOutParameter); end if; v_LocalVariable:=p_InOutParameter; --和法赋值,形参可读 p_InOutParameter:=8; --形参可以读取和写入 DBMS_OUTPUT.PUT('At end of ModeInOut:'); if(p_InOutParameter is null) then DBMS_OUTPUT.PUT_LINE('p_InOutParameter is null'); else DBMS_OUTPUT.PUT_LINE('p_InOutParameter='||p_InOutParameter); end if; ----调用ModeInOut过程,观察In Out参数的使用 declare v_InOut number :=1; begin DBMS_OUTPUT.PUT_LINE('Before calling ModeInOut, v_InOut='||v_InOut); ModeIn(v_InOut); DBMS_OUTPUT.PUT_LINE('After calling ModeInOut, v_InOut='||v_InOut); end; / Before calling ModeInout, v_InOut=1 --实参初始值为1 Inside ModeInOut:p_InOutParameter=1 -- 在调用过程的时候,实参值会传递到过程中,形参被赋值为1 At end of ModeInOut:p_InOutParameter=8 --形参可以读取和写入,值为改变后的8 After calling ModeInOut, v_InOut=8 -- 返回调用环境时候,返回调用环境(只有程序正常结束时),会将形参赋值给实参。