~ubuntu-branches/ubuntu/hardy/mysql-dfsg-5.0/hardy-updates

« back to all changes in this revision

Viewing changes to mysql-test/suite/funcs_1/storedproc/storedproc_02.inc

  • Committer: Bazaar Package Importer
  • Author(s): sean finney
  • Date: 2007-05-13 12:32:45 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20070513123245-8c3l187dk34cz2ar
Tags: 5.0.41-2
the previous "translation changes" inadvertently introduced unrelated
changes in the package control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#### suite/funcs_1/storedproc/storedproc_02.inc
 
2
#
 
3
--source suite/funcs_1/storedproc/load_sp_tb.inc
 
4
 
 
5
# ==============================================================================
 
6
# (numbering from requirement document TP v1.0, Last updated: 25 Jan 2005 01:00)
 
7
#
 
8
# 3.1.2 Syntax checks for the stored procedure-specific programming statements BEGIN/END, DECLARE, SET, SELECT/INTO, OPEN, FETCH, CLOSE:
 
9
#
 
10
#-  1. Ensure that all subclauses that should be supported are supported.
 
11
#-  2. Ensure that all subclauses that should not be supported are disallowed with an appropriate error message.
 
12
#-  3. Ensure that all supported subclauses are supported only in the correct order.
 
13
#-  4. Ensure that an appropriate error message is returned if a subclause is out-of-order in a stored procedure definition.
 
14
#-  5. Ensure that all subclauses that are defined to be mandatory are indeed required to be mandatory by the MySQL server and tools.
 
15
#-  6. Ensure that any subclauses that are defined to be optional are indeed treated as optional by the MySQL server and tools.
 
16
#-  7. Ensure that every BEGIN statement is coupled with a terminating END statement.
 
17
##  8. Ensure that the scope of each BEGIN/END compound statement within a stored procedure definition is properly applied.
 
18
#-  9. Ensure that the labels enclosing each BEGIN/END compound statement must match.
 
19
#- 10. Ensure that it is possible to put a beginning label at the start of a BEGIN/END compound statement without also requiring an ending label at the end of the same statement.
 
20
#- 11. Ensure that it is not possible to put an ending label at the end of a BEGIN/END compound statement without also requiring a matching beginning label at the start of the same statement.
 
21
#- 12. Ensure that every beginning label must end with a colon (:).
 
22
#- 13. Ensure that every beginning label with the same scope must be unique.
 
23
#- 14. Ensure that the variables, cursors, conditions, and handlers declared for a stored procedure (with the DECLARE statement) may only be properly defined.
 
24
#- 15. Ensure that the variables, cursors, conditions, and handlers declared for a stored procedure (with the DECLARE statement) may only be defined in the correct order.
 
25
#- 16. Ensure that every possible type of variable -- utilizing every data type definition supported by the MySQL server in combination with both no DEFAULT subclause and with DEFAULT subclauses that set the variable�s default value to a range of appropriate values -- may be declared for a stored procedure.
 
26
#- 17. Ensure that the DECLARE statement can declare multiple variables both separately and all at once from a variable list.
 
27
#- 18. Ensure that invalid variable declarations are rejected, with an appropriate error message.
 
28
#- 19. Ensure that every possible type of cursor may be declared for a stored procedure.
 
29
#- 20. Ensure that invalid cursor declarations are rejected, with an appropriate error message.
 
30
#- 21. Ensure that every possible type of condition may be declared for a stored procedure.
 
31
# -22. Ensure that invalid condition declarations are rejected, with an appropriate error message.
 
32
#- 23. Ensure that every possible type of handler may be declared for a stored procedure.
 
33
#- 24. Ensure that invalid handler declarations are rejected, with an appropriate error message.
 
34
#- 25. Ensure that the scope of every variable, cursor, condition, and handler declared for a stored procedure (with the DECLARE statement) is properly applied.
 
35
## 26. Ensure that the initial value of every variable declared for a stored procedure is either NULL or its DEFAULT value, as appropriate.
 
36
#- 27. Ensure that the SET statement can assign a value to every local variable declared within a stored procedure�s definition, as well as to every appropriate global server variable.
 
37
#- 28. Ensure that the SET statement can assign values to variables either separately or to multiple variables in a list.
 
38
#- 29. Ensure that the SET statement may assign only those values to a variable that are appropriate for that variable�s data type definition.
 
39
## 30. Ensure that, when a stored procedure is called/executed, every variable always uses the correct value: either the value with which it is initialized or the value to which it is subsequently SET or otherwise assigned, as appropriate.
 
40
## 31. Ensure that the SELECT ... INTO statement properly assigns values to the variables in its variable list.
 
41
## 32. Ensure that a SELECT ... INTO statement that retrieves multiple rows is rejected, with an appropriate error message.
 
42
## 33. Ensure that a SELECT ... INTO statement that retrieves too many columns for the number of variables in its variable list is rejected, with an appropriate error message.
 
43
## 34. Ensure that a SELECT ... INTO statement that retrieves too few columns for the number of variables in its variable list is rejected, with an appropriate error message.
 
44
#- 35. Ensure that a SELECT ... INTO statement that retrieves column values with inappropriate data types for the matching variables in its variable list is rejected, with an appropriate error message.
 
45
#- 36. Ensure that the DECLARE ... CONDITION FOR statement can declare a properly-named condition for every possible SQLSTATE and MySQL-specific error code.
 
46
#- 37. Ensure that no two conditions declared with the same scope may have the same condition name.
 
47
## 38. Ensure that the scope of every condition declared is properly applied.
 
48
#- 39. Ensure that every SQLSTATE value declared with a DECLARE ... CONDITION FOR statement is a character string that is 5 characters long.
 
49
#- 40. Ensure that the DECLARE ... CONDITION FOR statement cannot declare a condition for an invalid SQLSTATE.
 
50
#- 41. Ensure that the DECLARE ... CONDITION FOR statement cannot declare a condition for the �successful completion SQLSTATE: �00000�.
 
51
#- 42. Ensure that the DECLARE ... HANDLER FOR statement can declare a CONTINUE, EXIT, and UNDO handler for every condition declared (with a DECLARE ... CONDITION FOR statement), within the scope of the handler, for a stored procedure, as well as for every possible SQLSTATE and MySQL-specific error code, as well as for the predefined conditions SQLWARNING, NOT FOUND, and SQLEXCEPTION.
 
52
## 43. Ensure that the DECLARE ... HANDLER FOR statement can not declare any handler for a condition declared outside of the scope of the handler.
 
53
## 44. Ensure that the DECLARE ... HANDLER FOR statement cannot declare a handler for any invalid, or undeclared, condition.
 
54
## 45. Ensure that the scope of every handler declared is properly applied.
 
55
#- 46. Ensure that, within the same scope, no two handlers may be declared for the same condition.
 
56
#- 47. Ensure that every SQLSTATE value declared with a DECLARE ... HANDLER FOR statement is a character string that is 5 characters long.
 
57
#- 48. Ensure that the DECLARE ... HANDLER FOR statement cannot declare a condition for an invalid SQLSTATE.
 
58
#- 49. Ensure that the DECLARE ... HANDLER FOR statement cannot declare a condition for the �successful completion SQLSTATE: �00000�.
 
59
## 50. Ensure that a CONTINUE handler allows the execution of the stored procedure to continue once the handler statement has completed its own execution (that is, once the handler action statement has been executed).
 
60
## 51. Ensure that an EXIT handler causes the execution of the stored procedure to terminate, within its scope, once the handler action statement has been executed.
 
61
## 52. Ensure that an EXIT handler does not cause the execution of the stored procedure to terminate outside of its scope.
 
62
#- 53. Ensure that a handler condition of SQLWARNING takes the same action as a handler condition defined with an SQLSTATE that begins with �01�.
 
63
## 54. Ensure that a handler with a condition defined with an SQLSTATE that begins with �01� is always exactly equivalent in action to a handler with an SQLWARNING condition.
 
64
#- 55. Ensure that a handler condition of NOT FOUND takes the same action as a handler condition defined with an SQLSTATE that begins with �02�.
 
65
## 56. Ensure that a handler with a condition defined with an SQLSTATE that begins with �02� is always exactly equivalent in action to a handler with a NOT FOUND condition.
 
66
#- 57. Ensure that a handler condition of SQLEXCEPTION takes the same action as a handler condition defined with an SQLSTATE that begins with anything other that �01� or �02�.
 
67
## 58. Ensure that a handler with a condition defined with an SQLSTATE that begins with anything other that �01� or �02� is always exactly equivalent in action to a handler with an SQLEXCEPTION condition.
 
68
#- 59. Ensure that no two cursors in a stored procedure can have the same name.
 
69
#- 60. Ensure that a cursor declaration may not include a SELECT ... INTO statement.
 
70
#- 61. Ensure that a cursor declaration that includes an ORDER BY clause may not be an updatable cursor.
 
