Le curseur implicite Oracle SQL%ROWCOUNT utilise en PL/SQL pour connaître le nombre de lignes impactées par la dernière requête de type DML (select, insert, update, delete, merge).
Exemple :
SQL> CREATE TABLE toto (a NUMBER, b NUMBER);
Table created.
SQL> SET SERVEROUTPUT ON SIZE 20000
SQL> DECLARE
2
3 v NUMBER;
4
5 BEGIN
6
7 INSERT INTO toto VALUES (1, null);
8
9 DBMS_OUTPUT.PUT_LINE('A - NB LIGNES : '||SQL%ROWCOUNT);
10
11 INSERT INTO toto VALUES (2, null);
12 INSERT INTO toto VALUES (3, null);
13 DBMS_OUTPUT.PUT_LINE('B - NB LIGNES : '||SQL%ROWCOUNT);
14
15 UPDATE toto SET b=2;
16 DBMS_OUTPUT.PUT_LINE('C - NB LIGNES : '||SQL%ROWCOUNT);
17
18 SELECT COUNT(*) INTO V FROM toto;
19 DBMS_OUTPUT.PUT_LINE('D - NB LIGNES : '||SQL%ROWCOUNT);
20
21 END;
22 /
A - NB LIGNES : 1
B - NB LIGNES : 1
C - NB LIGNES : 3
D - NB LIGNES : 1
PL/SQL procedure successfully completed.
SQL> CREATE TABLE titi AS SELECT * FROM toto;
Table created.
SQL> BEGIN
2
3 MERGE INTO toto USING titi ON (toto.a=titi.a)
4 WHEN MATCHED THEN UPDATE SET toto.b=titi.b;
5 DBMS_OUTPUT.PUT_LINE('E - NB LIGNES : '||SQL%ROWCOUNT);
6
7 EXECUTE IMMEDIATE('INSERT INTO titi SELECT * FROM titi');
8 DBMS_OUTPUT.PUT_LINE('F - NB LIGNES : '||SQL%ROWCOUNT);
9
10 DELETE FROM titi;
11 DBMS_OUTPUT.PUT_LINE('G - NB LIGNES : '||SQL%ROWCOUNT);
12
13 EXECUTE IMMEDIATE('DROP TABLE titi');
14 DBMS_OUTPUT.PUT_LINE('H - NB LIGNES : '||SQL%ROWCOUNT);
15
16 END;
17 /
E - NB LIGNES : 3
F - NB LIGNES : 3
G - NB LIGNES : 6
H - NB LIGNES : 0
PL/SQL procedure successfully completed.
Table created.
SQL> SET SERVEROUTPUT ON SIZE 20000
SQL> DECLARE
2
3 v NUMBER;
4
5 BEGIN
6
7 INSERT INTO toto VALUES (1, null);
8
9 DBMS_OUTPUT.PUT_LINE('A - NB LIGNES : '||SQL%ROWCOUNT);
10
11 INSERT INTO toto VALUES (2, null);
12 INSERT INTO toto VALUES (3, null);
13 DBMS_OUTPUT.PUT_LINE('B - NB LIGNES : '||SQL%ROWCOUNT);
14
15 UPDATE toto SET b=2;
16 DBMS_OUTPUT.PUT_LINE('C - NB LIGNES : '||SQL%ROWCOUNT);
17
18 SELECT COUNT(*) INTO V FROM toto;
19 DBMS_OUTPUT.PUT_LINE('D - NB LIGNES : '||SQL%ROWCOUNT);
20
21 END;
22 /
A - NB LIGNES : 1
B - NB LIGNES : 1
C - NB LIGNES : 3
D - NB LIGNES : 1
PL/SQL procedure successfully completed.
SQL> CREATE TABLE titi AS SELECT * FROM toto;
Table created.
SQL> BEGIN
2
3 MERGE INTO toto USING titi ON (toto.a=titi.a)
4 WHEN MATCHED THEN UPDATE SET toto.b=titi.b;
5 DBMS_OUTPUT.PUT_LINE('E - NB LIGNES : '||SQL%ROWCOUNT);
6
7 EXECUTE IMMEDIATE('INSERT INTO titi SELECT * FROM titi');
8 DBMS_OUTPUT.PUT_LINE('F - NB LIGNES : '||SQL%ROWCOUNT);
9
10 DELETE FROM titi;
11 DBMS_OUTPUT.PUT_LINE('G - NB LIGNES : '||SQL%ROWCOUNT);
12
13 EXECUTE IMMEDIATE('DROP TABLE titi');
14 DBMS_OUTPUT.PUT_LINE('H - NB LIGNES : '||SQL%ROWCOUNT);
15
16 END;
17 /
E - NB LIGNES : 3
F - NB LIGNES : 3
G - NB LIGNES : 6
H - NB LIGNES : 0
PL/SQL procedure successfully completed.
Aucun commentaire:
Enregistrer un commentaire