oracle儲存過程,集合物件處理
我們在進行pl/sql程式設計時打交道最多的就是儲存過程了。儲存過程的結構是非常的簡單的,我們在這裡除了學習儲存過程的基本結構外,還會學習編寫儲存過程時相關的一些實用的知識。如:遊標的處理,異常的處理,集合的選擇等等
1.儲存過程結構
1.1 第一個儲存過程
Java程式碼
1.create or replace procedure proc1(
2. p_para1 varchar2,
3. p_para2 out varchar2,
4. p_para3 in out varchar2
5.)as
6. v_name varchar2(20);
7.begin
8. v_name := '張三丰';
9. p_para3 := v_name;
10. dbms_output.put_line('p_para3:'||p_para3);
11.end;
create or replace procedure proc1(
p_para1 varchar2,
p_para2 out varchar2,
p_para3 in out varchar2
)as
v_name varchar2(20);
begin
v_name := '張三丰';
p_para3 := v_name;
dbms_output.put_line('p_para3:'||p_para3);
end;
上面就是一個最簡單的儲存過程。一個儲存過程大體分為這麼幾個部分:
建立語句:create or replace procedure 儲存過程名
如果沒有or replace語句,則僅僅是新建一個儲存過程。如果系統存在該儲存過程,則會報錯。Create or replace procedure 如果系統中沒有此儲存過程就新建一個,如果系統中有此儲存過程則把原來刪除掉,重新建立一個儲存過程。
儲存過程名定義:包括儲存過程名和引數列表。引數名和引數型別。引數名不能重複, 引數傳遞方式:IN, OUT, IN OUT
IN 表示輸入引數,按值傳遞方式。
OUT 表示輸出引數,可以理解為按引用傳遞方式。可以作為儲存過程的輸出結果,供外部呼叫者使用。
IN OUT 即可作輸入引數,也可作輸出引數。
引數的資料型別只需要指明型別名即可,不需要指定寬度。
引數的寬度由外部呼叫者決定。
過程可以有引數,也可以沒有引數
變數宣告塊:緊跟著的as (is )關鍵字,可以理解為pl/sql的declare關鍵字,用於宣告變數。
變數宣告塊用於宣告該儲存過程需要用到的變數,它的作用域為該儲存過程。另外這裡宣告的變數必須指定寬度。遵循PL/SQL的變數宣告規範。
過程語句塊:從begin 關鍵字開始為過程的語句塊。儲存過程的具體邏輯在這裡來實現。
異常處理塊:關鍵字為exception ,為處理語句產生的異常。該部分為可選
結束塊:由end關鍵字結果。
1.2 儲存過程的引數傳遞方式
儲存過程的引數傳遞有三種方式:IN,OUT,IN OUT .
IN 按值傳遞,並且它不允許在儲存過程中被重新賦值。如果儲存過程的引數沒有指定存引數傳遞型別,預設為IN
Java程式碼
1.create or replace procedure proc1(
2. p_para1 varchar2,
3. p_para2 out varchar2,
4. p_para3 in out varchar2
5.)as
6. v_name varchar2(20);
7.begin
8. p_para1 :='aaa';
9. p_para2 :='bbb';
10. v_name := '張三丰';
11. p_para3 := v_name;
12. dbms_output.put_line('p_para3:'||p_para3);
13. null;
14.end;
15.
16.Warning: Procedure created with compilation errors
17.
18.SQL> show error;
19.Errors for PROCEDURE LIFEMAN.PROC1:
20.
21.LINE/COL ERROR
22.-------- ----------------------------------------------------------------------
23.8/3 PLS-00363: expression 'P_PARA1' cannot be used as an assignment target
24.8/3 PL/SQL: Statement ignored
create or replace procedure proc1(
p_para1 varchar2,
p_para2 out varchar2,
p_para3 in out varchar2
)as
v_name varchar2(20);
begin
p_para1 :='aaa';
p_para2 :='bbb';
v_name := '張三丰';
p_para3 := v_name;
dbms_output.put_line('p_para3:'||p_para3);
null;
end;
Warning: Procedure created with compilation errors
SQL> show error;
Errors for PROCEDURE LIFEMAN.PROC1:
LINE/COL ERROR
-------- ----------------------------------------------------------------------
8/3 PLS-00363: expression 'P_PARA1' cannot be used as an assignment target
8/3 PL/SQL: Statement ignored這一點與其它高階語言都不同。它相當於java在引數前面加上final關鍵字。
OUT 引數:作為輸出引數,需要注意,當一個引數被指定為OUT型別時,就算在呼叫儲存過程之前對該引數進行了賦值,在儲存過程中該引數的值仍然是null.
Java程式碼
1.create or replace procedure proc1(
2. p_para1 varchar2,
3. p_para2 out varchar2,
4. p_para3 in out varchar2
5.)as
6. v_name varchar2(20);
7.begin
8. v_name := '張三丰';
9. p_para3 := v_name;
10. dbms_output.put_line('p_para1:'||p_para1);
11. dbms_output.put_line('p_para2:'||p_para2);
12. dbms_output.put_line('p_para3:'||p_para3);
13.end;
14.
15.SQL> var p1 varchar2(10);
16.SQL> var p2 varchar2(10);
17.SQL> var p3 varchar2(10);
18.SQL> exec :p1 :='aaaa';
19.SQL> exec :p2 :='bbbb';
20.SQL> exec :p3 :='cccc';
21.SQL> exec proc1(:p1,:p2,:p3);
22.p_para1:aaaa
23.p_para2:
24.p_para3:張三丰
25.SQL> exec dbms_output.put_line(:p2);
26.
27.
28.PL/SQL procedure successfully completed
29.p2
30.---------
create or replace procedure proc1(
p_para1 varchar2,
p_para2 out varchar2,
p_para3 in out varchar2
)as
v_name varchar2(20);
begin
v_name := '張三丰';
p_para3 := v_name;
dbms_output.put_line('p_para1:'||p_para1);
dbms_output.put_line('p_para2:'||p_para2);
dbms_output.put_line('p_para3:'||p_para3);
end;
SQL> var p1 varchar2(10);
SQL> var p2 varchar2(10);
SQL> var p3 varchar2(10);
SQL> exec :p1 :='aaaa';
SQL> exec :p2 :='bbbb';
SQL> exec :p3 :='cccc';
SQL> exec proc1(:p1,:p2,:p3);
p_para1:aaaa
p_para2:
p_para3:張三丰
SQL> exec dbms_output.put_line(:p2);
PL/SQL procedure successfully completed
p2
---------
INOUT 是真正的按引用傳遞引數。即可作為傳入引數也可以作為傳出引數。
Java程式碼
1.1.3 儲存過程引數寬度
2.create or replace procedure proc1(
3. p_para1 varchar2,
4. p_para2 out varchar2,
5. p_para3 in out varchar2
6.)as
7. v_name varchar2(2);
8.begin
9. v_name := p_para1;
10.end;
11.
12.SQL> var p1 varchar2(10);
13.SQL> var p2 varchar2(20);
14.SQL> var p3 varchar2(30);
15.SQL> exec :p1 :='aaaaaa';
16.SQL> exec proc1(:p1,:p2,:p3);
17.
18.
19.ORA-06502: PL/SQL: numeric or value error: character string buffer too small
20.ORA-06512: at "LIFEMAN.PROC1", line 8
21.ORA-06512: at line 1
1.3 儲存過程引數寬度
create or replace procedure proc1(
p_para1 varchar2,
p_para2 out varchar2,
p_para3 in out varchar2
)as
v_name varchar2(2);
begin
v_name := p_para1;
end;
SQL> var p1 varchar2(10);
SQL> var p2 varchar2(20);
SQL> var p3 varchar2(30);
SQL> exec :p1 :='aaaaaa';
SQL> exec proc1(:p1,:p2,:p3);
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "LIFEMAN.PROC1", line 8
ORA-06512: at line 1
首先,我們要明白,我們無法在儲存過程的定義中指定儲存引數的寬度,也就導致了我們無法在儲存過程中控制傳入變數的寬度。這個寬度是完全由外部傳入時決定的。
我們再來看看OUT型別的引數的寬度。
Java程式碼
1.create or replace procedure proc1(
2. p_para1 varchar2,
3. p_para2 out varchar2,
4. p_para3 in out varchar2
5.)as
6. v_name varchar2(2);
7.begin
8. p_para2 :='aaaaaaaaaaaaaaaaaaaa';
9.end;
10.SQL> var p1 varchar2(1);
11.SQL> var p2 varchar2(1);
12.SQL> var p3 varchar2(1);
13.SQL> exec :p2 :='a';
14.SQL> exec proc1(:p1,:p2,:p3);
create or replace procedure proc1(
p_para1 varchar2,
p_para2 out varchar2,
p_para3 in out varchar2
)as
v_name varchar2(2);
begin
p_para2 :='aaaaaaaaaaaaaaaaaaaa';
end;
SQL> var p1 varchar2(1);
SQL> var p2 varchar2(1);
SQL> var p3 varchar2(1);
SQL> exec :p2 :='a';
SQL> exec proc1(:p1,:p2,:p3);在該過程中,p_para2被賦予了20個字元a.
而在外部的呼叫過程中,p2這個引數僅僅被定義為varchar2(1).
而把p2作為引數呼叫這個過程,卻並沒有報錯。而且它的真實值就是20個a
Java程式碼
1.SQL> select dump(:p2) from dual;
2.DUMP(:P2)
3.---------------------------------------------------------------------------
4.Typ=1 Len=20: 97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97
5.p2
6.---------
7.aaaaaaaaaaaaaaaaaaaa
8.
9. 再來看看IN OUT引數的寬度
10.create or replace procedure proc1(
11. p_para1 varchar2,
12. p_para2 out varchar2,
13. p_para3 in out varchar2
14.)as
15. v_name varchar2(2);
16.begin
17. p_para3 :='aaaaaaaaaaaaaaaaaaaa';
18.end;
19.
20.SQL> var p1 varchar2(1);
21.SQL> var p2 varchar2(1);
22.SQL> var p3 varchar2(1);
23.SQL> exec proc1(:p1,:p2,:p3);
SQL> select dump(:p2) from dual;
DUMP(:P2)
---------------------------------------------------------------------------
Typ=1 Len=20: 97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97
p2
---------
aaaaaaaaaaaaaaaaaaaa
再來看看IN OUT引數的寬度
create or replace procedure proc1(
p_para1 varchar2,
p_para2 out varchar2,
p_para3 in out varchar2
)as
v_name varchar2(2);
begin
p_para3 :='aaaaaaaaaaaaaaaaaaaa';
end;
SQL> var p1 varchar2(1);
SQL> var p2 varchar2(1);
SQL> var p3 varchar2(1);
SQL> exec proc1(:p1,:p2,:p3);執行這個過程,仍然正確執行。
可見,對於IN引數,其寬度是由外部決定。
對於OUT 和IN OUT 引數,其寬度是由儲存過程內部決定。
因此,在寫儲存過程時,對引數的寬度進行說明是非常有必要的,最明智的方法就是引數的資料型別使用%type。這樣雙方就達成了一致。
1.3 引數的預設值
儲存過程的引數可以設定預設值
Java程式碼
1.create or replace procedure procdefault(p1 varchar2,
2. p2 varchar2 default 'mark')
3.as
4.begin
5. dbms_output.put_line(p2);
6.end;
7.
8.SQL> set serveroutput on;
9.SQL> exec procdefault('a');
create or replace procedure procdefault(p1 varchar2,
p2 varchar2 default 'mark')
as
begin
dbms_output.put_line(p2);
end;
SQL> set serveroutput on;
SQL> exec procdefault('a');mark
可以通過default 關鍵字為儲存過程的引數指定預設值。在對儲存過程呼叫時,就可以省略預設值。
需要注意的是:預設值僅僅支援IN傳輸型別的引數。OUT 和 IN OUT不能指定預設值
對於有預設值的引數不是排在最後的情況。
Java程式碼
1.create or replace procedure procdefault2(p1 varchar2 default 'remark',
2. p2 varchar2 )
3.as
4.begin
5. dbms_output.put_line(p1);
6.end;
create or replace procedure procdefault2(p1 varchar2 default 'remark',
p2 varchar2 )
as
begin
dbms_output.put_line(p1);
end;第一個引數有預設值,第二個引數沒有。如果我們想使用第一個引數的預設值時
exec procdefault2('aa');
這樣是會報錯的。
那怎麼變呢?可以指定引數的值。
Java程式碼
1.SQL> exec procdefault2(p2 =>'aa');
SQL> exec procdefault2(p2 =>'aa');
remark
這樣就OK了,指定aa傳給引數p2
2. 儲存過程內部塊
2.1 內部塊
我們知道了儲存過程的結構,語句塊由begin開始,以end結束。這些塊是可以巢狀。在語句塊中可以巢狀任何以下的塊。
Java程式碼
1.Declare … begin … exception … end;
2.create or replace procedure innerBlock(p1 varchar2)
3.as
4. o1 varchar2(10) := 'out1';
5.begin
6. dbms_output.put_line(o1);
7. declare
8. inner1 varchar2(20);
9. begin
10. inner1 :='inner1';
11. dbms_output.put_line(inner1);
12.
13. declare
14. inner2 varchar2(20);
15. begin
16. inner2 := 'inner2';
17. dbms_output.put_line(inner2);
18. end;
19. exception
20. when others then
21. null;
22. end;
23.end;
Declare … begin … exception … end;
create or replace procedure innerBlock(p1 varchar2)
as
o1 varchar2(10) := 'out1';
begin
dbms_output.put_line(o1);
declare
inner1 varchar2(20);
begin
inner1 :='inner1';
dbms_output.put_line(inner1);
declare
inner2 varchar2(20);
begin
inner2 := 'inner2';
dbms_output.put_line(inner2);
end;
exception
when others then
null;
end;
end;需要注意變數的作用域。
3.儲存過程的常用技巧
3.1 哪種集合?
我們在使用儲存過程的時候經常需要處理記錄集,也就是多條資料記錄。分為單列多行和多列多行,這些型別都可以稱為集合型別。我們在這裡進行比較這些集合型別,以便於在程式設計時做出正確的選擇。
索引表,也稱為pl/sql表,不能儲存於資料庫中,元素的個數沒有限制,下標可以為負值。
Java程式碼
1.type t_table is table of varchar2(20) index by binary_integer;
2. v_student t_table;
type t_table is table of varchar2(20) index by binary_integer;
v_student t_table;varchar2(20)表示存放元素的資料型別,binary_integer表示元素下標的資料型別。
巢狀表,索引表沒有 index by子句就是巢狀表,它可以存放於資料中,元素個數無限,下標從1開始,並且需要初始化
Java程式碼
1.type t_nestTable is table of varchar2(20);
2.v_class t_nestTable ;
type t_nestTable is table of varchar2(20);
v_class t_nestTable ;僅是這樣宣告是不能使用的,必須對巢狀表進行初始化,對巢狀表進行初始化可以使用它的建構函式
Java程式碼
1.v_class :=t_nestTable('a','b','c');
v_class :=t_nestTable('a','b','c');變長陣列,變長陣列與高階語言的陣列型別非常相似,下標以1開始,元素個數有限。
Java程式碼
1.type t_array is varray (20) of varchar2(20);
type t_array is varray (20) of varchar2(20);
varray(20)就定義了變長陣列的最大元素個數是20個
變長陣列與巢狀表一樣,也可以是資料表列的資料型別。
同時,變長陣列的使用也需要事先初始化。
型別 可儲存於資料庫 元素個數 是否需初始化 初始下標值
索引表 否 無限 不需
巢狀表 可 無限 需 1
可變陣列 可 有限(自定義) 需 1
由此可見,如果僅僅是在儲存過程中當作集合變數使用,索引表是最好的選擇。
3.2 選用何種遊標?
顯示遊標分為:普通遊標,引數化遊標和遊標變數三種。
下面以一個過程來進行說明
Java程式碼
1.create or replace procedure proccursor(p varchar2)
2.as
3.v_rownum number(10) := 1;
4.cursor c_postype is select pos_type from pos_type_tbl where rownum =1;
5.cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
6.cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
7.type t_postype is ref cursor ;
8.c_postype3 t_postype;
9.v_postype varchar2(20);
10.begin
11. open c_postype;
12. fetch c_postype into v_postype;
13. dbms_output.put_line(v_postype);
14. close c_postype;
15. open c_postype1;
16. fetch c_postype1 into v_postype;
17. dbms_output.put_line(v_postype);
18. close c_postype1;
19. open c_postype2(1);
20. fetch c_postype2 into v_postype;
21. dbms_output.put_line(v_postype);
22. close c_postype2;
23. open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
24. fetch c_postype3 into v_postype;
25. dbms_output.put_line(v_postype);
26. close c_postype3;
27.end;
create or replace procedure proccursor(p varchar2)
as
v_rownum number(10) := 1;
cursor c_postype is select pos_type from pos_type_tbl where rownum =1;
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
type t_postype is ref cursor ;
c_postype3 t_postype;
v_postype varchar2(20);
begin
open c_postype;
fetch c_postype into v_postype;
dbms_output.put_line(v_postype);
close c_postype;
open c_postype1;
fetch c_postype1 into v_postype;
dbms_output.put_line(v_postype);
close c_postype1;
open c_postype2(1);
fetch c_postype2 into v_postype;
dbms_output.put_line(v_postype);
close c_postype2;
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
fetch c_postype3 into v_postype;
dbms_output.put_line(v_postype);
close c_postype3;
end;
cursor c_postype is select pos_type from pos_type_tbl where rownum =1
這一句是定義了一個最普通的遊標,把整個查詢已經寫死,呼叫時不可以作任何改變。
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
這一句並沒有寫死,查詢引數由變數v_rownum來決定。需要注意的是v_rownum必須在這個遊標定義之前宣告。
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
這一條語句與第二條作用相似,都是可以為遊標實現動態的查詢。但是它進一步的縮小了引數的作用域範圍。但是可讀性降低了不少。
type t_postype is ref cursor ;
c_postype3 t_postype;
先定義了一個引用遊標型別,然後再聲明瞭一個遊標變數。
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
然後再用open for 來開啟一個查詢。需要注意的是它可以多次使用,用來開啟不同的查詢。
從動態性來說,遊標變數是最好用的,但是閱讀性也是最差的。
注意,遊標的定義只能用使關鍵字IS,它與AS不通用。
3.3 遊標迴圈最佳策略
我們在進行PL/SQL程式設計時,經常需要迴圈讀取結果集的資料。進行逐行處理,這個過程就需要對遊標進行迴圈。對遊標進行迴圈的方法有多種,我們在此一一分析。
Java程式碼
1.create or replace procedure proccycle(p varchar2)
2.as
3.cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;
4.v_postype varchar2(20);
5.v_description varchar2(50);
6.begin
7.open c_postype;
8. if c_postype%found then
9. dbms_output.put_line('found true');
10. elsif c_postype%found = false then
11. dbms_output.put_line('found false');
12. else
13. dbms_output.put_line('found null');
14. end if;
15. loop
16. fetch c_postype into v_postype,v_description ;
17. exit when c_postype%notfound;
18. dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
19. end loop;
20. close c_postype;
21.dbms_output.put_line('---loop end---');
22. open c_postype;
23. fetch c_postype into v_postype,v_description;
24. while c_postype%found loop
25. dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
26. fetch c_postype into v_postype,v_description ;
27. end loop;
28.
29. close c_postype;
30.dbms_output.put_line('---while end---');
31. for v_pos in c_postype loop
32. v_postype := v_pos.pos_type;
33. v_description := v_pos.description;
34. dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
35. end loop;
36. dbms_output.put_line('---for end---');
37.end;
create or replace procedure proccycle(p varchar2)
as
cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;
v_postype varchar2(20);
v_description varchar2(50);
begin
open c_postype;
if c_postype%found then
dbms_output.put_line('found true');
elsif c_postype%found = false then
dbms_output.put_line('found false');
else
dbms_output.put_line('found null');
end if;
loop
fetch c_postype into v_postype,v_description ;
exit when c_postype%notfound;
dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
end loop;
close c_postype;
dbms_output.put_line('---loop end---');
open c_postype;
fetch c_postype into v_postype,v_description;
while c_postype%found loop
dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
fetch c_postype into v_postype,v_description ;
end loop;
close c_postype;
dbms_output.put_line('---while end---');
for v_pos in c_postype loop
v_postype := v_pos.pos_type;
v_description := v_pos.description;
dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
end loop;
dbms_output.put_line('---for end---');
end;
使用遊標之前需要開打遊標,open cursor,迴圈完後再關閉遊標close cursor.
這是使用遊標應該慎記於心的法則。
上面的過程演示了遊標迴圈的三種方法。
在討論迴圈方法之前,我們先看看%found和%notfound這些遊標的屬性。
Java程式碼
1.open c_postype;
2. if c_postype%found then
3. dbms_output.put_line('found true');
4. elsif c_postype%found = false then
5. dbms_output.put_line('found false');
6. else
7. dbms_output.put_line('found null');
8. end if;
open c_postype;
if c_postype%found then
dbms_output.put_line('found true');
elsif c_postype%found = false then
dbms_output.put_line('found false');
else
dbms_output.put_line('found null');
end if;在開啟一個遊標之後,馬上檢查它的%found或%notfound屬性,它得到的結果即不是true也不是false.而是null.必須執行一條fetch語句後,這些屬性才有值。
第一種使用loop 迴圈
Java程式碼
1.loop
2. fetch c_postype into v_postype,v_description ;
3. exit when c_postype%notfound;
4. ……
5.end loop
loop
fetch c_postype into v_postype,v_description ;
exit when c_postype%notfound;
……
end loop這裡需要注意,exit when語句一定要緊跟在fetch之後。必避免多餘的資料處理。
處理邏輯需要跟在exit when之後。這一點需要多加小心。
迴圈結束後要記得關閉遊標。
第二種使用while迴圈。
Java程式碼
1. fetch c_postype into v_postype,v_description;
2.while c_postype%found loop
3. ……
4. fetch c_postype into v_postype,v_description ;
5.end loop;
fetch c_postype into v_postype,v_description;
while c_postype%found loop
……
fetch c_postype into v_postype,v_description ;
end loop;
我們知道了一個遊標開啟後,必須執行一次fetch語句,遊標的屬性才會起作用。所以使用while 迴圈時,就需要在迴圈之前進行一次fetch動作。
而且資料處理動作必須放在迴圈體內的fetch方法之前。迴圈體內的fetch方法要放在最後。否則就會多處理一次。這一點也要非常的小心。
總之,使用while來迴圈處理遊標是最複雜的方法。
第三種 for迴圈
Java程式碼
1.for v_pos in c_postype loop
2. v_postype := v_pos.pos_type;
3. v_description := v_pos.description;
4. …
5. end loop;
for v_pos in c_postype loop
v_postype := v_pos.pos_type;
v_description := v_pos.description;
…
end loop;可見for迴圈是比較簡單實用的方法。
首先,它會自動open和close遊標。解決了你忘記開啟或關閉遊標的煩惱。
其它,自動定義了一個記錄型別及宣告該型別的變數,並自動fetch資料到這個變數中。
我們需要注意v_pos 這個變數無需要在迴圈外進行宣告,無需要為其指定資料型別。
它應該是一個記錄型別,具體的結構是由遊標決定的。
這個變數的作用域僅僅是在迴圈體內。
把v_pos看作一個記錄變數就可以了,如果要獲得某一個值就像呼叫記錄一樣就可以了。
如v_pos.pos_type
由此可見,for迴圈是用來循環遊標的最好方法。高效,簡潔,安全。
但遺憾的是,常常見到的卻是第一種方法。所以從今之後得改變這個習慣了。
3.4 select into不可乎視的問題
我們知道在pl/sql中要想從資料表中向變數賦值,需要使用select into 子句。
但是它會帶動來一些問題,如果查詢沒有記錄時,會丟擲no_data_found異常。
如果有多條記錄時,會丟擲too_many_rows異常。
這個是比較糟糕的。一旦丟擲了異常,就會讓過程中斷。特別是no_data_found這種異常,沒有嚴重到要讓程式中斷的地步,可以完全交給由程式進行處理。
Java程式碼
1.create or replace procedure procexception(p varchar2)
2.as
3. v_postype varchar2(20);
4.begin
5. select pos_type into v_postype from pos_type_tbl where 1=0;
6. dbms_output.put_line(v_postype);
7.end;
8.
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
begin
select pos_type into v_postype from pos_type_tbl where 1=0;
dbms_output.put_line(v_postype);
end;
執行這個過程
Java程式碼
1.SQL> exec procexception('a');
2.報錯
3.ORA-01403: no data found
4.ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6
5.ORA-06512: at line 1
SQL> exec procexception('a');
報錯
ORA-01403: no data found
ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6
ORA-06512: at line 1
處理這個有三個辦法
1. 直接加上異常處理。
Java程式碼
1.create or replace procedure procexception(p varchar2)
2.as
3. v_postype varchar2(20);
4.
5.begin
6. select pos_type into v_postype from pos_type_tbl where 1=0;
7. dbms_output.put_line(v_postype);
8.exception
9. when no_data_found then
10. dbms_output.put_line('沒找到資料');
11.end;
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
begin
select pos_type into v_postype from pos_type_tbl where 1=0;
dbms_output.put_line(v_postype);
exception
when no_data_found then
dbms_output.put_line('沒找到資料');
end;這樣做換湯不換藥,程式仍然被中斷。可能這樣不是我們所想要的。
2. select into做為一個獨立的塊,在這個塊中進行異常處理
Java程式碼
1.create or replace procedure procexception(p varchar2)
2.as
3. v_postype varchar2(20);
4.
5.begin
6. begin
7. select pos_type into v_postype from pos_type_tbl where 1=0;
8. dbms_output.put_line(v_postype);
9. exception
10. when no_data_found then
11. v_postype := '';
12. end;
13. dbms_output.put_line(v_postype);
14.end;
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
begin
begin
select pos_type into v_postype from pos_type_tbl where 1=0;
dbms_output.put_line(v_postype);
exception
when no_data_found then
v_postype := '';
end;
dbms_output.put_line(v_postype);
end;這是一種比較好的處理方式了。不會因為這個異常而引起程式中斷。
3.使用遊標
Java程式碼
1.create or replace procedure procexception(p varchar2)
2.as
3. v_postype varchar2(20);
4. cursor c_postype is select pos_type from pos_type_tbl where 1=0;
5.begin
6. open c_postype;
7. fetch c_postype into v_postype;
8. close c_postype;
9. dbms_output.put_line(v_postype);
10.end;
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
cursor c_postype is select pos_type from pos_type_tbl where 1=0;
begin
open c_postype;
fetch c_postype into v_postype;
close c_postype;
dbms_output.put_line(v_postype);
end;這樣就完全的避免了no_data_found異常。完全交由程式設計師來進行控制了。
第二種情況是too_many_rows 異常的問題。
Too_many_rows 這個問題比起no_data_found要複雜一些。
給一個變數賦值時,但是查詢結果有多個記錄。
處理這種問題也有兩種情況:
1. 多條資料是可以接受的,也就是說從結果集中隨便取一個值就行。這種情況應該很極端了吧,如果出現這種情況,也說明了程式的嚴謹性存在問題。
2. 多條資料是不可以被接受的,在這種情況肯定是程式的邏輯出了問題,也說是說原來根本就不會想到它會產生多條記錄。
對於第一種情況,就必須採用遊標來處理,而對於第二種情況就必須使用內部塊來處理,重新丟擲異常。
多條資料可以接受,隨便取一條,這個跟no_data_found的處理方式一樣,使用遊標。
我這裡僅說第二種情況,不可接受多條資料,但是不要忘了處理no_data_found哦。這就不能使用遊標了,必須使用內部塊。
Java程式碼
1.create or replace procedure procexception2(p varchar2)
2.as
3. v_postype varchar2(20);
4.
5.begin
6. begin
7. select pos_type into v_postype from pos_type_tbl where rownum < 5;
8. exception
9. when no_data_found then
10. v_postype :=null;
11. when too_many_rows then
12. raise_application_error(-20000,'對v_postype賦值時,找到多條資料');
13. end;
14. dbms_output.put_line(v_postype);
15.end;
create or replace procedure procexception2(p varchar2)
as
v_postype varchar2(20);
begin
begin
select pos_type into v_postype from pos_type_tbl where rownum < 5;
exception
when no_data_found then
v_postype :=null;
when too_many_rows then
raise_application_error(-20000,'對v_postype賦值時,找到多條資料');
end;
dbms_output.put_line(v_postype);
end;需要注意的是一定要加上對no_data_found的處理,對出現多條記錄的情況則繼續丟擲異常,讓上一層來處理。
總之對於select into的語句需要注意這兩種情況了。需要妥當處理啊。
3.5 在儲存過程中返回結果集
我們使用儲存過程都是返回值都是單一的,有時我們需要從過程中返回一個集合。即多條資料。這有幾種解決方案。比較簡單的做法是寫臨時表,但是這種做法不靈活。而且維護麻煩。我們可以使用巢狀表來實現.沒有一個集合型別能夠與java的jdbc型別匹配。這就是物件與關係資料庫的阻抗吧。資料庫的物件並不能夠完全轉換為程式語言的物件,還必須使用關係資料庫的處理方式。
Java程式碼
1.create or replace package procpkg is
2. type refcursor is ref cursor;
3. procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor);
4.end procpkg;
5.
6.create or replace package body procpkg is
7. procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor)
8. is
9. v_posTypeList PosTypeTable;
10. begin
11. v_posTypeList :=PosTypeTable();--初始化巢狀表
12. v_posTypeList.extend;
13. v_posTypeList(1) := PosType('A001','客戶資料變更');
14. v_posTypeList.extend;
15. v_posTypeList(2) := PosType('A002','團體資料變更');
16. v_posTypeList.extend;
17. v_posTypeList(3) := PosType('A003','受益人變更');
18. v_posTypeList.extend;
19. v_posTypeList(4) := PosType('A004','續期交費方式變更');
20. open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable));
21. end;
22.end procpkg;
create or replace package procpkg is
type refcursor is ref cursor;
procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor);
end procpkg;
create or replace package body procpkg is
procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor)
is
v_posTypeList PosTypeTable;
begin
v_posTypeList :=PosTypeTable();--初始化巢狀表
v_posTypeList.extend;
v_posTypeList(1) := PosType('A001','客戶資料變更');
v_posTypeList.extend;
v_posTypeList(2) := PosType('A002','團體資料變更');
v_posTypeList.extend;
v_posTypeList(3) := PosType('A003','受益人變更');
v_posTypeList.extend;
v_posTypeList(4) := PosType('A004','續期交費方式變更');
open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable));
end;
end procpkg;
在包頭中定義了一個遊標變數,並把它作為儲存過程的引數型別。
在儲存過程中定義了一個巢狀表變數,對資料寫進巢狀表中,然後把巢狀表進行型別轉換為table,遊標變數從這個巢狀表中進行查詢。外部程式呼叫這個遊標。
所以這個過程需要定義兩個型別。
Java程式碼
1.create or replace type PosType as Object (
2. posType varchar2(20),
3. description varchar2(50)
4.);
create or replace type PosType as Object (
posType varchar2(20),
description varchar2(50)
);create or replace type PosTypeTable is table of PosType;
需要注意,這兩個型別不能定義在包頭中,必須單獨定義,這樣java層才能使用。
在外部通過pl/sql來呼叫這個過程非常簡單。
Java程式碼
1.set serveroutput on;
2.declare
3. type refcursor is ref cursor;
4. v_ref_postype refcursor;
5. v_postype varchar2(20);
6. v_desc varchar2(50);
7.begin
8. procpkg.procrefcursor('a',v_ref_postype);
9. loop
10. fetch v_ref_postype into v_postype,v_desc;
11. exit when v_ref_postype%notfound;
12. dbms_output.put_line('posType:'|| v_postype || ';description:' || v_desc);
13. end loop;
14.end;
set serveroutput on;
declare
type refcursor is ref cursor;
v_ref_postype refcursor;
v_postype varchar2(20);
v_desc varchar2(50);
begin
procpkg.procrefcursor('a',v_ref_postype);
loop
fetch v_ref_postype into v_postype,v_desc;
exit when v_ref_postype%notfound;
dbms_output.put_line('posType:'|| v_postype || ';description:' || v_desc);
end loop;
end;
注意:對於遊標變數,不能使用for迴圈來處理。因為for迴圈會隱式的執行open動作。而通過open for來開啟的遊標%isopen是為true的。也就是預設開啟的。Open一個已經open的遊標是錯誤的。所以不能使用for迴圈來處理遊標變數。
我們主要討論的是如何通過jdbc呼叫來處理這個輸出引數。
Java程式碼
1.conn = this.getDataSource().getConnection();
2.CallableStatement call = conn.prepareCall("{call procpkg.procrefcursor(?,?)}");
3.call.setString(1, null);
4.call.registerOutParameter(2, OracleTypes.CURSOR);
5.call.execute();
6.ResultSet rsResult = (ResultSet) call.getObject(2);
7.while (rsResult.next()) {
8. String posType = rsResult.getString("posType");
9. String description = rsResult.getString("description");
10. ......
11.}
conn = this.getDataSource().getConnection();
CallableStatement call = conn.prepareCall("{call procpkg.procrefcursor(?,?)}");
call.setString(1, null);
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
ResultSet rsResult = (ResultSet) call.getObject(2);
while (rsResult.next()) {
String posType = rsResult.getString("posType");
String description = rsResult.getString("description");
......
}
這就是jdbc的處理方法。
Ibatis處理方法:
1.引數配置
Java程式碼
1.<parameterMap id="PosTypeMAP" class="java.util.Map">
2. <parameter property="p" jdbcType="VARCHAR" javaType="java.lang.String" />
3. <parameter property="p_ref_postypeList" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" typeHandler="com.palic.elis.pos.dayprocset.integration.dao.impl.CursorHandlerCallBack" />
4.</parameterMap>
5.
6.2.呼叫過程
7. <procedure id ="procrefcursor" parameterMap ="PosTypeMAP">
8. {call procpkg.procrefcursor(?,?)}
9. </procedure>
10.
11.3.定義自己的處理器
12. public class CursorHandlerCallBack implements TypeHandler{
13. public Object getResult(CallableStatement cs, int index) throws SQLException {
14. ResultSet rs = (ResultSet)cs.getObject(index);
15. List result = new ArrayList();
16. while(rs.next()) {
17. String postype =rs.getString(1);
18. String description = rs.getString(2);
19. CodeTableItemDTO posTypeItem = new CodeTableItemDTO();
20. posTypeItem.setCode(postype);
21. posTypeItem.setDescription(description);
22. result.add(posTypeItem);
23. }
24. return result;
25. }
26.
27.
28.
29.4. dao方法
30. public List procPostype() {
31. String p = "";
32. Map para = new HashMap();
33. para.put("p",p);
34. para.put("p_ref_postypeList",null);
35. this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procrefcursor", para);
36. return (List)para.get("p_ref_postypeList");
37. }
<parameterMap id="PosTypeMAP" class="java.util.Map">
<parameter property="p" jdbcType="VARCHAR" javaType="java.lang.String" />
<parameter property="p_ref_postypeList" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" typeHandler="com.palic.elis.pos.dayprocset.integration.dao.impl.CursorHandlerCallBack" />
</parameterMap>
2.呼叫過程
<procedure id ="procrefcursor" parameterMap ="PosTypeMAP">
{call procpkg.procrefcursor(?,?)}
</procedure>
3.定義自己的處理器
public class CursorHandlerCallBack implements TypeHandler{
public Object getResult(CallableStatement cs, int index) throws SQLException {
ResultSet rs = (ResultSet)cs.getObject(index);
List result = new ArrayList();
while(rs.next()) {
String postype =rs.getString(1);
String description = rs.getString(2);
CodeTableItemDTO posTypeItem = new CodeTableItemDTO();
posTypeItem.setCode(postype);
posTypeItem.setDescription(description);
result.add(posTypeItem);
}
return result;
}
4. dao方法
public List procPostype() {
String p = "";
Map para = new HashMap();
para.put("p",p);
para.put("p_ref_postypeList",null);
this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procrefcursor", para);
return (List)para.get("p_ref_postypeList");
}
這個跟jdbc的方式非常的相似.
我們使用的是ibatis的2.0版本,比較麻煩。
如果是使用2.2以上版本就非常簡單的。
因為可以在parameterMap中定義一個resultMap.這樣就無需要自己定義處理器了。
可以從分析2.0和2.0的dtd檔案知道。
上面的兩種方式都是非常的複雜,如果僅僅是需要返回一個結果集,那就完全可以使用函式來實現了。
Java程式碼
1.create or replace package procpkg is
2. type refcursor is ref cursor;
3. procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor);
4. function procpostype(p varchar2) return PosTypeTable;
5.end procpkg;
6.
7.create or replace package body procpkg is
8. procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor)
9. is
10. v_posTypeList PosTypeTable;
11. begin
12. v_posTypeList :=PosTypeTable();--初始化巢狀表
13. v_posTypeList.extend;
14. v_posTypeList(1) := PosType('A001','客戶資料變更');
15. v_posTypeList.extend;
16. v_posTypeList(2) := PosType('A002','團體資料變更');
17. v_posTypeList.extend;
18. v_posTypeList(3) := PosType('A003','受益人變更');
19. v_posTypeList.extend;
20. v_posTypeList(4) := PosType('A004','續期交費方式變更');
21. open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable));
22. end;
23.
24. function procpostype(p varchar2) return PosTypeTable
25. as
26. v_posTypeList PosTypeTable;
27. begin
28. v_posTypeList :=PosTypeTable();--初始化巢狀表
29. v_posTypeList.extend;
30. v_posTypeList(1) := PosType('A001','客戶資料變更');
31. v_posTypeList.extend;
32. v_posTypeList(2) := PosType('A002','團體資料變更');
33. v_posTypeList.extend;
34. v_posTypeList(3) := PosType('A003','受益人變更');
35. v_posTypeList.extend;
36. v_posTypeList(4) := PosType('A004','續期交費方式變更');
37. return v_posTypeList;
38. end;
39.end procpkg;
create or replace package procpkg is
type refcursor is ref cursor;
procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor);
function procpostype(p varchar2) return PosTypeTable;
end procpkg;
create or replace package body procpkg is
procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor)
is
v_posTypeList PosTypeTable;
begin
v_posTypeList :=PosTypeTable();--初始化巢狀表
v_posTypeList.extend;
v_posTypeList(1) := PosType('A001','客戶資料變更');
v_posTypeList.extend;
v_posTypeList(2) := PosType('A002','團體資料變更');
v_posTypeList.extend;
v_posTypeList(3) := PosType('A003','受益人變更');
v_posTypeList.extend;
v_posTypeList(4) := PosType('A004','續期交費方式變更');
open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable));
end;
function procpostype(p varchar2) return PosTypeTable
as
v_posTypeList PosTypeTable;
begin
v_posTypeList :=PosTypeTable();--初始化巢狀表
v_posTypeList.extend;
v_posTypeList(1) := PosType('A001','客戶資料變更');
v_posTypeList.extend;
v_posTypeList(2) := PosType('A002','團體資料變更');
v_posTypeList.extend;
v_posTypeList(3) := PosType('A003','受益人變更');
v_posTypeList.extend;
v_posTypeList(4) := PosType('A004','續期交費方式變更');
return v_posTypeList;
end;
end procpkg;
ibatis配置
Java程式碼
1.<resultMap id="posTypeResultMap" class="com.palic.elis.pos.common.dto.CodeTableItemDTO">
2. <result property="code" column="posType"/>
3. <result property="description" column="description"/>
4. </resultMap>
5.
6. <select id="procPostype" resultMap="posTypeResultMap">
7. select * from table(cast (procpkg.procpostype(#value#) as PosTypeTable))
8. </select>
<resultMap id="posTypeResultMap" class="com.palic.elis.pos.common.dto.CodeTableItemDTO">
<result property="code" column="posType"/>
<result property="description" column="description"/>
</resultMap>
<select id="procPostype" resultMap="posTypeResultMap">
select * from table(cast (procpkg.procpostype(#value#) as PosTypeTable))
</select>Dao的寫法跟普通查詢一樣
Java程式碼
1.public List queryPostype() {
2. return this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procPostype", null);
3.}
public List queryPostype() {
return this.getSqlMapClientTemplate().queryForList("pos_dayprocset.procPostype", null);
}
有幾點需要注意,這裡不能使用索引表,而是巢狀表。
另外就是把巢狀表強制轉換為普通表。
轉載自:http://www.javaeye.com/topic/311176
Oracle中Cursor介紹
轉自:http://www.javaeye.com/topic/649874
一 概念
遊標是SQL的一個記憶體工作區,由系統或使用者以變數的形式定義。遊標的作用就是用於臨時儲存從資料庫中提取的資料塊。在某些情況下,需要把資料從存放在磁碟的表中調到計算機記憶體中進行處理,最後將處理結果顯示出來或最終寫回資料庫。這樣資料處理的速度才會提高,否則頻繁的磁碟資料交換會降低效率。
二 型別
Cursor型別包含三種: 隱式Cursor,顯式Cursor和Ref Cursor(動態Cursor)。
1. 隱式Cursor:
1).對於Select …INTO…語句,一次只能從資料庫中獲取到一條資料,對於這種型別的DML Sql語句,就是隱式Cursor。例如:Select /Update / Insert/Delete操作。
2)作用:可以通過隱式Cusor的屬性來了解操作的狀態和結果,從而達到流程的控制。Cursor的屬性包含:
SQL%ROWCOUNT 整型 代表DML語句成功執行的資料行數
SQL%FOUND 布林型 值為TRUE代表插入、刪除、更新或單行查詢操作成功
SQL%NOTFOUND 布林型 與SQL%FOUND屬性返回值相反
SQL%ISOPEN 布林型 DML執行過程中為真,結束後為假
3) 隱式Cursor是系統自動開啟和關閉Cursor.
下面是一個Sample:
- Set Serveroutput on;
- begin
- update t_contract_master set liability_state = 1 where policy_code = '123456789';
- if SQL%Found then
- dbms_output.put_line('the Policy is updated successfully.');
- commit;
- else
- dbms_output.put_line('the policy is updated failed.');
- end if;
- end;
- /
- Set Serveroutput on;
- begin
- update t_contract_master set liability_state = 1 where policy_code = '123456789';
- if SQL%Found then
-
dbms_output.put_line(
相關推薦
oracle儲存過程,集合物件處理
我們在進行pl/sql程式設計時打交道最多的就是儲存過程了。儲存過程的結構是非常的簡單的,我們在這裡除了學習儲存過程的基本結構外,還會學習編寫儲存過程時相關的一些實用的知識。如:遊標的處理,異常的處理,集合的選擇等等 1.儲存過程結構 1.1 第一個儲存過程 Java程式碼 1.create
bat批處理指令碼呼叫oracle儲存過程,系統定時呼叫指令碼
編寫一個bat批處理指令碼呼叫oracle儲存過程,並設定系統定時完成該指令碼任務,有以下幾個步驟: 1. 先編寫一個call.sql檔案: set timing on; DECLARE o_UserID varchar2(20); BEGIN Packag
Oracle 儲存過程,觸發器,事務,鎖
部落格園 首頁 新隨筆 聯絡 管理 訂閱 隨筆- 75 文章- 0 評論- 0 Oracle 儲存過程,觸發器,事務,鎖 1.1儲存過程 儲存過程是一種命名的PL/SQL程式塊,他可以有引數,也可以有若干
mybatis 呼叫oracle儲存過程,傳參、返回遊標的值獲取--示例
1,dao層程式碼 Integer currentlq_fsx = getSqlSession().selectOne("lqMapper.maxscore"); Map<String,Object> map = new HashMap<String,O
Oracle儲存過程,臨時表的建立、刪除,變數的定義和使用
create or replace procedure Test_GetOaUserInfo as --authid current_user操作當前儲存過程的當前使用者,否則提示許可權不足,但是這樣儲存過程這能執行一次 --,或者GRANT CREATE A
Oracle儲存過程呼叫bat批處理指令碼程式
由於系統業務的複雜性,會經常用到Oracle資料的儲存過程,那些比較複雜的邏輯就寫在了儲存過程中。今天有遇到一個需求,是需要在儲存過程中呼叫windows系統上的bat批處理檔案,之前
Oracle儲存過程,函式,觸發器
一:儲存過程的定義 1>過程(多次編譯 多次執行): --過程實現計算器 declare p1 number:=1; p2 number:=2; sign varchar2(3):='-';
使用oracle 儲存過程返回集合
1 所謂返回集合是返回遊標 集合可以不使用臨時表,使用oracle自定義型別 2 可以先建一個object的type,再建一個type 型別為table 如:CREATE OR REPLACE TYPE Dis_WGM_dayreport_type as objec
Oracle儲存過程物件(package,procedure etc...) 呼叫許可權 ----20180206
在一些技術論壇裡面,常常看到有朋友問這種問題: 為什麼我的使用者具有DBA許可權,卻無法在儲存過程裡面建立一張普通表呢? 下面就結合具體案例來談談這個問題: SQL> conn eric/eric; Connected.SQL> select * from dba_role_privs whe
py呼叫oracle的儲存過程,注意procedure的out取值方式
#!/usr/bin/env python import sys import csv import cx_Oracle import codecs import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
Oracle 儲存過程中傳送郵件,並支援使用者驗證 中文標題和內容
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Oracle學習筆記 -分頁,儲存過程,自增
Oracled 的欄位自增,查詢,遊標的返回,儲存過程。 1,自增(觸發器) //還有一個條件需要增加一個佇列 create or replace TRIGGER MYSYSTEM.CBOUNCEID BEFORE INSERT ON MYSYSTEM.BOUNCERECORD
java中呼叫Oracle儲存過程時,出現異常:java.sql.SQLException: ORA-00928: 缺失 SELECT 關鍵字(已解決)
在java中呼叫Oracle儲存過程時,出現異常:java.sql.SQLException: ORA-00928: 缺失 SELECT 關鍵字 //java程式碼 @Test public void testProcedure(){
ORACLE 建立儲存過程,儲存函式
基本概念儲存過程和儲存函式相當於一個東西。儲存過程在Oracle裡叫procedure。儲存過程沒有返回值。儲存函式在Oracle裡叫function。儲存函式有返回值。基本語法create or replace procedure 名字--create or replace
oracle儲存過程傳入in引數,返回結果集
create or replace procedure proc_report_TEST(zhxshss in varchar2, pcursor out sys_refcursor) as begi
oracle儲存過程處理ddl與dml語句
declare CURSOR C_EVENT is select table_name from [email protected]_YWKDB; temp varchar2(100); begin OPEN C_EVENT; F
oracle檢視包儲存過程,函式,以及儲存過程引數,函式引數
1. 今天檢視系統程式碼時意識到系統中有太多的包以及它們的引數實在太多不容易記,所以想做一個查詢,把它們都查出來 2. 檢視系統中用到的包中所包含的儲存過程,函式等 SELECT U.PACKAGE_NAME AS 包名, U.OBJECT_NAME AS 方法 FRO
工作總結24 Windows的任務計劃,定時執行oracle儲存過程或語句塊
1、在pl/sql中,建立一個儲存過程 create or replace procedure pro as begin /***業務程式***/ commit; end pro; 2、在某
ORACLE PL/SQL語法應用:遊標,儲存過程,觸發器,函式
--遊標 --do while迴圈 declare cursor c is select * from t_t_student order by id; v_record c%rowtype
mysql觸發器,儲存過程,預處理語句
建立t1表 create table t1(id varchar(10),name varchar(10),age varchar(20)); 建立t2表 create table t2(id varchar(10),name varchar(10),age varc