71
#- 62. Ensure that OPEN <cursor name> fails unless a cursor with the same name has already been declared.
 
72
#- 63. Ensure that OPEN <cursor name> fails if the same cursor is currently already open.
 
73
#- 64. Ensure that FETCH <cursor name> fails unless a cursor with the same name is already open.
 
74
## 65. Ensure that FETCH <cursor name> returns the first row of the cursor�s result set the first time FETCH is executed, that it returns each subsequent row of the cursor�s result set each of the subsequent times FETCH is executed, and that it returns a NOT FOUND warning if it is executed after the last row of the cursor�s result set has already been fetched.
 
75
#- 66. Ensure that FETCH <cursor name> fails with an appropriate error message if it is executed before the cursor has been opened.
 
76
#- 67. Ensure that FETCH <cursor name> fails with an appropriate error message if it is executed after the cursor has been closed.
 
77
## 68. Ensure that FETCH <cursor name> fails with an appropriate error message if the number of columns to be fetched does not match the number of variables specified by the FETCH statement.
 
78
#- 69. Ensure that FETCH <cursor name> fails with an appropriate error message if the data type of the column values being fetched are not appropriate for the matching FETCH variables to which the data is being assigned.
 
79
#- 70. Ensure that CLOSE <cursor name> fails unless a cursor with the same name is already open.
 
80
#- 71. Ensure that all cursors are closed when a transaction terminates with a COMMIT statement.
 
81
#- 72. Ensure that all cursors are closed when a transaction terminates with a ROLLBACK statement.
 
82
#- 73. Ensure that the result set of a cursor that has been closed is not longer available to the FETCH statement.
 
83
#- 74. Ensure that every cursor declared within a compound statement is closed when that compound statement ends.
 
84
## 75. Ensure that, for nested compound statements, a cursor that was declared and opened during an outer level of the statement is not closed when an inner level of a compound statement ends.
 
85
## 76. Ensure that all cursors operate asensitively, so that there is no concurrency conflict between cursors operating on the same, or similar, sets of results during execution of one or more stored procedures.
 
86
# 77. Ensure that multiple cursors, nested within multiple compound statements within a stored procedure, always act correctly and return the expected result.
 
87
#
 
88
# ==============================================================================
 
89
let $message= Section 3.1.2 - Syntax checks for the stored procedure-specific
 
90
programming statements BEGIN/END, DECLARE, SET, SELECT/INTO, OPEN, FETCH, CLOSE:;
 
91
--source include/show_msg80.inc
 
92
 
 
93
 
 
94
# ------------------------------------------------------------------------------
 
95
let $message= Testcase 3.1.2.8:;
 
96
--source include/show_msg.inc
 
97
let $message=
 
98
Ensure that the scope of each BEGIN/END compound statement within a stored
 
99
procedure definition is properly applied;
 
100
--source include/show_msg80.inc
 
101
 
 
102
--disable_warnings
 
103
DROP PROCEDURE IF EXISTS sp1;
 
104
--enable_warnings
 
105
 
 
106
delimiter //;
 
107
CREATE PROCEDURE sp1( )
 
108
begin_label: BEGIN
 
109
   declare x char DEFAULT 'x';
 
110
   declare y char DEFAULT 'y';
 
111
   set x = '1';
 
112
   set y = '2';
 
113
   label1: BEGIN
 
114
      declare x char DEFAULT 'X';
 
115
      declare y char DEFAULT 'Y';
 
116
      SELECT f1, f2 into x, y from t2 limit 1;
 
117
      SELECT '1.1', x, y;
 
118
      label2: BEGIN
 
119
         declare x char default 'a';
 
120
         declare y char default 'b';
 
121
         label3: BEGIN
 
122
            declare x char default 'c';
 
123
            declare y char default 'd';
 
124
            label4: BEGIN
 
125
               declare x char default 'e';
 
126
               declare y char default 'f';
 
127
               label5: BEGIN
 
128
                  declare x char default 'g';
 
129
                  declare y char default 'h';
 
130
                  SELECT 5, x, y;
 
131
               END label5;
 
132
               SELECT 4, x, y;
 
133
            END label4;
 
134
            SELECT 3, x, y;
 
135
         END label3;
 
136
         SELECT 2, x, y;
 
137
      END label2;
 
138
   END label1;
 
139
   set @v1 = x;
 
140
   set @v2 = y;
 
141
   SELECT '1.2', @v1, @v2;
 
142
END begin_label//
 
143
delimiter ;//
 
144
 
 
145
CALL sp1();
 
146
 
 
147
#cleanup
 
148
DROP PROCEDURE IF EXISTS sp1;
 
149
 
 
150
 
 
151
# ------------------------------------------------------------------------------
 
152
let $message= Testcase 3.1.2.26:;
 
153
--source include/show_msg.inc
 
154
let $message=
 
155
Ensure that the initial value of every variable declared for a stored procedure
 
156
is either NULL or its DEFAULT value, as appropriate.;
 
157
--source include/show_msg80.inc
 
158
 
 
159
--disable_warnings
 
160
DROP PROCEDURE IF EXISTS sp1;
 
161
--enable_warnings
 
162
 
 
163
set @v1=0;
 
164
set @v2=0;
 
165
 
 
166
delimiter //;
 
167
CREATE PROCEDURE sp1( )
 
168
BEGIN
 
169
   declare x1 char default 'x';
 
170
   declare y1 char;
 
171
   declare x2 tinytext default 'tinytext';
 
172
   declare y2 tinytext;
 
173
   declare x3 datetime default '2005-10-03 12:13:14';
 
174
   declare y3 datetime;
 
175
   declare x4 float default 1.2;
 
176
   declare y4 float;
 
177
   declare x5 blob default 'b';
 
178
   declare y5 blob;
 
179
   declare x6 smallint default 127;
 
180
   declare y6 smallint;
 
181
   SELECT x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6;
 
182
END//
 
183
delimiter ;//
 
184
 
 
185
CALL sp1();
 
186
 
 
187
# cleanup
 
188
DROP PROCEDURE sp1;
 
189
 
 
190
 
 
191
# ------------------------------------------------------------------------------
 
192
let $message= Testcase 3.1.2.30:;
 
193
--source include/show_msg.inc
 
194
let $message=
 
195
Ensure that, when a stored procedure is called/executed, every variable always
 
196
uses the correct value: either the value with which it is initialized or the
 
197
value to which it is subsequently SET or otherwise assigned, as appropriate.;
 
198
--source include/show_msg80.inc
 
199
 
 
200
--disable_warnings
 
201
DROP PROCEDURE IF EXISTS sp1;
 
202
--enable_warnings
 
203
 
 
204
delimiter //;
 
205
CREATE PROCEDURE sp1( IN invar INT, OUT outvar INT )
 
206
BEGIN
 
207
    declare x integer;
 
208
    declare y integer default 1;
 
209
    set @x = x;
 
210
    set @y = y;
 
211
    set @z = 234;
 
212
    SELECT f1, f2 into @x, @y from t2 limit 1;
 
213
    SELECT @x, @y, @z, invar;
 
214
    BEGIN
 
215
      set @x = 2;
 
216
      SELECT @x, @y, @z;
 
217
      SET outvar = @x * invar + @z * @f;
 
218
      SET invar = outvar;
 
219
      BEGIN
 
220
        set @y = null, @z = 'abcd';
 
221
        SELECT @x, @y, @z;
 
222
      END;
 
223
    END;
 
224
END//
 
225
delimiter ;//
 
226
 
 
227
SET @invar  = 100;
 
228
SET @outvar = @invar;
 
229
SET @f      = 10;
 
230
 
 
231
SELECT @x, @y, @z, @invar, @outvar;
 
232
 
 
233
CALL sp1( @invar, @outvar );
 
234
 
 
235
SELECT @x, @y, @z, @invar, @outvar;
 
236
 
 
237
# cleanup
 
238
DROP PROCEDURE sp1;
 
239
 
 
240
 
 
241
# ------------------------------------------------------------------------------
 
242
let $message= Testcase 3.1.2.31:;
 
243
--source include/show_msg.inc
 
244
let $message=
 
245
Ensure that the SELECT ... INTO statement properly assigns values to the
 
246
variables in its variable list.;
 
247
--source include/show_msg80.inc
 
248
# also tested in a lot of other testcases
 
249
 
 
250
--disable_warnings
 
251
DROP PROCEDURE IF EXISTS sp1;
 
252
--enable_warnings
 
253
 
 
254
delimiter //;
 
255
CREATE PROCEDURE sp1( )
 
256
BEGIN
 
257
   declare x integer; declare y integer;
 
258
   set @x=x;
 
259
   set @y=y;
 
260
   SELECT f4, f3 into @x, @y from t2 limit 1;
 
261
   SELECT @x, @y;
 
262
END//
 
263
delimiter ;//
 
264
 
 
265
CALL sp1();
 
266
 
 
267
# cleanup 3.1.2.31
 
