Bonjour,
En premier, il y a les scripts puis mes questions suivent, merci.
script_01.sql
script_02.sql
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 if object_id('a') is not null begin drop table a end go create table a ( a int null ) go insert into a (a) values (1) go -- Début de transaction begin tran insert into a (a) values (3) -- Génère une erreur car la table n'existe pas insert into b (b) values (2) select @@error if @@error <> 0 begin rollback tran print "erreur" return end commit tran go select * from a go if object_id('a') is not null begin drop table a end go
script_03.sql
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 if object_id('a') is not null begin drop table a end go create table a ( a int null ) go insert into a (a) values (1) go -- Début de transaction begin tran insert into a (a) values (3) -- Génère une erreur car la table n'existe pas insert into b (b) values (2) select @@error go if @@error <> 0 begin rollback tran print "erreur" return end commit tran go select * from a go if object_id('a') is not null begin drop table a end go
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 if object_id('a') is not null begin drop table a end go create table a ( a int null ) go insert into a (a) values (1) go -- Début de transaction begin tran insert into a (a) values (3) -- Génère une erreur car la table n'existe pas insert into b (b) values (2) if @@error <> 0 begin rollback tran print "erreur" return end commit tran go select * from a go if object_id('a') is not null begin drop table a end go
Lorsque j'exécute ces scripts là avec isql j'obtiens :
Je constate qu'on sort du premier batch juste après "insert into b (b) values (2)".
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 (1 row affected) Msg 208, Level 16, State 1: Server 'MUTUDEV', Line 8: b not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output). a ----------- 1 (1 row affected)
Puis on reprend au deuxième batch.
Faut il en conclure que pour ce type d'erreur la gestion d'erreur n'y fait rien ?
Pourquoi ?
Maintenant la même opération avec sqladv :
script_01.sql
script_02.sql
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 (1 row affected) Server Message: Number 208, Severity 16 Server 'MUTUDEV', Line 7: b not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output). (1 row affected) Resultset : a ----------- 1
script_03.sql
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 (1 row affected) Server Message: Number 208, Severity 16 Server 'MUTUDEV', Line 7: b not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output). erreur (1 row affected) Resultset : a ----------- 1
Je remarque que la gestion d'erreur marche car il a bien fait le print "erreur".
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 (1 row affected) Server Message: Number 208, Severity 16 Server 'MUTUDEV', Line 7: b not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output). (1 row affected) Resultset : a ----------- 1
Pourquoi la gestion d'erreur fonctionne sur le script_02.sql ?
Je tiens à préciser que le discours tenu par les formateurs sybase concernant la gestion d'erreurs est le suivant :
Comment être sûr de sa gestion d'erreurs ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 /* Gestion d'erreur lors de mises à jour */ if conditions begin declare @err int, @rows int begin tran print "#@# Mise à jour de la table ..., champ ..." -- Instruction DML select @err = @@error, @rows = @@rowcount if @err <> 0 begin rollback tran raiserror 30001, "table_name", "base_name" return end commit tran print "Nombre de mises à jour : %1!." , @rows end else begin print "Pas de mise à jour dans la table ..." end go
Pour information :
1> select @@version
2> go
-----------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
----------------
Adaptive Server Enterprise/12.5.3/EBF 13197 ESD#6/P/Sun_svr4/OS 5.8/ase1253/1945/64-bit/FBO/Fri Jan 20 09:46:56 2006
Partager