1. 程式人生 > >sql%bulk_rowcount && sql%rowcount 的使用

sql%bulk_rowcount && sql%rowcount 的使用

行數 attr osi 底部 line 數據 scala cat varchar2

說明:

%BULK_ROWCOUNT 屬性計算FORALL叠代影響行數
  在進行SQL數據操作語句時,SQL引擎打開一個隱式遊標(命名為SQL),該遊標的標量屬性(scalar attribute)有 %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT。
  FORALL語句除具有上邊的標量屬性外,還有個復合屬性(composite attribute):%BULK_ROWCOUNT,該屬性具有索引表(index-by table)語法。它的第i個元素存貯SQL語句(INSERT, UPDATE或DELETE)第i個執行的處理行數。如果第i個執行未影響行,%bulk_rowcount (i),返回0。FORALL與%bulk_rowcount屬性使用相同下標

--%bulk_rowcount 代碼 :

 1 /*
 2 drop table TESTT ;
 3 CREATE TABLE TESTT(TID varchar2(100)) ;
 4 TRUNCATE TABLE TESTT ;
 5 SELECT * FROM TESTT ;
 6 
 7 */
 8 
 9 DECLARE
10   type stp_index is table of sys_product.productcode%type;
11   --idx_tab stp_index :=number_index(null) ;
12   idx_tab stp_index;
13 cursor cur_stp is 14 select productcode pcode from sys_product; 15 16 BEGIN 17 ----新增 18 open cur_stp; 19 loop 20 fetch cur_stp bulk collect 21 into idx_tab; 22 exit when cur_stp%notfound; 23 end loop; 24 close cur_stp ; 25 forall cur in idx_tab.first .. idx_tab.last
26 insert into testt values (idx_tab(cur)); 27 dbms_output.put_line(insert -------------- ||sql%rowcount); 28 for i in idx_tab.first .. idx_tab.count loop 29 dbms_output.put_line(idx_tab(i)|| insert 受影響行為 || sql%bulk_rowcount(i)|| 行 !); 30 end loop; 31 COMMIT; 32 --修改 33 forall cur in idx_tab.first .. idx_tab.last 34 update testt set tid=idx_tab(cur)|| % where tid=idx_tab(cur); 35 dbms_output.put_line(update -------------- ||sql%rowcount); 36 for i in idx_tab.first .. idx_tab.count loop 37 dbms_output.put_line(idx_tab(i)|| update 受影響行為 || sql%bulk_rowcount(i)|| 行 ! || sql%rowcount); 38 end loop; 39 END;

---結果:

 1 insert --------------19
 2 GFWD insert 受影響行為 1 行 !
 3 GOPTION insert 受影響行為 1 行 !
 4 NCD insert 受影響行為 1 行 !
 5 GLS insert 受影響行為 1 行 !
 6 IBO insert 受影響行為 1 行 !
 7 REPO insert 受影響行為 1 行 !
 8 OBPO insert 受影響行為 1 行 !
 9 BSD insert 受影響行為 1 行 !
10 DEPO insert 受影響行為 1 行 !
11 IRS insert 受影響行為 1 行 !
12 FXSWAP insert 受影響行為 1 行 !
13 FRA insert 受影響行為 1 行 !
14 IBL insert 受影響行為 1 行 !
15 FXFWD insert 受影響行為 1 行 !
16 CCS insert 受影響行為 1 行 !
17 FXOPTION insert 受影響行為 1 行 !
18 GSWAP insert 受影響行為 1 行 !
19 BSDC insert 受影響行為 1 行 !
20 CI insert 受影響行為 1 行 !
21 update --------------19
22 GFWD update 受影響行為 1 行 !       19
23 GOPTION update 受影響行為 1 行 !       19
24 NCD update 受影響行為 1 行 !       19
25 GLS update 受影響行為 1 行 !       19
26 IBO update 受影響行為 1 行 !       19
27 REPO update 受影響行為 1 行 !       19
28 OBPO update 受影響行為 1 行 !       19
29 BSD update 受影響行為 1 行 !       19
30 DEPO update 受影響行為 1 行 !       19
31 IRS update 受影響行為 1 行 !       19
32 FXSWAP update 受影響行為 1 行 !       19
33 FRA update 受影響行為 1 行 !       19
34 IBL update 受影響行為 1 行 !       19
35 FXFWD update 受影響行為 1 行 !       19
36 CCS update 受影響行為 1 行 !       19
37 FXOPTION update 受影響行為 1 行 !       19
38 GSWAP update 受影響行為 1 行 !       19
39 BSDC update 受影響行為 1 行 !       19
40 CI update 受影響行為 1 行 !       19

--sql%rowcount && cursor%rowcount ;

 1 declare
 2   cursor cur_stp is
 3     select s.productcode pcode from sys_product s;
 4   TID varchar2(100);
 5 begin
 6   open cur_stp;
 7   dbms_output.put_Line(0_cur_stp%rowcount=||cur_stp%rowcount);
 8   loop
 9     fetch cur_stp
10       into tid;
11       --註意這裏沒有換行;
12     dbms_output.put(cur_stp%rowcount|| ); --‘0~19‘
13     exit when cur_stp%notfound;
14   end loop;
15   --放到遊標裏面和外面的區別;
16   dbms_output.put_Line(sql%rowcount= || sql%rowcount);
17   dbms_output.put_Line(1_cur_stp%rowcount= || cur_stp%rowcount);
18   close cur_stp;
19 
20   for i in cur_stp loop
21     dbms_output.put_Line(cur_stp%rowcount || -- || i.pcode);
22   end loop;
23   --放到底部的話會 拋錯 :無效的遊標;  dbms_output.put_Line(cur_stp%rowcount); 
24 end;

--結果:

 1 0_cur_stp%rowcount=0
 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 19 sql%rowcount=
 3 1_cur_stp%rowcount=19
 4 1--GFWD
 5 2--GOPTION
 6 3--NCD
 7 4--GLS
 8 5--IBO
 9 6--REPO
10 7--OBPO
11 8--BSD
12 9--DEPO
13 10--IRS
14 11--FXSWAP
15 12--FRA
16 13--IBL
17 14--FXFWD
18 15--CCS
19 16--FXOPTION
20 17--GSWAP
21 18--BSDC
22 19--CI

sql%bulk_rowcount && sql%rowcount 的使用