268
DROP PROCEDURE sp1;
 
269
 
 
270
 
 
271
# ------------------------------------------------------------------------------
 
272
let $message= Testcase 3.1.2.32:;
 
273
--source include/show_msg.inc
 
274
let $message=
 
275
Ensure that a SELECT ... INTO statement that retrieves multiple rows is
 
276
rejected, with an appropriate error message.;
 
277
--source include/show_msg80.inc
 
278
 
 
279
--disable_warnings
 
280
DROP PROCEDURE IF EXISTS sp1;
 
281
--enable_warnings
 
282
 
 
283
delimiter //;
 
284
CREATE PROCEDURE sp1( )
 
285
BEGIN
 
286
   declare x integer; declare y integer;
 
287
   set @x=x;
 
288
   set @y=y;
 
289
   SELECT f4, f3 into @x, @y from t2;
 
290
END//
 
291
delimiter ;//
 
292
 
 
293
#Error: 1172 SQLSTATE: 42000 (ER_TOO_MANY_ROWS) Message: Result consisted of more than one row
 
294
--error 1172
 
295
CALL sp1();
 
296
 
 
297
# cleanup 3.1.2.32
 
298
DROP PROCEDURE sp1;
 
299
 
 
300
 
 
301
# ------------------------------------------------------------------------------
 
302
let $message= Testcase 3.1.2.33:;
 
303
--source include/show_msg.inc
 
304
let $message=
 
305
Ensure that a SELECT ... INTO statement that retrieves too many columns for the
 
306
number of variables in its variable list is rejected, with an appropriate error
 
307
message.;
 
308
--source include/show_msg80.inc
 
309
 
 
310
--disable_warnings
 
311
DROP PROCEDURE IF EXISTS sp1;
 
312
--enable_warnings
 
313
 
 
314
delimiter //;
 
315
CREATE PROCEDURE sp1( )
 
316
BEGIN
 
317
    declare x integer; declare y integer;
 
318
    set @x=x;
 
319
    set @y=y;
 
320
    SELECT f4, f3, f2, f1 into @x, @y from t2;
 
321
END//
 
322
delimiter ;//
 
323
 
 
324
--error 1222
 
325
CALL sp1();
 
326
 
 
327
# cleanup 3.1.2.33
 
328
DROP PROCEDURE sp1;
 
329
 
 
330
 
 
331
# ------------------------------------------------------------------------------
 
332
let $message= Testcase 3.1.2.34:;
 
333
--source include/show_msg.inc
 
334
let $message=
 
335
Ensure that a SELECT ... INTO statement that retrieves too few columns for the
 
336
number of variables in its variable list is rejected, with an appropriate error
 
337
message.;
 
338
--source include/show_msg80.inc
 
339
 
 
340
--disable_warnings
 
341
DROP PROCEDURE IF EXISTS sp1;
 
342
--enable_warnings
 
343
 
 
344
delimiter //;
 
345
CREATE PROCEDURE sp1( )
 
346
BEGIN
 
347
    declare x integer; declare y integer; declare z integer;
 
348
    set @x=x;
 
349
    set @y=y;
 
350
    set @z=z;
 
351
    SELECT f4 into @x, @y, @z from t2;
 
352
END//
 
353
delimiter ;//
 
354
 
 
355
--error 1222
 
356
CALL sp1();
 
357
 
 
358
# cleanup 3.1.2.34
 
359
DROP PROCEDURE sp1;
 
360
 
 
361
 
 
362
# ------------------------------------------------------------------------------
 
363
let $message= Testcase 3.1.2.38:;
 
364
--source include/show_msg.inc
 
365
let $message=
 
366
Ensure that the scope of every condition declared is properly applied.;
 
367
--source include/show_msg80.inc
 
368
 
 
369
--disable_warnings
 
370
DROP PROCEDURE IF EXISTS h1;
 
371
DROP TABLE IF EXISTS res_t1;
 
372
--enable_warnings
 
373
 
 
374
create table res_t1(w char unique, x char);
 
375
 
 
376
insert into res_t1 values('a', 'b');
 
377
 
 
378
# Error: 1339 SQLSTATE: 20000 (ER_SP_CASE_NOT_FOUND) Message: Case not found for CASE statement
 
379
# Error: 1022 SQLSTATE: 23000 (ER_DUP_KEY) Message: Can't write; duplicate key in table '%s'
 
380
 
 
381
delimiter //;
 
382
CREATE PROCEDURE h1 ()
 
383
BEGIN
 
384
   declare x1, x2, x3, x4, x5, x6 int default 0;
 
385
   SELECT '-1-', x1, x2, x3, x4, x5, x6;
 
386
   BEGIN
 
387
      declare condname condition for sqlstate '23000';
 
388
      declare continue handler for condname set x5 = 1;
 
389
      set x6 = 0;
 
390
      insert into res_t1 values ('a', 'b');
 
391
      set x6 = 1;
 
392
      SELECT '-2-', x1, x2, x3, x4, x5, x6;
 
393
   END;
 
394
   begin1_label: BEGIN
 
395
      BEGIN
 
396
         declare condname condition for sqlstate '20000';
 
397
         declare continue handler for condname set x1 = 1;
 
398
         set x2 = 0;
 
399
         case x2
 
400
            when 1 then set x2=10;
 
401
            when 2 then set x2=11;
 
402
         END case;
 
403
         set x2 = 1;
 
404
         SELECT '-3-', x1, x2, x3, x4, x5, x6;
 
405
         begin2_label: BEGIN
 
406
            BEGIN
 
407
               declare condname condition for sqlstate '23000';
 
408
               declare exit handler for condname set x3 = 1;
 
409
               set x4= 1;
 
410
               SELECT '-4a', x1, x2, x3, x4, x5, x6;
 
411
               insert into res_t1 values ('a', 'b');
 
412
               set x4= 2;
 
413
               SELECT '-4b', x1, x2, x3, x4, x5, x6;
 
414
            END;
 
415
            SELECT '-5-', x1, x2, x3, x4, x5, x6;
 
416
         END begin2_label;
 
417
         SELECT '-6-', x1, x2, x3, x4, x5, x6;
 
418
      END;
 
419
      SELECT '-7-', x1, x2, x3, x4, x5, x6;
 
420
   END begin1_label;
 
421
   SELECT 'END', x1, x2, x3, x4, x5, x6;
 
422
END//
 
423
delimiter ;//
 
424
 
 
425
CALL h1();
 
426
 
 
427
# and a 2nd test
 
428
--disable_warnings
 
429
DROP TABLE IF EXISTS tnull;
 
430
DROP PROCEDURE IF EXISTS sp1;
 
431
--enable_warnings
 
432
 
 
433
CREATE TABLE tnull(f1 int);
 
434
 
 
435
delimiter //;
 
436
CREATE PROCEDURE sp1()
 
437
BEGIN
 
438
    declare cond1 condition for sqlstate '42S02';
 
439
    declare continue handler for cond1 set @var2 = 1;
 
440
    BEGIN
 
441
      declare cond1 condition for sqlstate '23000';
 
442
      declare continue handler for cond1 set @var2 = 1;
 
443
    END;
 
444
          insert into tnull values(1);
 
445
END//
 
446
delimiter ;//
 
447
 
 
448
CALL sp1();
 
449
 
 
450
# cleanup 3.1.2.38
 
451
DROP PROCEDURE h1;
 
452
drop table res_t1;
 
453
DROP PROCEDURE sp1;
 
454
DROP TABLE tnull;
 
455
 
 
456
 
 
457
# ------------------------------------------------------------------------------
 
458
let $message= Testcase 3.1.2.43:;
 
459
--source include/show_msg.inc
 
460
let $message=
 
461
Ensure that the DECLARE ... HANDLER FOR statement can not declare any handler
 
462
for a condition declared outside of the scope of the handler.;
 
463
--source include/show_msg80.inc
 
464
 
 
465
--disable_warnings
 
466
DROP PROCEDURE IF EXISTS h1;
 
467
DROP PROCEDURE IF EXISTS h2;
 
468
drop table IF EXISTS res_t1;
 
469
--enable_warnings
 
470
 
 
471
create table res_t1(w char unique, x char);
 
472
insert into res_t1 values ('a', 'b');
 
473
 
 
474
delimiter //;
 
475
--error 1319
 
476
CREATE PROCEDURE h1 ()
 
477
BEGIN
 
478
   declare x1, x2, x3, x4, x5, x6 int default 0;
 
479
   BEGIN
 
480
      declare cond_1 condition for sqlstate '23000';
 
481
      declare continue handler for cond_1 set x5 = 1;
 
482
      BEGIN
 
483
         declare cond_2 condition for sqlstate '20000';
 
484
         declare continue handler for cond_1 set x1 = 1;
 
485
         BEGIN
 
486
            declare continue handler for cond_2 set x3 = 1;
 
487
            set x2 = 1;
 
488
         END;
 
489
         set x6 = 0;
 
490
      END;
 
