###########################in############################
create or replace procedure pro_eight(p_one in varchar2, p_two out varchar2, p_three in out varchar2) is v_str1 varchar2(32) := ''; v_str2 varchar2(32) default ''; v_str3 varchar2(32); begin dbms_output.put_line('傳參:' || 'p_one:' || p_one|| ',p_two:' || p_two|| ',p_three:' || p_three); -- in類型變數不能接收賦值 p_one:='one changed'; --列印變數 dbms_output.put_line('變數:' || 'v_str1:' || v_str1|| ',v_str2:' || v_str2|| ',v_str3:' || v_str3);exception when others then dbms_output.put_line('exception!');end pro_eight;
###########################out############################
create or replace procedure pro_eight(p_one in varchar2, p_two out varchar2, p_three in out varchar2) is v_str1 varchar2(32) := ''; v_str2 varchar2(32) default ''; v_str3 varchar2(32); begin dbms_output.put_line('傳參:' || 'p_one:' || p_one|| ',p_two:' || p_two|| ',p_three:' || p_three); -- in類型變數不能接收賦值 --p_one:='one changed'; -- out類型只能接收賦值,不能給其他變數賦值。 -- 這樣賦值的語句雖然編譯和執行都不報錯,但是被賦值的變數就是為空白,根本沒接收到值。 v_str2 :=p_two; dbms_output.put_line('變數:' || ',v_str2:' || v_str2); --列印變數 dbms_output.put_line('變數:' || 'v_str1:' || v_str1|| ',v_str2:' || v_str2|| ',v_str3:' || v_str3);exception when others then dbms_output.put_line('exception!');end pro_eight;
###########################in out############################
create or replace procedure pro_eight(p_one in varchar2, p_two out varchar2, p_three in out varchar2) is v_str1 varchar2(32) := ''; v_str2 varchar2(32) default ''; v_str3 varchar2(32); begin dbms_output.put_line('傳參:' || 'p_one:' || p_one|| ',p_two:' || p_two|| ',p_three:' || p_three); -- in類型變數不能接收賦值 --p_one:='one changed'; -- out類型只能接收賦值,不能給其他變數賦值。 -- 這樣賦值的語句雖然編譯和執行都不報錯,但是被賦值的變數就是為空白,根本沒接收到值。 --v_str2 :=p_two; --dbms_output.put_line('變數:' || ',v_str2:' || v_str2); -- in out 類型可以接收賦值,也可以給其他變數賦值 v_str3:=p_three; v_str3:=v_str3||' changed'; dbms_output.put_line('變數:' || 'v_str3:' || v_str3); p_three:=v_str3||' again.'; dbms_output.put_line('參數:' || 'p_three:' || p_three); --列印變數 dbms_output.put_line('變數:' || 'v_str1:' || v_str1|| ',v_str2:' || v_str2|| ',v_str3:' || v_str3);exception when others then dbms_output.put_line('exception!');end pro_eight;
@@@@@@@@@@@@@@@@分割@@@@@@@@@@@@@@@@@@@
測試時in out傳參的區別
create or replace procedure pro_nine(p_one in varchar2) is v_str1 varchar2(32) := '';begin dbms_output.put_line('傳參:' || 'p_one:' || p_one); v_str1:=p_one;--變數的賦值 -- v_str2=>p_one;--報錯,=>只用於在調用時往參數列表傳參。 dbms_output.put_line('變數:' || 'v_str1:' || v_str1); exception when others then dbms_output.put_line('exception!');end pro_nine;
Sql windows 調用
call pro_nine('11');-- exec pro_nine('11');--報錯,這不是sql,是命令,應該在命令視窗執行
Command window 調用
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as uddrb@MYORCLSQL> exec pro_nine('11');PL/SQL procedure successfully completedSQL>
如果參數中有out和in out型別參數,就不能直接傳參,必須傳入變數。
create or replace procedure pro_nine(p_one in varchar2,p_two out varchar2) isbegin dbms_output.put_line('傳參:' || 'p_one:' || p_one); p_two:=p_one||' plus one.'; dbms_output.put_line('傳參:' || 'p_two:' || p_two);exception when others then dbms_output.put_line('exception!');end pro_nine;
Sql windows 調用
-- out參數必須綁定變數declare v_res varchar2(32);begin pro_nine('11',v_res); dbms_output.put_line('結果:' || 'v_res:' || v_res);end;
這裡正確執行了,但是想要看到列印語句需要把這些代碼放到test winddow執行。
Command window 調用
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as uddrb@MYORCLSQL> var v_str varchar2(32);SQL> exec :v_str :='222';PL/SQL procedure successfully completedv_str---------222SQL> exec pro_nine('111',p_two => :v_str);PL/SQL procedure successfully completedv_str---------111 plus one.SQL>
注意變數的聲明和賦值方式,以及傳參方式。
在command命令視窗模式下,變數的聲明var v_str varchar2(32);
執行預存程序語句需要使用命令exec。
使用變數時前面需要加一個冒號:v_str。
例如:
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as uddrb@MYORCLSQL> var v_str varchar2(32):='222';SQL> exec dbms_output.put_line('v_str:'||:v_str);PL/SQL procedure successfully completedv_str---------SQL>
上面的值沒列印出來,說明這樣賦值是不對的。應該用
exec :v_str :='222';
參數列表參數指定使用=>指定變數。
exec pro_nine('111',p_two => :v_str);
----------------------------------------
總結就不寫了,有些東西自己做一下實驗比看十本書都強。