-
Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathsave-exc-and-forall.sql
282 lines (245 loc) · 8.18 KB
/
save-exc-and-forall.sql
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Add the SAVE EXCEPTIONS clause to your FORALL statement when you want the
PL/SQL runtime engine to execute all DML statements generated by the FORALL,
even if one or more than fail with an error. If you use INDICES OF, you will
need to take some care to find your way back to the offending statement.
*/
CREATE TABLE employees
AS
SELECT * FROM hr.employees ;
-- Name that -24381! (once)
/*
PL/SQL raises ORA-24381 if at least one statement failed in a FORALL that
uses SAVE EXCEPTION. It is best to handle exceptions by name, but this error
is not named in STANDARD. So we do it ourselves!
*/
CREATE OR REPLACE PACKAGE std_errs
IS
failure_in_forall EXCEPTION;
PRAGMA EXCEPTION_INIT (failure_in_forall, -24381);
END;
/
-- First, Without SAVE EXCEPTIONS
/*
You can't update a first_name to a string of 1000 or 3000 bytes. But without
SAVE EXCEPTIONS we never get past the third element in the bind array.
The employees table has 107 rows. How many were updated?
*/
DECLARE
TYPE namelist_t IS TABLE OF VARCHAR2 (5000);
enames_with_errors namelist_t
:= namelist_t ('ABC',
'DEF',
RPAD ('BIGBIGGERBIGGEST', 1000, 'ABC'),
'LITTLE',
RPAD ('BIGBIGGERBIGGEST', 3000, 'ABC'),
'SMITHIE');
BEGIN
FORALL indx IN 1 .. enames_with_errors.COUNT
UPDATE employees
SET first_name = enames_with_errors (indx);
ROLLBACK;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (
'Updated ' || SQL%ROWCOUNT || ' rows.');
DBMS_OUTPUT.put_line (SQLERRM);
ROLLBACK;
END;
/
-- Now With SAVE EXCEPTIONS
/*
Execute every generated statement no matter how of them fail, please!
Now how many rows were updated? Notice that with SAVE EXCEPTIONS in place,
I can take advantage of SQL%BULK_EXCEPTIONS to see how statements failed,
and which ones, and with which error. Can you see, however, the difference
between the error information displayed in the previous step and this one?
*/
DECLARE
TYPE namelist_t IS TABLE OF VARCHAR2 (5000);
enames_with_errors namelist_t
:= namelist_t ('ABC',
'DEF',
RPAD ('BIGBIGGERBIGGEST', 1000, 'ABC'),
'LITTLE',
RPAD ('BIGBIGGERBIGGEST', 3000, 'ABC'),
'SMITHIE');
BEGIN
FORALL indx IN 1 .. enames_with_errors.COUNT SAVE EXCEPTIONS
UPDATE employees
SET first_name = enames_with_errors (indx);
ROLLBACK;
EXCEPTION
WHEN std_errs.failure_in_forall
THEN
DBMS_OUTPUT.put_line (SQLERRM);
DBMS_OUTPUT.put_line (
'Updated ' || SQL%ROWCOUNT || ' rows.');
FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
LOOP
DBMS_OUTPUT.put_line (
'Error '
|| indx
|| ' occurred on index '
|| SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX
|| ' attempting to update name to "'
|| enames_with_errors (
SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX)
|| '"');
DBMS_OUTPUT.put_line (
'Oracle error is '
|| SQLERRM (
-1 * SQL%BULK_EXCEPTIONS (indx).ERROR_CODE));
END LOOP;
ROLLBACK;
END;
/
-- Now Explore SAVE EXCEPTIONS with Sparse Bind Arrays
/*
If the array that drives the FORALL statement (the bind array) is not
dense and you use INDICES OF, you can run into some complications.
*/
CREATE TABLE plch_employees
(
employee_id INTEGER,
last_name VARCHAR2 (100),
salary NUMBER (8, 0)
) ;
BEGIN
INSERT INTO plch_employees
VALUES (100, 'Ninhursag ', 1000000);
INSERT INTO plch_employees
VALUES (200, 'Inanna', 1000000);
INSERT INTO plch_employees
VALUES (300, 'Enlil', 1000000);
COMMIT;
END;
/
-- INDICES OF - BETWEEN - Complications!
/*
INDICES OF is a great feature: you can use it when your bind array is not
densely filled. And you can use BETWEEN to further finesse which elements
in the bind array are used to generate statements. But then it is a challenge
to correlate the SQL%BULK_EXCEPTIONS ERROR_INDEX value back to the right
index value in the bind array! Which index values do you think will be displayed here?
*/
DECLARE
TYPE employee_aat IS TABLE OF employees.employee_id%TYPE
INDEX BY PLS_INTEGER;
l_employees employee_aat;
BEGIN
l_employees (1) := 100;
l_employees (2) := 200;
l_employees (3) := 300;
l_employees (4) := 200;
l_employees (5) := 100;
FORALL l_index IN INDICES OF l_employees BETWEEN 3 AND 5
SAVE EXCEPTIONS
UPDATE plch_employees
SET salary =
salary
* CASE employee_id WHEN 200 THEN 1 ELSE 100 END
WHERE employee_id = l_employees (l_index);
EXCEPTION
WHEN std_errs.failure_in_forall
THEN
DBMS_OUTPUT.put_line ('Errors:');
FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
LOOP
DBMS_OUTPUT.put_line (
SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX);
END LOOP;
ROLLBACK;
END;
/
-- Correlate ERROR INDEX Back to Bind Array
/*
Now I offer a helpful utility, bind_array_index_for, that figures out the
actual index value in the bind array from the ERROR_INDEX value and the
start/end values in the INDICES OF BETWEEN's clause.
*/
DECLARE
TYPE employee_aat IS TABLE OF employees.employee_id%TYPE
INDEX BY PLS_INTEGER;
l_employees employee_aat;
FUNCTION bind_array_index_for (
bind_array_in IN employee_aat,
error_index_in IN PLS_INTEGER,
start_in IN PLS_INTEGER DEFAULT NULL,
end_in IN PLS_INTEGER DEFAULT NULL)
RETURN PLS_INTEGER
IS
l_index PLS_INTEGER
:= NVL (start_in, bind_array_in.FIRST);
BEGIN
FOR indx IN 1 .. error_index_in - 1
LOOP
l_index := bind_array_in.NEXT (l_index);
END LOOP;
RETURN l_index;
END;
BEGIN
BEGIN
l_employees (1) := 100;
l_employees (100) := 200;
l_employees (500) := 300;
FORALL l_index IN INDICES OF l_employees SAVE EXCEPTIONS
UPDATE plch_employees
SET salary =
salary
* CASE employee_id
WHEN 200 THEN 1
ELSE 100
END
WHERE employee_id = l_employees (l_index);
EXCEPTION
WHEN failure_in_forall
THEN
DBMS_OUTPUT.put_line ('Errors:');
FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
LOOP
DBMS_OUTPUT.put_line (
SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX);
DBMS_OUTPUT.put_line (
bind_array_index_for (
l_employees,
SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX));
END LOOP;
ROLLBACK;
END;
BEGIN
l_employees (1) := 100;
l_employees (2) := 200;
l_employees (3) := 300;
l_employees (4) := 200;
l_employees (5) := 100;
FORALL l_index IN INDICES OF l_employees BETWEEN 3 AND 5
SAVE EXCEPTIONS
UPDATE plch_employees
SET salary =
salary
* CASE employee_id
WHEN 200 THEN 1
ELSE 100
END
WHERE employee_id = l_employees (l_index);
EXCEPTION
WHEN std_errs.failure_in_forall
THEN
DBMS_OUTPUT.put_line ('Errors:');
FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
LOOP
DBMS_OUTPUT.put_line (
SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX);
DBMS_OUTPUT.put_line (
bind_array_index_for (
l_employees,
SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX,
3,
5));
END LOOP;
ROLLBACK;
END;
END;
/