491
      BEGIN
 
492
         declare continue handler for cond_1 set x1 = 1;
 
493
         BEGIN
 
494
            declare continue handler for cond_2 set x3 = 1;
 
495
            set x2 = 1;
 
496
         END;
 
497
         set x6 = 0;
 
498
      END;
 
499
   END;
 
500
   SELECT x1, x2, x3, x4, x5, x6;
 
501
END//
 
502
 
 
503
CREATE PROCEDURE h2 ()
 
504
BEGIN
 
505
   declare x1, x2, x3, x4, x5, x6 int default 0;
 
506
   BEGIN
 
507
      declare condname condition for sqlstate '23000';
 
508
      declare continue handler for condname set x5 = 1;
 
509
      BEGIN
 
510
         declare condname condition for sqlstate '20000';
 
511
         declare continue handler for condname set x1 = 1;
 
512
         BEGIN
 
513
            declare condname condition for sqlstate '42000';
 
514
            declare continue handler for condname set x3 = 1;
 
515
            set x6 = 0;
 
516
            insert into res_t1 values ('a', 'b');
 
517
            set x6 = 1;
 
518
            set x4= 0;
 
519
            CALL sp1();
 
520
            set x4= 1;
 
521
            set x2 = 0;
 
522
            case x2
 
523
               when 1 then set x2=10;
 
524
               when 2 then set x2=11;
 
525
            END case;
 
526
            set x2 = 1;
 
527
         END;
 
528
         set x2 = 0;
 
529
         case x2
 
530
            when 1 then set x2=10;
 
531
            when 2 then set x2=11;
 
532
         END case;
 
533
         set x2 = 1;
 
534
         set x6 = 0;
 
535
         insert into res_t1 values ('a', 'b');
 
536
         set x6 = 1;
 
537
      END;
 
538
   END;
 
539
   SELECT x1, x2, x3, x4, x5, x6;
 
540
END//
 
541
delimiter ;//
 
542
 
 
543
CALL h2();
 
544
SELECT * FROM res_t1;
 
545
 
 
546
# cleanup 3.1.2.43
 
547
DROP PROCEDURE h2;
 
548
drop table res_t1;
 
549
 
 
550
 
 
551
# ------------------------------------------------------------------------------
 
552
let $message= Testcase 3.1.2.44:;
 
553
--source include/show_msg.inc
 
554
let $message=
 
555
Ensure that the DECLARE ... HANDLER FOR statement cannot declare a handler for
 
556
any invalid, or undeclared, condition.;
 
557
--source include/show_msg80.inc
 
558
 
 
559
--disable_warnings
 
560
DROP PROCEDURE IF EXISTS h1;
 
561
--enable_warnings
 
562
 
 
563
delimiter //;
 
564
#Error: 1319 SQLSTATE: 42000 (ER_SP_COND_MISMATCH) Message: Undefined CONDITION: %s
 
565
--error 1319
 
566
CREATE PROCEDURE h1 ()
 
567
BEGIN
 
568
   declare x1, x2, x3, x4, x5, x6 int default 0;
 
569
   BEGIN
 
570
      declare condname1 condition for sqlstate '23000';
 
571
      BEGIN
 
572
         declare condname2 condition for sqlstate '20000';
 
573
         declare continue handler for condname1 set x3 = 1;
 
574
         declare continue handler for condname2 set x1 = 1;
 
575
      END;
 
576
   END;
 
577
   BEGIN
 
578
      declare condname3 condition for sqlstate '42000';
 
579
      declare continue handler for condname1 set x3 = 1;
 
580
      declare continue handler for condname2 set x5 = 1;
 
581
      declare continue handler for condname3 set x1 = 1;
 
582
   END;
 
583
END//
 
584
 
 
585
#Error: 1064 SQLSTATE: 42000 (ER_PARSE_ERROR) Message: %s near '%s' at line %d
 
586
--error 1064
 
587
CREATE PROCEDURE h1 ()
 
588
BEGIN
 
589
   DECLARE x1 INT DEFAULT 0;
 
590
   BEGIN
 
591
      DECLARE condname1 CONDITION CHECK SQLSTATE '23000';
 
592
   END;
 
593
   DECLARE CONTINUE HANDLER FOR condname1 SET x1 = 1;
 
594
END//
 
595
 
 
596
#Error: 1407 SQLSTATE: 42000 (ER_SP_BAD_SQLSTATE) Message: Bad SQLSTATE: '%s'
 
597
--error 1407
 
598
CREATE PROCEDURE h1 ()
 
599
BEGIN
 
600
   DECLARE x1 INT DEFAULT 0;
 
601
   BEGIN
 
602
      DECLARE condname1 CONDITION FOR SQLSTATE 'qwert';
 
603
   END;
 
604
   DECLARE CONTINUE HANDLER FOR condname1 SET x1 = 1;
 
605
END//
 
606
delimiter ;//
 
607
 
 
608
# cleanup 3.1.2.44
 
609
#DROP PROCEDURE h1;
 
610
 
 
611
 
 
612
# ------------------------------------------------------------------------------
 
613
let $message= Testcase 3.1.2.45 + 3.1.2.50:;
 
614
--source include/show_msg.inc
 
615
let $message=
 
616
45. Ensure that the scope of every handler declared is properly applied.
 
617
50. Ensure that a CONTINUE handler allows the execution of the stored procedure
 
618
.   to continue once the handler statement has completed its own execution (that
 
619
.   is, once the handler action statement has been executed).;
 
620
--source include/show_msg80.inc
 
621
 
 
622
# RefMan: For an EXIT handler, execution of the current BEGIN...END compound
 
623
#         statement is terminated.
 
624
 
 
625
--disable_warnings
 
626
DROP PROCEDURE IF EXISTS p1;
 
627
DROP PROCEDURE IF EXISTS p1undo;
 
628
DROP PROCEDURE IF EXISTS h1;
 
629
DROP PROCEDURE IF EXISTS sp1;
 
630
drop table IF EXISTS res_t1;
 
631
--enable_warnings
 
632
 
 
633
--echo ==> 'UNDO' is still not supported.
 
634
delimiter //;
 
635
--error 1064
 
636
create procedure p1undo ()
 
637
begin
 
638
   declare undo handler for sqlexception select '1';
 
639
   select * from tqq;
 
640
   SELECT 'end of 1';
 
641
end;//
 
642
 
 
643
create procedure p1 ()
 
644
begin
 
645
   declare exit handler for sqlexception select 'exit handler 1';
 
646
   begin
 
647
      declare exit handler for sqlexception select 'exit handler 2';
 
648
      begin
 
649
         declare continue handler for sqlexception select 'continue handler 3';
 
650
         drop table if exists tqq;
 
651
         select * from tqq;
 
652
         SELECT 'end of BEGIN/END 3';
 
653
      end;
 
654
      drop table if exists tqq;
 
655
      select * from tqq;
 
656
      SELECT 'end of BEGIN/END 2';
 
657
   end;
 
658
   select * from tqq;
 
659
   SELECT 'end of BEGIN/END 1';
 
660
end;//
 
661
 
 
662
call p1()//
 
663
delimiter ;//
 
664
 
 
665
create table res_t1(w char unique, x char);
 
666
insert into res_t1 values ('a', 'b');
 
667
 
 
668
delimiter //;
 
669
CREATE PROCEDURE h1 ()
 
670
BEGIN
 
671
   declare x1, x2, x3, x4, x5, x6 int default 0;
 
672
   BEGIN
 
673
      declare continue handler for sqlstate '23000' set x5 = 1;
 
674
      insert into res_t1 values ('a', 'b');
 
675
      set x6 = 1;
 
676
   END;
 
677
   begin1_label: BEGIN
 
678
      BEGIN
 
679
         declare continue handler for sqlstate '23000' set x1 = 1;
 
680
         insert into res_t1 values ('a', 'b');
 
681
         set x2 = 1;
 
682
         begin2_label: BEGIN
 
683
            BEGIN
 
684
               declare exit handler for sqlstate '23000' set x3 = 1;
 
685
               set x4= 1;
 
686
               insert into res_t1 values ('a', 'b');
 
687
               set x4= 0;
 
688
            END;
 
689
         END begin2_label;
 
690
      END;
 
691
   END begin1_label;
 
692
   SELECT x1, x2, x3, x4, x5, x6;
 
693
END//
 
694
delimiter ;//
 
695
 
 
696
CALL h1();
 
697
 
 
698
 
 
699
delimiter //;
 
700
CREATE PROCEDURE sp1()
 
701
   begin1_label:BEGIN
 
702
      declare exit handler for sqlstate '00000' set @var1 = 5;
 
703
      set @var2 = 6;
 
704
      begin2_label:BEGIN
 
705
         declare continue handler for sqlstate '00000' set @var3 = 7;
 
706
         set @var4 = 8;
 
707
         SELECT @var3, @var4;
 
708
      END begin2_label;
 
709
      SELECT @var1, @var2;
 
710
   END begin1_label//
 
711
delimiter ;//
 
712
 
 
713
CALL sp1();
 
714
 
 
715
# cleanup 3.1.2.45+50
 
716
DROP PROCEDURE p1;
 
717
DROP PROCEDURE h1;
 
718
DROP PROCEDURE sp1;
 
719
DROP TABLE res_t1;
 
720
 
 
721
 
 
722
# ------------------------------------------------------------------------------
 
723
let $message= Testcase 3.1.2.50:;
 
724
--source include/show_msg.inc
 
725
 
 
726
# testcase: ensure that a continue handler allows the execution of the stored procedure
 
727
#            to continue once the handler statement has completed its own execution
 
728
#            (that is, once the handler action statement has been executed).
 
729
 
 
730
 
 
731
--disable_warnings
 
732
DROP PROCEDURE IF EXISTS sp1;
 
733
DROP PROCEDURE IF EXISTS sp2;
 
734
--enable_warnings
 
735
 
 
736
delimiter //;
 
737
CREATE PROCEDURE sp1 (x int, y int)
 
738
BEGIN
 
739
    set @y=0;
 
740
END//
 
741
delimiter ;//
 
742
 
 
743
delimiter //;
 
744
CREATE PROCEDURE sp2 ()
 
745
BEGIN
 
746
   declare continue handler for sqlstate '42000' set @x2 = 1;
 
747
   set @x=1;
 
748
   SELECT @x2;
 
749
   CALL sp1(1);
 
750
   set @x=2;
 
751
   SELECT @x2, @x;
 
752
END//
 
753
delimiter ;//
 
754
 
 
755
CALL sp2();
 
756
 
 
757
# cleanup
 
758
DROP PROCEDURE sp1;
 
759
DROP PROCEDURE sp2;
 
760
 
 
761
 
 
762
# ------------------------------------------------------------------------------
 
763
let $message= Testcase 3.2.2.51:;
 
764
--source include/show_msg.inc
 
765
let $message=
 
766
Ensure that an EXIT handler causes the execution of the stored procedure to
 
767
terminate, within its scope, once the handler action statement has been
 
768
executed.;
 
769
--source include/show_msg80.inc
 
770
# also tested in 3.1.2.45
 
771
 
 
772
--disable_warnings
 
773
DROP PROCEDURE IF EXISTS sp1;
 
774
DROP PROCEDURE IF EXISTS sp2;
 
775
--enable_warnings
 
776
 
 
777
delimiter //;
 
778
CREATE PROCEDURE sp1 (x int, y int)
 
779
BEGIN
 
780
    set @x=0;
 
781
END//
 
782
delimiter ;//
 
783
 
 
784
delimiter //;
 
785
CREATE PROCEDURE sp2 ()
 
786
BEGIN
 
787
   declare exit handler for sqlstate '42000' set @x2 = 1;
 
788
   set @x2=0;
 
789
   set @x=1;
 
790
   SELECT '-1-', @x2, @x;
 
791
   CALL sp1(1);
 
792
   SELECT '-2-', @x2, @x;
 
793
   set @x=2;
 
794
END//
 
795
delimiter ;//
 
796
 
 
797
# Error: 1318 SQLSTATE: 42000 (ER_SP_WRONG_NO_OF_ARGS) Message: Incorrect number of arguments for %s %s; expected %u, got %u
 
798
--error 1318
 
799
CALL sp1(1);
 
800
CALL sp2();
 
801
SELECT '-3-', @x2, @x;
 
802
 
 
803
# cleanup 3.1.2.51
 
804
DROP PROCEDURE sp1;
 
805
DROP PROCEDURE sp2;
 
806
 
 
807
 
 
808
# ------------------------------------------------------------------------------
 
809
let $message= Testcase 3.1.2.52:;
 
810
--source include/show_msg.inc
 
811
let $message=
 
812
Ensure that an EXIT handler does not cause the execution of the stored procedure
 
813
to terminate outside of its scope.;
 
814
--source include/show_msg80.inc
 
815
# tested also above in
 
816
 
 
817
--disable_warnings
 
818
DROP PROCEDURE IF EXISTS sp1;
 
819
DROP PROCEDURE IF EXISTS sp2;
 
820
--enable_warnings
 
821
 
 
822
delimiter //;
 
823
CREATE PROCEDURE sp1 (x int, y int)
 
824
BEGIN
 
825
    set @x=0;
 
826
END//
 
827
delimiter ;//
 
828
 
 
829
delimiter //;
 
830
CREATE PROCEDURE sp2()
 
831
BEGIN
 
832
   declare continue handler for sqlstate '42000' set @x2 = 2;
 
833
   set @x2 = 1;
 
834
   set @x =20;
 
835
   SELECT '-1-', @x2, @x;
 
836
   BEGIN
 
837
      declare exit handler for sqlstate '42000' set @x2 = 11;
 
838
      SELECT '-2-', @x2, @x;
 
839
      CALL sp1(1);
 
840
      SELECT '-3a', @x2, @x;
 
841
      set @x=21;
 
842
      SELECT '-3b', @x2, @x;
 
843
   END;
 
844
   set @x=22;
 
845
   SELECT '-4-', @x2, @x;
 
846
END//
 
847
delimiter ;//
 
848
 
 
849
CALL sp2();
 
850
 
 
851
# cleanup 3.1.2.52
 
852
DROP PROCEDURE sp1;
 
853
DROP PROCEDURE sp2;
 
854
 
 
855
 
 
856
# ------------------------------------------------------------------------------
 
857
let $message= Testcase 3.1.2.54:;
 
858
--source include/show_msg.inc
 
859
let $message=
 
860
Ensure that a handler with a condition defined with an SQLSTATE that begins with
 
861
�01� is always exactly equivalent in action to a handler with an SQLWARNING
 
862
condition.;
 
863
--source include/show_msg80.inc
 
864
 
 
865
--disable_warnings
 
866
DROP PROCEDURE IF EXISTS sp0;
 
867
DROP PROCEDURE IF EXISTS sp1;
 
868
DROP PROCEDURE IF EXISTS sp2;
 
869
DROP PROCEDURE IF EXISTS sp3;
 
870
DROP PROCEDURE IF EXISTS sp4;
 
871
DROP TABLE IF EXISTS temp;
 
872
--enable_warnings
 
873
 
 
874
CREATE TABLE temp( f1 CHAR, f2 CHAR);
 
875
 
 
876
delimiter //;
 
877
# 0 - without handler
 
878
CREATE PROCEDURE sp0()
 
879
BEGIN
 
880
   set @done=0;
 
881
   set @x=0;
 
882
   insert into temp values('xxx', 'yy');
 
883
   set @x=1;
 
884
END//
 
885
 
 
886
# 1st one with SQLSTATE + CONTINUE
 
887
CREATE PROCEDURE sp1()
 
888
BEGIN
 
889
   declare continue handler for sqlstate '01000' set @done = 1;
 
890
   set @done=0;
 
891
   set @x=0;
 
892
   insert into temp values('xxx', 'yy');
 
893
   set @x=1;
 
894
END//
 
895
 
 
896
# 2nd one with SQLWARNING + CONTINUE
 
897
CREATE PROCEDURE sp2()
 
898
BEGIN
 
899
   declare continue handler for sqlwarning set @done = 1;
 
900
   set @done=0;
 
901
   set @x=0;
 
902
   insert into temp values('xxx', 'yy');
 
903
   set @x=1;
 
904
END//
 
905
 
 
906
# 3 with SQLSTATE + EXIT
 
907
CREATE PROCEDURE sp3()
 
908
BEGIN
 
909
   declare exit handler for sqlstate '01000' set @done = 1;
 
910
   set @done=0;
 
911
   set @x=0;
 
912
   insert into temp values('xxx', 'yy');
 
913
   set @x=1;
 
914
END//
 
915
 
 
916
# 4 with SQLWARNING + EXIT
 
917
CREATE PROCEDURE sp4()
 
918
BEGIN
 
919
   declare exit handler for sqlwarning set @done = 1;
 
920
   set @done=0;
 
921
   set @x=0;
 
922
   insert into temp values('xxx', 'yy');
 
923
   set @x=1;
 
924
END//
 
925
delimiter ;//
 
926
 
 
927
INSERT INTO temp VALUES('0', NULL);
 
928
CALL sp0();
 
929
SELECT @done, @x;
 
930
 
 
931
INSERT INTO temp VALUES('1', NULL);
 
932
CALL sp1();
 
933
SELECT @done, @x;
 
934
 
 
935
INSERT INTO temp VALUES('2', NULL);
 
936
CALL sp2();
 
937
SELECT @done, @x;
 
938
 
 
939
INSERT INTO temp VALUES('3', NULL);
 
940
CALL sp3();
 
941
SELECT @done, @x;
 
942
 
 
943
INSERT INTO temp VALUES('4', NULL);
 
944
CALL sp4();
 
945
SELECT @done, @x;
 
946
 
 
947
SELECT * FROM temp;
 
948
 
 
949
# cleanup 3.1.2.54
 
950
DROP PROCEDURE sp1;
 
951
DROP PROCEDURE sp2;
 
952
DROP PROCEDURE sp3;
 
953
DROP PROCEDURE sp4;
 
954
DROP TABLE temp;
 
955
 
 
956
 
 
957
# ------------------------------------------------------------------------------
 
958
let $message= Testcase 3.1.2.56:;
 
959
--source include/show_msg.inc
 
960
let $message=
 
961
Ensure that a handler with a condition defined with an SQLSTATE that begins with
 
962
�02� is always exactly equivalent in action to a handler with a NOT FOUND
 
963
condition.;
 
964
--source include/show_msg80.inc
 
965
 
 
966
--disable_warnings
 
967
DROP PROCEDURE IF EXISTS sp0;
 
968
DROP PROCEDURE IF EXISTS sp1;
 
969
DROP PROCEDURE IF EXISTS sp2;
 
970
DROP PROCEDURE IF EXISTS sp3;
 
971
DROP PROCEDURE IF EXISTS sp4;
 
972
--enable_warnings
 
973
 
 
974
delimiter //;
 
975
# 0 - wihtout handler
 
976
CREATE PROCEDURE sp0()
 
977
BEGIN
 
978
   DECLARE f1_value CHAR(20);
 
979
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
980
   SET @done = 0;
 
981
   SET @x = 0;
 
982
   OPEN cur1;
 
983
   FETCH cur1 INTO f1_value;
 
984
   SET @x = 1;
 
985
   FETCH cur1 INTO f1_value;
 
986
   SET @x = 2;
 
987
   CLOSE cur1;
 
988
END//
 
989
 
 
990
# 1st one with SQLSTATE + CONTINUE
 
991
CREATE PROCEDURE sp1()
 
992
BEGIN
 
993
   DECLARE f1_value CHAR(20);
 
994
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
995
   declare continue handler for sqlstate '02000' set @done = 1;
 
996
   SET @done = 0;
 
997
   SET @x = 0;
 
998
   OPEN cur1;
 
999
   FETCH cur1 INTO f1_value;
 
1000
   SET @x = 1;
 
1001
   FETCH cur1 INTO f1_value;
 
1002
   SET @x = 2;
 
1003
   CLOSE cur1;
 
1004
END//
 
1005
 
 
1006
# 2nd one with NOT FOUND + CONTINUE
 
1007
CREATE PROCEDURE sp2()
 
1008
BEGIN
 
1009
   DECLARE f1_value CHAR(20);
 
1010
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1011
   declare continue handler for not found set @done = 1;
 
1012
   SET @done = 0;
 
1013
   SET @x = 0;
 
1014
   OPEN cur1;
 
1015
   FETCH cur1 INTO f1_value;
 
1016
   SET @x = 1;
 
1017
   FETCH cur1 INTO f1_value;
 
1018
   SET @x = 2;
 
1019
   CLOSE cur1;
 
1020
END//
 
1021
 
 
1022
# 3 with SQLSTATE + EXIT
 
1023
CREATE PROCEDURE sp3()
 
1024
BEGIN
 
1025
   DECLARE f1_value CHAR(20);
 
1026
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1027
   declare exit handler for sqlstate '02000' set @done = 1;
 
1028
   SET @done = 0;
 
1029
   SET @x = 0;
 
1030
   OPEN cur1;
 
1031
   FETCH cur1 INTO f1_value;
 
1032
   SET @x = 1;
 
1033
   FETCH cur1 INTO f1_value;
 
1034
   SET @x = 2;
 
1035
   CLOSE cur1;
 
1036
END//
 
1037
 
 
1038
# 4 with NOT FOUND + EXIT
 
1039
CREATE PROCEDURE sp4()
 
1040
BEGIN
 
1041
   DECLARE f1_value CHAR(20);
 
1042
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1043
   declare exit handler for not found set @done = 1;
 
1044
   SET @done = 0;
 
1045
   SET @x = 0;
 
1046
   OPEN cur1;
 
1047
   FETCH cur1 INTO f1_value;
 
1048
   SET @x = 1;
 
1049
   FETCH cur1 INTO f1_value;
 
1050
   SET @x = 2;
 
1051
   CLOSE cur1;
 
1052
END//
 
1053
delimiter ;//
 
1054
 
 
1055
--error 1329
 
1056
CALL sp0();
 
1057
SELECT @done, @x;
 
1058
 
 
1059
CALL sp1();
 
1060
SELECT @done, @x;
 
1061
 
 
1062
CALL sp2();
 
1063
SELECT @done, @x;
 
1064
 
 
1065
CALL sp3();
 
1066
SELECT @done, @x;
 
1067
 
 
1068
CALL sp4();
 
1069
SELECT @done, @x;
 
1070
 
 
1071
# cleanup 3.1.2.56
 
1072
DROP PROCEDURE sp0;
 
1073
DROP PROCEDURE sp1;
 
1074
DROP PROCEDURE sp2;
 
1075
DROP PROCEDURE sp3;
 
1076
DROP PROCEDURE sp4;
 
1077
 
 
1078
 
 
1079
# ------------------------------------------------------------------------------
 
1080
let $message= Testcase 3.1.2.58:;
 
1081
--source include/show_msg.inc
 
1082
let $message=
 
1083
Ensure that a handler with a condition defined with an SQLSTATE that begins with
 
1084
anything other that �01� or �02� is always exactly equivalent in action to a
 
1085
handler with an SQLEXCEPTION condition.;
 
1086
--source include/show_msg80.inc
 
1087
 
 
1088
# Error: 1339 SQLSTATE: 20000 (ER_SP_CASE_NOT_FOUND) Message: Case not found for CASE statement
 
1089
# Error: 1222 SQLSTATE: 21000 (ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT) Message: The used SELECT statements have a different number of columns
 
1090
# Error: 1326 SQLSTATE: 24000 (ER_SP_CURSOR_NOT_OPEN) Message: Cursor is not open
 
1091
 
 
1092
--disable_warnings
 
1093
DROP PROCEDURE IF EXISTS sp0;
 
1094
DROP PROCEDURE IF EXISTS sp1;
 
1095
DROP PROCEDURE IF EXISTS sp2;
 
1096
DROP PROCEDURE IF EXISTS sp3;
 
1097
DROP PROCEDURE IF EXISTS sp4;
 
1098
--enable_warnings
 
1099
 
 
1100
delimiter //;
 
1101
# 0 - without handler
 
1102
CREATE PROCEDURE sp0()
 
1103
BEGIN
 
1104
   DECLARE f1_value CHAR(20);
 
1105
   DECLARE cv INT DEFAULT 0;
 
1106
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1107
   SET @x = 1;
 
1108
   CASE cv
 
1109
      WHEN 2 THEN SET @x = 2;
 
1110
      WHEN 3 THEN SET @x = 3;
 
1111
   END case;
 
1112
   SET @x = 4;
 
1113
   SELECT f1, f2 FROM t2
 
1114
   UNION
 
1115
   SELECT f1, f2,3 FROM t2;
 
1116
   SET @x = 5;
 
1117
   FETCH cur1 INTO f1_value;
 
1118
   SET @x = 6;
 
1119
END//
 
1120
 
 
1121
# 1 - SQLSTATEs - CONTINUE
 
1122
CREATE PROCEDURE sp1()
 
1123
BEGIN
 
1124
   DECLARE f1_value CHAR(20);
 
1125
   DECLARE cv INT DEFAULT 0;
 
1126
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1127
   DECLARE continue HANDLER FOR SQLSTATE '20000' SELECT '20000' AS 'SQLSTATE';
 
1128
   DECLARE continue HANDLER FOR SQLSTATE '21000' SELECT '21000' AS 'SQLSTATE';
 
1129
   DECLARE continue HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE';
 
1130
   SET @x = 1;
 
1131
   CASE cv
 
1132
      WHEN 2 THEN SET @x = 2;
 
1133
      WHEN 3 THEN SET @x = 3;
 
1134
   END case;
 
1135
   SET @x = 4;
 
1136
   SELECT f1, f2 FROM t2
 
1137
   UNION
 
1138
   SELECT f1, f2,3 FROM t2;
 
1139
   SET @x = 5;
 
1140
   FETCH cur1 INTO f1_value;
 
1141
   SET @x = 6;
 
1142
END//
 
1143
 
 
1144
# 2 - SQLEXCEPTION matches 2 of 3 conditions - CONTINUE
 
1145
CREATE PROCEDURE sp2()
 
1146
BEGIN
 
1147
   DECLARE f1_value CHAR(20);
 
1148
   DECLARE cv INT DEFAULT 0;
 
1149
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1150
   DECLARE continue HANDLER FOR SQLEXCEPTION SELECT 'SQLEXCEPTION' AS 'SQLSTATE';
 
1151
   DECLARE continue HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE';
 
1152
   SET @x = 1;
 
1153
   CASE cv
 
1154
      WHEN 2 THEN SET @x = 2;
 
1155
      WHEN 3 THEN SET @x = 3;
 
1156
   END case;
 
1157
   SET @x = 4;
 
1158
   SELECT f1, f2 FROM t2
 
1159
   UNION
 
1160
   SELECT f1, f2,3 FROM t2;
 
1161
   SET @x = 5;
 
1162
   FETCH cur1 INTO f1_value;
 
1163
   SET @x = 6;
 
1164
END//
 
1165
 
 
1166
# 3 - SQLSTATEs - EXIT
 
1167
CREATE PROCEDURE sp3()
 
1168
BEGIN
 
1169
   DECLARE f1_value CHAR(20);
 
1170
   DECLARE cv INT DEFAULT 0;
 
1171
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1172
   DECLARE EXIT HANDLER FOR SQLSTATE '20000' SELECT '20000' AS 'SQLSTATE';
 
1173
   DECLARE EXIT HANDLER FOR SQLSTATE '21000' SELECT '21000' AS 'SQLSTATE';
 
1174
   DECLARE EXIT HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE';
 
1175
   SET @x = 1;
 
1176
   CASE cv
 
1177
      WHEN 2 THEN SET @x = 2;
 
1178
      WHEN 3 THEN SET @x = 3;
 
1179
   END case;
 
1180
   SET @x = 4;
 
1181
   SELECT f1, f2 FROM t2
 
1182
   UNION
 
1183
   SELECT f1, f2,3 FROM t2;
 
1184
   SET @x = 5;
 
1185
   FETCH cur1 INTO f1_value;
 
1186
   SET @x = 6;
 
1187
END//
 
1188
 
 
1189
# 4 - SQLEXCEPTION matches 2 of 3 conditions - EXIT
 
1190
CREATE PROCEDURE sp4()
 
1191
BEGIN
 
1192
   DECLARE f1_value CHAR(20);
 
1193
   DECLARE cv INT DEFAULT 0;
 
1194
   DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1;
 
1195
   DECLARE EXIT HANDLER FOR SQLEXCEPTION SELECT 'SQLEXCEPTION' AS 'SQLSTATE';
 
1196
   DECLARE EXIT HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE';
 
1197
   SET @x = 1;
 
1198
   CASE cv
 
1199
      WHEN 2 THEN SET @x = 2;
 
1200
      WHEN 3 THEN SET @x = 3;
 
1201
   END case;
 
1202
   SET @x = 4;
 
1203
   SELECT f1, f2 FROM t2
 
1204
   UNION
 
1205
   SELECT f1, f2,3 FROM t2;
 
1206
   SET @x = 5;
 
1207
   FETCH cur1 INTO f1_value;
 
1208
   SET @x = 6;
 
1209
   CLOSE cur1;
 
1210
END//
 
1211
delimiter ;//
 
1212
 
 
1213
CALL sp0();
 
1214
SELECT '-0-', @x;
 
1215
 
 
1216
CALL sp1();
 
1217
SELECT '-1-', @x;
 
1218
 
 
1219
CALL sp2();
 
1220
SELECT '-2-', @x;
 
1221
 
 
1222
CALL sp3();
 
1223
SELECT '-3-', @x;
 
1224
 
 
1225
CALL sp4();
 
1226
SELECT '-4-', @x;
 
1227
 
 
1228
# cleanup 3.1.2.58
 
1229
DROP PROCEDURE sp0;
 
1230
DROP PROCEDURE sp1;
 
1231
DROP PROCEDURE sp2;
 
1232
DROP PROCEDURE sp3;
 
1233
DROP PROCEDURE sp4;
 
1234
 
 
1235
 
 
1236
# ------------------------------------------------------------------------------
 
1237
let $message= Testcase 3.1.2.65:;
 
1238
--source include/show_msg.inc
 
1239
let $message=
 
1240
Ensure that FETCH <cursor name> returns the first row of the cursor_s result set
 
1241
the first time FETCH is executed, that it returns each subsequent row of the
 
1242
cursor_s result set each of the subsequent times FETCH is executed, and that it
 
1243
returns a NOT FOUND warning if it is executed after the last row of the cursor_s
 
1244
result set has already been fetched.;
 
1245
--source include/show_msg80.inc
 
1246
 
 
1247
--disable_warnings
 
1248
DROP PROCEDURE IF EXISTS sp1;
 
1249
DROP TABLE IF EXISTS temp;
 
1250
--enable_warnings
 
1251
 
 
1252
CREATE TABLE temp(
 
1253
   cnt INT,
 
1254
   f1 CHAR(20),
 
1255
   f2 CHAR(20),
 
1256
   f3 INT,
 
1257
   f4 CHAR(20),
 
1258
   f5 INT);
 
1259
 
 
1260
INSERT INTO temp VALUES(0, 'onip', 'abc', 8760, 'xyz', 10);
 
1261
 
 
1262
# NOT used: declare continue handler for sqlstate '02000' set proceed=0;
 
1263
# --> warning is shown when procedure is executed.
 
1264
delimiter //;
 
1265
CREATE PROCEDURE sp1( )
 
1266
BEGIN
 
1267
   declare proceed int default 1;
 
1268
   declare count integer default 1;
 
1269
   declare f1_value char(20);
 
1270
   declare f2_value char(20);
 
1271
   declare f5_value char(20);
 
1272
   declare f4_value integer;
 
1273
   declare f6_value integer;
 
1274
   declare cur1 cursor for SELECT f1, f2, f4, f5, f6 from t2 limit 3;
 
1275
   open cur1;
 
1276
   while proceed do
 
1277
      SELECT count AS 'loop';
 
1278
      fetch cur1 into f1_value, f2_value, f4_value, f5_value, f6_value;
 
1279
      insert into temp values (count, f1_value, f2_value, f4_value, f5_value, f6_value);
 
1280
      set count = count + 1;
 
1281
   END while;
 
1282
END//
 
1283
delimiter ;//
 
1284
 
 
1285
--error 1329
 
1286
CALL sp1();
 
1287
 
 
1288
SELECT * FROM temp;
 
1289
 
 
1290
# cleanup 3.1.2.65
 
1291
DROP TABLE temp;
 
1292
DROP PROCEDURE sp1;
 
1293
 
 
1294
 
 
1295
# ------------------------------------------------------------------------------
 
1296
let $message= Testcase 3.1.2.68:;
 
1297
--source include/show_msg.inc
 
1298
let $message=
 
1299
Ensure that FETCH <cursor name> fails with an appropriate error message if the
 
1300
number of columns to be fetched does not match the number of variables specified
 
1301
by the FETCH statement.;
 
1302
--source include/show_msg80.inc
 
1303
 
 
1304
--disable_warnings
 
1305
DROP PROCEDURE IF EXISTS sp1;
 
1306
DROP PROCEDURE IF EXISTS sp2;
 
1307
--enable_warnings
 
1308
 
 
1309
delimiter //;
 
1310
--echo --> not enough columns in FETCH statement
 
1311
CREATE PROCEDURE sp1( )
 
1312
BEGIN
 
1313
   declare newf1 char(20);
 
1314
   declare cur1 cursor for SELECT f1, f2 from t2 limit 10;
 
1315
   declare continue handler for sqlstate '02000' SELECT 'HANDLER executed.' AS '';
 
1316
   BEGIN
 
1317
      open cur1;
 
1318
      fetch cur1 into newf1;
 
1319
      SELECT newf1;
 
1320
      close cur1;
 
1321
   END;
 
1322
END//
 
1323
 
 
1324
--echo --> too many columns in FETCH statement
 
1325
CREATE PROCEDURE sp2( )
 
1326
BEGIN
 
1327
   declare newf1 char(20);
 
1328
   declare newf2 char(20);
 
1329
   declare cur1 cursor for SELECT f1 from t2 limit 10;
 
1330
   declare continue handler for sqlstate '02000' SELECT 'HANDLER executed.' AS '';
 
1331
   BEGIN
 
1332
      open cur1;
 
1333
      fetch cur1 into newf1, newf2;
 
1334
      SELECT newf1, newf2;
 
1335
      close cur1;
 
1336
   END;
 
1337
END//
 
1338
delimiter ;//
 
1339
 
 
1340
--echo --> not enough columns in FETCH statement
 
1341
--error 1328
 
1342
CALL sp1();
 
1343
 
 
1344
--echo --> too many columns in FETCH statement
 
1345
--error 1328
 
1346
CALL sp2();
 
1347
 
 
1348
# cleanup 3.1.2.68
 
1349
DROP PROCEDURE sp1;
 
1350
DROP PROCEDURE sp2;
 
1351
 
 
1352
 
 
1353
# ------------------------------------------------------------------------------
 
1354
let $message= Testcase 3.1.2.75:;
 
1355
--source include/show_msg.inc
 
1356
let $message=
 
1357
Ensure that, for nested compound statements, a cursor that was declared and
 
1358
opened during an outer level of the statement is not closed when an inner level
 
1359
of a compound statement ends.;
 
1360
--source include/show_msg80.inc
 
1361
 
 
1362
--disable_warnings
 
1363
DROP TABLE IF EXISTS temp1;
 
1364
DROP PROCEDURE IF EXISTS sp1;
 
1365
--enable_warnings
 
1366
 
 
1367
create table temp1( f0 char(20), f1 char(20), f2 char(20), f3 int, f4 char(20) );
 
1368
 
 
1369
#Error: 1329 SQLSTATE: 02000 (ER_SP_FETCH_NO_DATA) Message: No data to FETCH
 
1370
 
 
1371
SELECT f1, f2, f4, f5 from t2;
 
1372
 
 
1373
delimiter //;
 
1374
CREATE PROCEDURE sp1( )
 
1375
BEGIN
 
1376
   declare count integer;
 
1377
   declare from0 char(20);
 
1378
   declare newf1 char(20);
 
1379
   declare newf2 char(20);
 
1380
   declare newf5 char(20);
 
1381
   declare newf4 integer;
 
1382
   declare cur1 cursor for SELECT f1, f2, f4, f5 from t2 limit 5;
 
1383
   declare cur2 cursor for SELECT f1, f2, f4, f5 from t2 limit 5;
 
1384
   open cur1;
 
1385
   open cur2;
 
1386
   BEGIN
 
1387
      declare continue handler for sqlstate '02000' set count = 1;
 
1388
      fetch cur1 into newf1, newf2, newf4, newf5;
 
1389
      SELECT '-1-', count, newf1, newf2, newf4, newf5;
 
1390
      insert into temp1 values ('cur1_out', newf1, newf2, newf4, newf5);
 
1391
      set count = 4;
 
1392
      BEGIN
 
1393
         while count > 0 do
 
1394
            fetch cur1 into newf1, newf2, newf4, newf5;
 
1395
            SELECT '-2-', count, newf1, newf2, newf4, newf5;
 
1396
            set count = count - 1;
 
1397
         END while;
 
1398
         SELECT '-3-', count, newf1, newf2, newf4, newf4;
 
1399
      END;
 
1400
      BEGIN
 
1401
         fetch cur1 into newf1, newf2, newf4, newf5;
 
1402
         SELECT '-4-', newf1, newf2, newf4, newf5;
 
1403
         insert into temp1 values ('cur1_in', newf1, newf2, newf4, newf5);
 
1404
      END;
 
1405
      fetch cur2 into newf1, newf2, newf4, newf5;
 
1406
      SELECT '-5-', newf1, newf2, newf4, newf5;
 
1407
      insert into temp1 values ('cur2', newf1, newf2, newf4, newf5);
 
1408
      close cur1;
 
1409
   END;
 
1410
   fetch cur2 into newf1, newf2, newf4, newf5;
 
1411
   SELECT '-6-', newf1, newf2, newf4, newf5;
 
1412
   close cur2;
 
1413
END//
 
1414
delimiter ;//
 
1415
 
 
1416
CALL sp1();
 
1417
 
 
1418
SELECT * from temp1;
 
1419
 
 
1420
# cleanup 3.1.2.75
 
1421
DROP PROCEDURE sp1;
 
1422
drop table temp1;
 
1423
 
 
1424
 
 
1425
# ------------------------------------------------------------------------------
 
1426
let $message= Testcase 3.1.2.76:;
 
1427
--source include/show_msg.inc
 
1428
let $message=
 
1429
Ensure that all cursors operate asensitively, so that there is no concurrency
 
1430
conflict between cursors operating on the same, or similar, sets of results
 
1431
during execution of one or more stored procedures.;
 
1432
--source include/show_msg80.inc
 
1433
 
 
1434
--disable_warnings
 
1435
DROP PROCEDURE IF EXISTS sp1;
 
1436
drop table IF EXISTS temp1;
 
1437
drop table IF EXISTS temp2;
 
1438
--enable_warnings
 
1439
 
 
1440
create table temp1( f0 char(10), cnt int, f1 char(20), f2 char(20), f3 date, f4 integer );
 
1441
create table temp2( f0 char(10), cnt int, f1 char(20), f2 char(20), f3 date, f4 integer );
 
1442
 
 
1443
delimiter //;
 
1444
CREATE PROCEDURE sp_inner( )
 
1445
BEGIN
 
1446
   declare proceed int default 1;
 
1447
   declare i_count integer default 20;
 
1448
   declare i_newf1 char(20);
 
1449
   declare i_newf2 char(20);
 
1450
   declare i_newf3 date;
 
1451
   declare i_newf4 integer;
 
1452
   declare i_newf11 char(20);
 
1453
   declare i_newf12 char(20);
 
1454
   declare i_newf13 date;
 
1455
   declare i_newf14 integer;
 
1456
   declare cur1 cursor for SELECT f1, f2, f3, f4 from t2 limit 4;
 
1457
   declare cur2 cursor for SELECT f1, f2, f3, f4 from t2 limit 3;
 
1458
   declare continue handler for sqlstate '02000' set proceed=0;
 
1459
   open cur1;
 
1460
   open cur2;
 
1461
   set i_count = 10;
 
1462
   while proceed do
 
1463
      fetch cur1 into i_newf1, i_newf2, i_newf3, i_newf4;
 
1464
      IF proceed THEN
 
1465
         insert into temp1 values ('sp_inner', i_count, i_newf1, i_newf2, i_newf3, i_newf4);
 
1466
         fetch cur2 into i_newf11, i_newf12, i_newf13, i_newf14;
 
1467
         IF proceed THEN
 
1468
            insert into temp2 values ('sp_inner', i_count, i_newf11, i_newf12, i_newf13, i_newf14);
 
1469
         END IF;
 
1470
      END IF;
 
1471
      set i_count = i_count - 1;
 
1472
   END while;
 
1473
   close cur1;
 
1474
   close cur2;
 
1475
END//
 
1476
 
 
1477
CREATE PROCEDURE sp_outer( )
 
1478
BEGIN
 
1479
   DECLARE proceed INT DEFAULT 1;
 
1480
   DECLARE o_count INTEGER DEFAULT 20;
 
1481
   DECLARE o_newf1 CHAR(20);
 
1482
   DECLARE o_newf2 CHAR(20);
 
1483
   DECLARE o_newf3 DATE;
 
1484
   DECLARE o_newf4 INTEGER;
 
1485
   DECLARE o_newf11 CHAR(20);
 
1486
   DECLARE o_newf12 CHAR(20);
 
1487
   DECLARE o_newf13 DATE;
 
1488
   DECLARE o_newf14 INTEGER;
 
1489
   DECLARE cur1 CURSOR FOR SELECT f1, f2, f3, f4 FROM t2 LIMIT 5;
 
1490
   DECLARE cur2 CURSOR FOR SELECT f1, f2, f3, f4 FROM t2 LIMIT 5;
 
1491
   DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET proceed=0;
 
1492
   OPEN cur1;
 
1493
   OPEN cur2;
 
1494
   SET o_count = 1;
 
1495
   WHILE proceed DO
 
1496
      FETCH cur1 INTO o_newf1, o_newf2, o_newf3, o_newf4;
 
1497
      IF proceed THEN
 
1498
         INSERT INTO temp1 VALUES ('_sp_out_', o_count, o_newf1, o_newf2, o_newf3, o_newf4);
 
1499
         CALL sp_inner();
 
1500
         FETCH cur2 INTO o_newf11, o_newf12, o_newf13, o_newf14;
 
1501
         IF proceed THEN
 
1502
            INSERT INTO temp2 VALUES ('_sp_out_', o_count, o_newf11, o_newf12, o_newf13, o_newf14);
 
1503
         END IF;
 
1504
      END IF;
 
1505
      SET o_count = o_count + 1;
 
1506
   END WHILE;
 
1507
   CLOSE cur1;
 
1508
   CLOSE cur2;
 
1509
END//
 
1510
delimiter ;//
 
1511
 
 
1512
CALL sp_outer();
 
1513
 
 
1514
SELECT * FROM temp1;
 
1515
SELECT * FROM temp2;
 
1516
 
 
1517
# cleanup 3.1.2.75
 
1518
DROP PROCEDURE sp_outer;
 
1519
DROP PROCEDURE sp_inner;
 
1520
DROP TABLE temp1;
 
1521
DROP TABLE temp2;
 
1522
 
 
1523
 
 
1524
# ==============================================================================
 
1525
# USE the same .inc to cleanup before and after the test
 
1526
--source suite/funcs_1/storedproc/cleanup_sp_tb.inc
 
1527
 
 
1528
# ==============================================================================
 
1529
let $message= .                               +++ END OF SCRIPT +++;
 
1530
--source include/show_msg80.inc
 
1531
# ==============================================================================