mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysql-test / t / sp.test
blob75d290f7c8bf5946fa6d14ed8c51837e01e8d5b6
2 # Basic stored PROCEDURE tests
4 # Test cases for bugs are added at the end. See template there.
6 # Some tests that require --error go into sp-error.test
7 # Tests that require inndb go into sp_trans.test
8 # Tests that check privilege and security issues go to sp-security.test.
9 # Tests that require multiple connections, except security/privilege tests,
10 #   go to sp-thread.
11 # Tests that uses 'goto' to into sp-goto.test (currently disabled)
12 # Tests that destroys system tables (e.g. mysql.proc) for error testing
13 #   go to sp-destruct.
14 # Tests that require --with-geometry go into sp_gis.test
15 # Tests that require multibyte character sets, which are not always available,
16 #   go into separate files (e.g. sp-ucs2.test)
18 use test;
20 # Test tables
22 # t1 and t2 are reused throughout the file, and dropped at the end.
23 # t3 and up are created and dropped when needed.
25 --disable_warnings
26 drop table if exists t1,t2,t3,t4;
27 drop view if exists v1;
28 drop procedure if exists p1;
29 drop procedure if exists p2;
30 drop function if exists f1;
31 drop function if exists f2;
32 --enable_warnings
33 create table t1 (
34         id   char(16) not null default '',
35         data int not null
37 create table t2 (
38         s   char(16),
39         i   int,
40         d   double
44 # Single statement, no params.
45 --disable_warnings
46 drop procedure if exists foo42;
47 --enable_warnings
48 create procedure foo42()
49   insert into test.t1 values ("foo", 42);
51 call foo42();
52 select * from t1;
53 delete from t1;
54 drop procedure foo42;
57 # Single statement, two IN params.
58 --disable_warnings
59 drop procedure if exists bar;
60 --enable_warnings
61 create procedure bar(x char(16), y int)
62   insert into test.t1 values (x, y);
64 call bar("bar", 666);
65 select * from t1;
66 delete from t1;
67 # Don't drop procedure yet...
70 # Now for multiple statements...
71 delimiter |;
73 # Empty statement
74 --disable_warnings
75 drop procedure if exists empty|
76 --enable_warnings
77 create procedure empty()
78 begin
79 end|
81 call empty()|
82 drop procedure empty|
84 # Scope test. This is legal (warnings might be possible in the future,
85 # but for the time being, we just accept it).
86 --disable_warnings
87 drop procedure if exists scope|
88 --enable_warnings
89 create procedure scope(a int, b float)
90 begin
91   declare b int;
92   declare c float;
94   begin
95     declare c int;
96   end;
97 end|
99 drop procedure scope|
101 # Two statements.
102 --disable_warnings
103 drop procedure if exists two|
104 --enable_warnings
105 create procedure two(x1 char(16), x2 char(16), y int)
106 begin
107   insert into test.t1 values (x1, y);
108   insert into test.t1 values (x2, y);
109 end|
111 call two("one", "two", 3)|
112 select * from t1|
113 delete from t1|
114 drop procedure two|
117 # Simple test of local variables and SET.
118 --disable_warnings
119 drop procedure if exists locset|
120 --enable_warnings
121 create procedure locset(x char(16), y int)
122 begin
123   declare z1, z2 int;
124   set z1 = y;
125   set z2 = z1+2;
126   insert into test.t1 values (x, z2);
127 end|
129 call locset("locset", 19)|
130 select * from t1|
131 delete from t1|
132 drop procedure locset|
135 # In some contexts local variables are not recognized
136 # (and in some, you have to qualify the identifier).
137 --disable_warnings
138 drop procedure if exists setcontext|
139 --enable_warnings
140 create procedure setcontext()
141 begin
142   declare data int default 2;
144   insert into t1 (id, data) values ("foo", 1);
145   replace t1 set data = data, id = "bar";
146   update t1 set id = "kaka", data = 3 where t1.data = data;
147 end|
149 call setcontext()|
150 select * from t1 order by data|
151 delete from t1|
152 drop procedure setcontext|
155 # Set things to null
156 create table t3 ( d date, i int, f double, s varchar(32) )|
158 --disable_warnings
159 drop procedure if exists nullset|
160 --enable_warnings
161 create procedure nullset()
162 begin
163   declare ld date;
164   declare li int;
165   declare lf double;
166   declare ls varchar(32);
168   set ld = null, li = null, lf = null, ls = null;
169   insert into t3 values (ld, li, lf, ls);
171   insert into t3 (i, f, s) values ((ld is null), 1,    "ld is null"),
172                                   ((li is null), 1,    "li is null"),
173                                   ((li = 0),     null, "li = 0"),
174                                   ((lf is null), 1,    "lf is null"),
175                                   ((lf = 0),     null, "lf = 0"),
176                                   ((ls is null), 1,    "ls is null");
177 end|
179 call nullset()|
180 select * from t3|
181 drop table t3|
182 drop procedure nullset|
185 # The peculiar (non-standard) mixture of variables types in SET.
186 --disable_warnings
187 drop procedure if exists mixset|
188 --enable_warnings
189 create procedure mixset(x char(16), y int)
190 begin
191   declare z int;
193   set @z = y, z = 666, max_join_size = 100;
194   insert into test.t1 values (x, z);
195 end|
197 call mixset("mixset", 19)|
198 show variables like 'max_join_size'|
199 select id,data,@z from t1|
200 delete from t1|
201 drop procedure mixset|
204 # Multiple CALL statements, one with OUT parameter.
205 --disable_warnings
206 drop procedure if exists zip|
207 --enable_warnings
208 create procedure zip(x char(16), y int)
209 begin
210   declare z int;
211   call zap(y, z);
212   call bar(x, z);
213 end|
215 # SET local variables and OUT parameter.
216 --disable_warnings
217 drop procedure if exists zap|
218 --enable_warnings
219 create procedure zap(x int, out y int)
220 begin
221   declare z int;
222   set z = x+1, y = z;
223 end|
225 call zip("zip", 99)|
226 select * from t1|
227 delete from t1|
228 drop procedure zip|
229 drop procedure bar|
231 # Top-level OUT parameter
232 call zap(7, @zap)|
233 select @zap|
235 drop procedure zap|
238 # "Deep" calls...
239 --disable_warnings
240 drop procedure if exists c1|
241 --enable_warnings
242 create procedure c1(x int)
243   call c2("c", x)|
244 --disable_warnings
245 drop procedure if exists c2|
246 --enable_warnings
247 create procedure c2(s char(16), x int)
248   call c3(x, s)|
249 --disable_warnings
250 drop procedure if exists c3|
251 --enable_warnings
252 create procedure c3(x int, s char(16))
253   call c4("level", x, s)|
254 --disable_warnings
255 drop procedure if exists c4|
256 --enable_warnings
257 create procedure c4(l char(8), x int, s char(16))
258   insert into t1 values (concat(l,s), x)|
260 call c1(42)|
261 select * from t1|
262 delete from t1|
263 drop procedure c1|
264 drop procedure c2|
265 drop procedure c3|
266 drop procedure c4|
268 # INOUT test
269 --disable_warnings
270 drop procedure if exists iotest|
271 --enable_warnings
272 create procedure iotest(x1 char(16), x2 char(16), y int)
273 begin
274   call inc2(x2, y);
275   insert into test.t1 values (x1, y);
276 end|
278 --disable_warnings
279 drop procedure if exists inc2|
280 --enable_warnings
281 create procedure inc2(x char(16), y int)
282 begin
283   call inc(y);
284   insert into test.t1 values (x, y);
285 end|
287 --disable_warnings
288 drop procedure if exists inc|
289 --enable_warnings
290 create procedure inc(inout io int)
291   set io = io + 1|
293 call iotest("io1", "io2", 1)|
294 select * from t1 order by data desc|
295 delete from t1|
296 drop procedure iotest|
297 drop procedure inc2|
299 # Propagating top-level @-vars
300 --disable_warnings
301 drop procedure if exists incr|
302 --enable_warnings
303 create procedure incr(inout x int)
304   call inc(x)|
306 # Before
307 select @zap|
308 call incr(@zap)|
309 # After
310 select @zap|
312 drop procedure inc|
313 drop procedure incr|
315 # Call-by-value test
316 #  The expected result is:
317 #    ("cbv2", 4)
318 #    ("cbv1", 4711)
319 --disable_warnings
320 drop procedure if exists cbv1|
321 --enable_warnings
322 create procedure cbv1()
323 begin
324   declare y int default 3;
326   call cbv2(y+1, y);
327   insert into test.t1 values ("cbv1", y);
328 end|
330 --disable_warnings
331 drop procedure if exists cbv2|
332 --enable_warnings
333 create procedure cbv2(y1 int, inout y2 int)
334 begin
335   set y2 = 4711;
336   insert into test.t1 values ("cbv2", y1);
337 end|
339 call cbv1()|
340 select * from t1 order by data|
341 delete from t1|
342 drop procedure cbv1|
343 drop procedure cbv2|
346 # Subselect arguments
348 insert into t2 values ("a", 1, 1.1), ("b", 2, 1.2), ("c", 3, 1.3)|
350 --disable_warnings
351 drop procedure if exists sub1|
352 --enable_warnings
353 create procedure sub1(id char(16), x int)
354   insert into test.t1 values (id, x)|
356 --disable_warnings
357 drop procedure if exists sub2|
358 --enable_warnings
359 create procedure sub2(id char(16))
360 begin
361   declare x int;
362   set x = (select sum(t.i) from test.t2 t);
363   insert into test.t1 values (id, x);
364 end|
366 --disable_warnings
367 drop procedure if exists sub3|
368 --enable_warnings
369 create function sub3(i int) returns int deterministic
370   return i+1|
372 call sub1("sub1a", (select 7))|
373 call sub1("sub1b", (select max(i) from t2))|
374 --error ER_OPERAND_COLUMNS
375 call sub1("sub1c", (select i,d from t2 limit 1))|
376 call sub1("sub1d", (select 1 from (select 1) a))|
377 call sub2("sub2")|
378 select * from t1 order by id|
379 select sub3((select max(i) from t2))|
380 drop procedure sub1|
381 drop procedure sub2|
382 drop function sub3|
383 delete from t1|
384 delete from t2|
386 # Basic tests of the flow control constructs
388 # Just test on 'x'...
389 --disable_warnings
390 drop procedure if exists a0|
391 --enable_warnings
392 create procedure a0(x int)
393 while x do
394   set x = x-1;
395   insert into test.t1 values ("a0", x);
396 end while|
398 call a0(3)|
399 select * from t1 order by data desc|
400 delete from t1|
401 drop procedure a0|
404 # The same, but with a more traditional test.
405 --disable_warnings
406 drop procedure if exists a|
407 --enable_warnings
408 create procedure a(x int)
409 while x > 0 do
410   set x = x-1;
411   insert into test.t1 values ("a", x);
412 end while|
414 call a(3)|
415 select * from t1 order by data desc|
416 delete from t1|
417 drop procedure a|
420 # REPEAT
421 --disable_warnings
422 drop procedure if exists b|
423 --enable_warnings
424 create procedure b(x int)
425 repeat
426   insert into test.t1 values (repeat("b",3), x);
427   set x = x-1;
428 until x = 0 end repeat|
430 call b(3)|
431 select * from t1 order by data desc|
432 delete from t1|
433 drop procedure b|
436 # Check that repeat isn't parsed the wrong way
437 --disable_warnings
438 drop procedure if exists b2|
439 --enable_warnings
440 create procedure b2(x int)
441 repeat(select 1 into outfile 'b2');
442   insert into test.t1 values (repeat("b2",3), x);
443   set x = x-1;
444 until x = 0 end repeat|
446 # We don't actually want to call it.
447 drop procedure b2|
450 # Labelled WHILE with ITERATE (pointless really)
451 --disable_warnings
452 drop procedure if exists c|
453 --enable_warnings
454 create procedure c(x int)
455 hmm: while x > 0 do
456   insert into test.t1 values ("c", x);
457   set x = x-1;
458   iterate hmm;
459   insert into test.t1 values ("x", x);
460 end while hmm|
462 call c(3)|
463 select * from t1 order by data desc|
464 delete from t1|
465 drop procedure c|
468 # Labelled WHILE with LEAVE
469 --disable_warnings
470 drop procedure if exists d|
471 --enable_warnings
472 create procedure d(x int)
473 hmm: while x > 0 do
474   insert into test.t1 values ("d", x);
475   set x = x-1;
476   leave hmm;
477   insert into test.t1 values ("x", x);
478 end while|
480 call d(3)|
481 select * from t1|
482 delete from t1|
483 drop procedure d|
486 # LOOP, with simple IF statement
487 --disable_warnings
488 drop procedure if exists e|
489 --enable_warnings
490 create procedure e(x int)
491 foo: loop
492   if x = 0 then
493     leave foo;
494   end if;
495   insert into test.t1 values ("e", x);
496   set x = x-1;
497 end loop foo|
499 call e(3)|
500 select * from t1 order by data desc|
501 delete from t1|
502 drop procedure e|
505 # A full IF statement
506 --disable_warnings
507 drop procedure if exists f|
508 --enable_warnings
509 create procedure f(x int)
510 if x < 0 then
511   insert into test.t1 values ("f", 0);
512 elseif x = 0 then
513   insert into test.t1 values ("f", 1);
514 else
515   insert into test.t1 values ("f", 2);
516 end if|
518 call f(-2)|
519 call f(0)|
520 call f(4)|
521 select * from t1 order by data|
522 delete from t1|
523 drop procedure f|
526 # This form of CASE is really just syntactic sugar for IF-ELSEIF-...
527 --disable_warnings
528 drop procedure if exists g|
529 --enable_warnings
530 create procedure g(x int)
531 case
532 when x < 0 then
533   insert into test.t1 values ("g", 0);
534 when x = 0 then
535   insert into test.t1 values ("g", 1);
536 else
537   insert into test.t1 values ("g", 2);
538 end case|
540 call g(-42)|
541 call g(0)|
542 call g(1)|
543 select * from t1 order by data|
544 delete from t1|
545 drop procedure g|
548 # The "simple CASE"
549 --disable_warnings
550 drop procedure if exists h|
551 --enable_warnings
552 create procedure h(x int)
553 case x
554 when 0 then
555   insert into test.t1 values ("h0", x);
556 when 1 then
557   insert into test.t1 values ("h1", x);
558 else
559   insert into test.t1 values ("h?", x);
560 end case|
562 call h(0)|
563 call h(1)|
564 call h(17)|
565 select * from t1 order by data|
566 delete from t1|
567 drop procedure h|
570 # It's actually possible to LEAVE a BEGIN-END block
571 --disable_warnings
572 drop procedure if exists i|
573 --enable_warnings
574 create procedure i(x int)
575 foo:
576 begin
577   if x = 0 then
578     leave foo;
579   end if;
580   insert into test.t1 values ("i", x);
581 end foo|
583 call i(0)|
584 call i(3)|
585 select * from t1|
586 delete from t1|
587 drop procedure i|
590 # SELECT with one of more result set sent back to the clinet
591 insert into t1 values ("foo", 3), ("bar", 19)|
592 insert into t2 values ("x", 9, 4.1), ("y", -1, 19.2), ("z", 3, 2.2)|
594 --disable_warnings
595 drop procedure if exists sel1|
596 --enable_warnings
597 create procedure sel1()
598 begin
599   select * from t1 order by data;
600 end|
602 call sel1()|
603 drop procedure sel1|
605 --disable_warnings
606 drop procedure if exists sel2|
607 --enable_warnings
608 create procedure sel2()
609 begin
610   select * from t1 order by data;
611   select * from t2 order by s;
612 end|
614 call sel2()|
615 drop procedure sel2|
616 delete from t1|
617 delete from t2|
619 # SELECT INTO local variables
620 --disable_warnings
621 drop procedure if exists into_test|
622 --enable_warnings
623 create procedure into_test(x char(16), y int)
624 begin
625   insert into test.t1 values (x, y);
626   select id,data into x,y from test.t1 limit 1;
627   insert into test.t1 values (concat(x, "2"), y+2);
628 end|
630 call into_test("into", 100)|
631 select * from t1 order by data|
632 delete from t1|
633 drop procedure into_test|
636 # SELECT INTO with a mix of local and global variables
637 --disable_warnings
638 drop procedure if exists into_tes2|
639 --enable_warnings
640 create procedure into_test2(x char(16), y int)
641 begin
642   insert into test.t1 values (x, y);
643   select id,data into x,@z from test.t1 limit 1;
644   insert into test.t1 values (concat(x, "2"), y+2);
645 end|
647 call into_test2("into", 100)|
648 select id,data,@z from t1 order by data|
649 delete from t1|
650 drop procedure into_test2|
653 # SELECT * INTO ... (bug test)
654 --disable_warnings
655 drop procedure if exists into_test3|
656 --enable_warnings
657 create procedure into_test3()
658 begin
659   declare x char(16);
660   declare y int;
662   select * into x,y from test.t1 limit 1;
663   insert into test.t2 values (x, y, 0.0);
664 end|
666 insert into t1 values ("into3", 19)|
667 # Two call needed for bug test
668 call into_test3()|
669 call into_test3()|
670 select * from t2|
671 delete from t1|
672 delete from t2|
673 drop procedure into_test3|
676 # SELECT INTO with no data is a warning ("no data", which we will
677 # not see normally). When not caught, execution proceeds.
678 --disable_warnings
679 drop procedure if exists into_test4|
680 --enable_warnings
681 create procedure into_test4()
682 begin
683   declare x int;
685   select data into x from test.t1 limit 1;
686   insert into test.t3 values ("into4", x);
687 end|
689 delete from t1|
690 create table t3 ( s char(16), d int)|
691 call into_test4()|
692 select * from t3|
693 insert into t1 values ("i4", 77)|
694 call into_test4()|
695 select * from t3|
696 delete from t1|
697 drop table t3|
698 drop procedure into_test4|
701 # These two (and the two procedures above) caused an assert() to fail in
702 # sql_base.cc:lock_tables() at some point.
703 --disable_warnings
704 drop procedure if exists into_outfile|
705 --enable_warnings
706 --replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
707 eval create procedure into_outfile(x char(16), y int)
708 begin
709   insert into test.t1 values (x, y);
710   select * into outfile "$MYSQLTEST_VARDIR/tmp/spout" from test.t1;
711   insert into test.t1 values (concat(x, "2"), y+2);
712 end|
714 # Check that file does not exists
715 --error 1
716 --file_exists $MYSQLTEST_VARDIR/tmp/spout
717 call into_outfile("ofile", 1)|
718 --remove_file $MYSQLTEST_VARDIR/tmp/spout
719 delete from t1|
720 drop procedure into_outfile|
722 --disable_warnings
723 drop procedure if exists into_dumpfile|
724 --enable_warnings
725 --replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
726 eval create procedure into_dumpfile(x char(16), y int)
727 begin
728   insert into test.t1 values (x, y);
729   select * into dumpfile "$MYSQLTEST_VARDIR/tmp/spdump" from test.t1 limit 1;
730   insert into test.t1 values (concat(x, "2"), y+2);
731 end|
733 # Check that file does not exists
734 --error 1
735 --file_exists $MYSQLTEST_VARDIR/tmp/spdump
736 call into_dumpfile("dfile", 1)|
737 --remove_file $MYSQLTEST_VARDIR/tmp/spdump
738 delete from t1|
739 drop procedure into_dumpfile|
741 --disable_warnings
742 drop procedure if exists create_select|
743 --enable_warnings
744 create procedure create_select(x char(16), y int)
745 begin
746   insert into test.t1 values (x, y);
747   create temporary table test.t3 select * from test.t1;
748   insert into test.t3 values (concat(x, "2"), y+2);
749 end|
751 call create_select("cs", 90)|
752 select * from t1, t3|
753 drop table t3|
754 delete from t1|
755 drop procedure create_select|
758 # A minimal, constant FUNCTION.
759 --disable_warnings
760 drop function if exists e|
761 --enable_warnings
762 create function e() returns double
763   return 2.7182818284590452354|
765 set @e = e()|
766 select e(), @e|
768 # A minimal function with one argument
769 --disable_warnings
770 drop function if exists inc|
771 --enable_warnings
772 create function inc(i int) returns int
773   return i+1|
775 select inc(1), inc(99), inc(-71)|
777 # A minimal function with two arguments
778 --disable_warnings
779 drop function if exists mul|
780 --enable_warnings
781 create function mul(x int, y int) returns int
782   return x*y|
784 select mul(1,1), mul(3,5), mul(4711, 666)|
786 # A minimal string function
787 --disable_warnings
788 drop function if exists append|
789 --enable_warnings
790 create function append(s1 char(8), s2 char(8)) returns char(16)
791   return concat(s1, s2)|
793 select append("foo", "bar")|
795 # A function with flow control
796 --disable_warnings
797 drop function if exists fac|
798 --enable_warnings
799 create function fac(n int unsigned) returns bigint unsigned
800 begin
801   declare f bigint unsigned default 1;
803   while n > 1 do
804     set f = f * n;
805     set n = n - 1;
806   end while;
807   return f;
808 end|
810 select fac(1), fac(2), fac(5), fac(10)|
812 # Nested calls
813 --disable_warnings
814 drop function if exists fun|
815 --enable_warnings
816 create function fun(d double, i int, u int unsigned) returns double
817   return mul(inc(i), fac(u)) / e()|
819 select fun(2.3, 3, 5)|
822 # Various function calls in differen statements
824 insert into t2 values (append("xxx", "yyy"), mul(4,3), e())|
825 insert into t2 values (append("a", "b"), mul(2,mul(3,4)), fun(1.7, 4, 6))|
827 # Disable PS because double's give a bit different values
828 --disable_ps_protocol
829 select * from t2 where s = append("a", "b")|
830 select * from t2 where i = mul(4,3) or i = mul(mul(3,4),2) order by i|
831 select * from t2 where d = e()|
832 select * from t2 order by i|
833 --enable_ps_protocol
834 delete from t2|
836 drop function e|
837 drop function inc|
838 drop function mul|
839 drop function append|
840 drop function fun|
844 # CONDITIONs and HANDLERs
847 --disable_warnings
848 drop procedure if exists hndlr1|
849 --enable_warnings
850 create procedure hndlr1(val int)
851 begin
852   declare x int default 0;
853   declare foo condition for 1136;
854   declare bar condition for sqlstate '42S98';        # Just for testing syntax
855   declare zip condition for sqlstate value '42S99';  # Just for testing syntax
856   declare continue handler for foo set x = 1;
858   insert into test.t1 values ("hndlr1", val, 2);  # Too many values
859   if (x) then
860     insert into test.t1 values ("hndlr1", val);   # This instead then
861   end if;
862 end|
864 call hndlr1(42)|
865 select * from t1|
866 delete from t1|
867 drop procedure hndlr1|
869 --disable_warnings
870 drop procedure if exists hndlr2|
871 --enable_warnings
872 create procedure hndlr2(val int)
873 begin
874   declare x int default 0;
876   begin
877     declare exit handler for sqlstate '21S01' set x = 1;
879     insert into test.t1 values ("hndlr2", val, 2); # Too many values
880   end;
882   insert into test.t1 values ("hndlr2", x);
883 end|
885 call hndlr2(42)|
886 select * from t1|
887 delete from t1|
888 drop procedure hndlr2|
891 --disable_warnings
892 drop procedure if exists hndlr3|
893 --enable_warnings
894 create procedure hndlr3(val int)
895 begin
896   declare x int default 0;
897   declare continue handler for sqlexception        # Any error
898   begin
899     declare z int;
901     set z = 2 * val;
902     set x = 1;
903   end;
905   if val < 10 then
906     begin
907       declare y int;
909       set y = val + 10;
910       insert into test.t1 values ("hndlr3", y, 2);  # Too many values
911       if x then
912         insert into test.t1 values ("hndlr3", y);
913       end if;
914     end;
915   end if;
916 end|
918 call hndlr3(3)|
919 select * from t1|
920 delete from t1|
921 drop procedure hndlr3|
924 # Variables might be uninitialized when using handlers
925 # (Otherwise the compiler can detect if a variable is not set, but
926 #  not in this case.)
927 create table t3 ( id   char(16), data int )|
929 --disable_warnings
930 drop procedure if exists hndlr4|
931 --enable_warnings
932 create procedure hndlr4()
933 begin
934   declare x int default 0;
935   declare val int;                                 # No default
936   declare continue handler for sqlstate '02000' set x=1;
938   select data into val from test.t3 where id='z' limit 1;  # No hits
940   insert into test.t3 values ('z', val);
941 end|
943 call hndlr4()|
944 select * from t3|
945 drop table t3|
946 drop procedure hndlr4|
950 # Cursors
952 --disable_warnings
953 drop procedure if exists cur1|
954 --enable_warnings
955 create procedure cur1()
956 begin
957   declare a char(16);
958   declare b int;
959   declare c double;
960   declare done int default 0;
961   declare c cursor for select * from test.t2;
962   declare continue handler for sqlstate '02000' set done = 1;
964   open c;
965   repeat
966     fetch c into a, b, c;
967     if not done then
968        insert into test.t1 values (a, b+c);
969     end if;
970   until done end repeat;
971   close c;
972 end|
974 insert into t2 values ("foo", 42, -1.9), ("bar", 3, 12.1), ("zap", 666, -3.14)|
975 call cur1()|
976 select * from t1|
977 drop procedure cur1|
979 create table t3 ( s char(16), i int )|
981 --disable_warnings
982 drop procedure if exists cur2|
983 --enable_warnings
984 create procedure cur2()
985 begin
986   declare done int default 0;
987   declare c1 cursor for select id,data from test.t1 order by id,data;
988   declare c2 cursor for select i from test.t2 order by i;
989   declare continue handler for sqlstate '02000' set done = 1;
991   open c1;
992   open c2;
993   repeat
994   begin
995     declare a char(16);
996     declare b,c int;
998     fetch from c1 into a, b;
999     fetch next from c2 into c;
1000     if not done then
1001       if b < c then
1002         insert into test.t3 values (a, b);
1003       else
1004         insert into test.t3 values (a, c);
1005       end if;
1006     end if;
1007   end;
1008   until done end repeat;
1009   close c1;
1010   close c2;
1011 end|
1013 call cur2()|
1014 select * from t3 order by i,s|
1015 delete from t1|
1016 delete from t2|
1017 drop table t3|
1018 drop procedure cur2|
1021 # The few characteristics we parse
1022 --disable_warnings
1023 drop procedure if exists chistics|
1024 --enable_warnings
1025 create procedure chistics()
1026     language sql
1027     modifies sql data
1028     not deterministic
1029     sql security definer
1030     comment 'Characteristics procedure test'
1031   insert into t1 values ("chistics", 1)|
1033 show create procedure chistics|
1034 # Call it, just to make sure.
1035 call chistics()|
1036 select * from t1|
1037 delete from t1|
1038 alter procedure chistics sql security invoker|
1039 show create procedure chistics|
1040 drop procedure chistics|
1042 --disable_warnings
1043 drop function if exists chistics|
1044 --enable_warnings
1045 create function chistics() returns int
1046     language sql
1047     deterministic
1048     sql security invoker
1049     comment 'Characteristics procedure test'
1050   return 42|
1052 show create function chistics|
1053 # Call it, just to make sure.
1054 select chistics()|
1055 alter function chistics
1056    no sql
1057    comment 'Characteristics function test'|
1058 show create function chistics|
1059 drop function chistics|
1062 # Check mode settings
1063 insert into t1 values ("foo", 1), ("bar", 2), ("zip", 3)|
1065 set @@sql_mode = 'ANSI'|
1066 delimiter $|
1067 --disable_warnings
1068 drop procedure if exists modes$
1069 --enable_warnings
1070 create procedure modes(out c1 int, out c2 int)
1071 begin
1072   declare done int default 0;
1073   declare x int;
1074   declare c cursor for select data from t1;
1075   declare continue handler for sqlstate '02000' set done = 1;
1077   select 1 || 2 into c1;
1078   set c2 = 0;
1079   open c;
1080   repeat
1081     fetch c into x;
1082     if not done then
1083       set c2 = c2 + 1;
1084     end if;
1085   until done end repeat;
1086   close c;
1087 end$
1088 delimiter |$
1089 set @@sql_mode = ''|
1091 set sql_select_limit = 1|
1092 call modes(@c1, @c2)|
1093 set sql_select_limit = default|
1095 select @c1, @c2|
1096 delete from t1|
1097 drop procedure modes|
1100 # Check that dropping a database without routines works.
1101 # (Dropping with routines is tested in sp-security.test)
1102 # First an empty db.
1103 create database sp_db1|
1104 drop database sp_db1|
1106 # Again, with a table.
1107 create database sp_db2|
1108 use sp_db2|
1109 # Just put something in here...
1110 create table t3 ( s char(4), t int )|
1111 insert into t3 values ("abcd", 42), ("dcba", 666)|
1112 use test|
1113 drop database sp_db2|
1115 # And yet again, with just a procedure.
1116 create database sp_db3|
1117 use sp_db3|
1118 --disable_warnings
1119 drop procedure if exists dummy|
1120 --enable_warnings
1121 create procedure dummy(out x int)
1122   set x = 42|
1123 use test|
1124 drop database sp_db3|
1125 # Check that it's gone
1126 select type,db,name from mysql.proc where db = 'sp_db3'|
1129 # ROW_COUNT() function after a CALL
1130 # We test the other cases here too, although it's not strictly SP specific
1131 --disable_warnings
1132 drop procedure if exists rc|
1133 --enable_warnings
1134 create procedure rc()
1135 begin
1136   delete from t1;
1137   insert into t1 values ("a", 1), ("b", 2), ("c", 3);
1138 end|
1140 call rc()|
1141 select row_count()|
1142 --disable_ps_protocol
1143 update t1 set data=42 where id = "b";
1144 select row_count()|
1145 --enable_ps_protocol
1146 delete from t1|
1147 select row_count()|
1148 delete from t1|
1149 select row_count()|
1150 select * from t1|
1151 select row_count()|
1152 drop procedure rc|
1156 # Let us test how well new locking scheme works.
1159 # Let us prepare playground
1160 --disable_warnings
1161 drop function if exists f0|
1162 drop function if exists f1|
1163 drop function if exists f2|
1164 drop function if exists f3|
1165 drop function if exists f4|
1166 drop function if exists f5|
1167 drop function if exists f6|
1168 drop function if exists f7|
1169 drop function if exists f8|
1170 drop function if exists f9|
1171 drop function if exists f10|
1172 drop function if exists f11|
1173 drop function if exists f12_1|
1174 drop function if exists f12_2|
1175 drop view if exists v0|
1176 drop view if exists v1|
1177 drop view if exists v2|
1178 --enable_warnings
1179 delete from t1|
1180 delete from t2|
1181 insert into t1 values ("a", 1), ("b", 2) |
1182 insert into t2 values ("a", 1, 1.0), ("b", 2, 2.0), ("c", 3, 3.0) |
1184 # Test the simplest function using tables
1185 create function f1() returns int
1186   return (select sum(data) from t1)|
1187 select f1()|
1188 # This should work too (and give 2 rows as result)
1189 select id, f1() from t1 order by id|
1191 # Function which uses two instances of table simultaneously
1192 create function f2() returns int
1193   return (select data from t1 where data <= (select sum(data) from t1) order by data limit 1)|
1194 select f2()|
1195 select id, f2() from t1 order by id|
1197 # Function which uses the same table twice in different queries
1198 create function f3() returns int
1199 begin
1200   declare n int;
1201   declare m int;
1202   set n:= (select min(data) from t1);
1203   set m:= (select max(data) from t1);
1204   return n < m;
1205 end|
1206 select f3()|
1207 select id, f3() from t1 order by id|
1209 # Calling two functions using same table
1210 select f1(), f3()|
1211 select id, f1(), f3() from t1 order by id|
1213 # Function which uses two different tables
1214 create function f4() returns double 
1215   return (select d from t1, t2 where t1.data = t2.i and t1.id= "b")|
1216 select f4()|
1217 select s, f4() from t2 order by s|
1219 # Recursive functions which due to this recursion require simultaneous
1220 # access to several instance of the same table won't work
1221 create function f5(i int) returns int
1222 begin
1223   if i <= 0 then
1224     return 0;
1225   elseif i = 1  then
1226     return (select count(*) from t1 where data = i);
1227   else
1228     return (select count(*) + f5( i - 1) from t1 where data = i);
1229   end if;
1230 end|
1231 select f5(1)|
1232 # Since currently recursive functions are disallowed ER_SP_NO_RECURSION
1233 # error will be returned, once we will allow them error about
1234 # insufficient number of locked tables will be returned instead.
1235 --error ER_SP_NO_RECURSION
1236 select f5(2)|
1237 --error ER_SP_NO_RECURSION
1238 select f5(3)|
1240 # OTOH this should work 
1241 create function f6() returns int
1242 begin
1243   declare n int;
1244   set n:= f1();
1245   return (select count(*) from t1 where data <= f7() and data <= n);
1246 end|
1247 create function f7() returns int
1248   return (select sum(data) from t1 where data <= f1())|
1249 select f6()|
1250 select id, f6() from t1 order by id|
1253 # Let us test how new locking work with views
1255 # The most trivial view
1256 create view v1 (a) as select f1()|
1257 select * from v1|
1258 select id, a from t1, v1 order by id|
1259 select * from v1, v1 as v|
1260 # A bit more complex construction
1261 create view v2 (a) as select a*10 from v1|
1262 select * from v2|
1263 select id, a from t1, v2 order by id|
1264 select * from v1, v2|
1266 # Nice example where the same view is used on
1267 # on different expression levels
1268 create function f8 () returns int
1269   return (select count(*) from v2)|
1271 select *, f8() from v1|
1273 # Let us test what will happen if function is missing
1274 drop function f1|
1275 --error ER_VIEW_INVALID
1276 select * from v1|
1278 # And what will happen if we have recursion which involves
1279 # views and functions ?
1280 create function f1() returns int
1281   return (select sum(data) from t1) + (select sum(data) from v1)|
1282 --error ER_SP_NO_RECURSION
1283 select f1()|
1284 --error ER_SP_NO_RECURSION
1285 select * from v1|
1286 --error ER_SP_NO_RECURSION
1287 select * from v2|
1288 # Back to the normal cases
1289 drop function f1|
1290 create function f1() returns int
1291   return (select sum(data) from t1)|
1293 # Let us also test some weird cases where no real tables is used
1294 create function f0() returns int
1295   return (select * from (select 100) as r)|
1296 select f0()|
1297 select *, f0() from (select 1) as t|
1298 create view v0 as select f0()|
1299 select * from v0|
1300 select *, f0() from v0|
1303 # Let us test how well prelocking works with explicit LOCK TABLES.
1305 lock tables t1 read, t1 as t11 read|
1306 # These should work well
1307 select f3()|
1308 select id, f3() from t1 as t11 order by id|
1309 # Degenerate cases work too :)
1310 select f0()|
1311 select * from v0|
1312 select *, f0() from v0, (select 123) as d1|
1313 # But these should not !
1314 --error ER_TABLE_NOT_LOCKED
1315 select id, f3() from t1|
1316 --error ER_TABLE_NOT_LOCKED
1317 select f4()|
1318 unlock tables|
1320 # Let us test how LOCK TABLES which implicitly depends on functions
1321 # works
1322 lock tables v2 read, mysql.proc read|
1323 select * from v2|
1324 select * from v1|
1325 # These should not work as we have too little instances of tables locked
1326 --error ER_TABLE_NOT_LOCKED
1327 select * from v1, t1|
1328 --error ER_TABLE_NOT_LOCKED
1329 select f4()|
1330 unlock tables|
1332 # Tests for handling of temporary tables in functions.
1334 # Unlike for permanent tables we should be able to create, use
1335 # and drop such tables in functions.
1337 # Simplest function using temporary table. It is also test case for bug 
1338 # #12198 "Temporary table aliasing does not work inside stored functions"
1339 create function f9() returns int
1340 begin
1341   declare a, b int;
1342   drop temporary table if exists t3;
1343   create temporary table t3 (id int);
1344   insert into t3 values (1), (2), (3);
1345   set a:= (select count(*) from t3);
1346   set b:= (select count(*) from t3 t3_alias);
1347   return a + b;
1348 end|
1349 # This will emit warning as t3 was not existing before.
1350 select f9()|
1351 select f9() from t1 limit 1|
1353 # Function which uses both temporary and permanent tables.
1354 create function f10() returns int
1355 begin
1356   drop temporary table if exists t3;
1357   create temporary table t3 (id int);
1358   insert into t3 select id from t4;
1359   return (select count(*) from t3);
1360 end|
1361 # Check that we don't ignore completely tables used in function
1362 --error ER_NO_SUCH_TABLE
1363 select f10()|
1364 create table t4 as select 1 as id|
1365 select f10()|
1367 # Practical cases which we don't handle well (yet)
1369 # Function which does not work because of well-known and documented
1370 # limitation of MySQL. We can't use the several instances of the
1371 # same temporary table in statement.
1372 create function f11() returns int
1373 begin
1374   drop temporary table if exists t3;
1375   create temporary table t3 (id int);
1376   insert into t3 values (1), (2), (3);
1377   return (select count(*) from t3 as a, t3 as b);
1378 end|
1379 --error ER_CANT_REOPEN_TABLE
1380 select f11()|
1381 --error ER_CANT_REOPEN_TABLE
1382 select f11() from t1|
1383 # Test that using a single table instance at a time works
1384 create function f12_1() returns int
1385 begin
1386   drop temporary table if exists t3;
1387   create temporary table t3 (id int);
1388   insert into t3 values (1), (2), (3);
1389   return f12_2();
1390 end|
1391 create function f12_2() returns int
1392   return (select count(*) from t3)|
1394 drop temporary table t3|
1395 select f12_1()|
1396 select f12_1() from t1 limit 1|
1398 # Cleanup
1399 drop function f0|
1400 drop function f1|
1401 drop function f2|
1402 drop function f3|
1403 drop function f4|
1404 drop function f5|
1405 drop function f6|
1406 drop function f7|
1407 drop function f8|
1408 drop function f9|
1409 drop function f10|
1410 drop function f11|
1411 drop function f12_1|
1412 drop function f12_2|
1413 drop view v0|
1414 drop view v1|
1415 drop view v2|
1416 truncate table t1 |
1417 truncate table t2 |
1418 drop table t4|
1420 # End of non-bug tests
1424 # Some "real" examples
1427 # fac
1429 --disable_warnings
1430 drop table if exists t3|
1431 --enable_warnings
1432 create table t3 (n int unsigned not null primary key, f bigint unsigned)|
1434 --disable_warnings
1435 drop procedure if exists ifac|
1436 --enable_warnings
1437 create procedure ifac(n int unsigned)
1438 begin
1439   declare i int unsigned default 1;
1441   if n > 20 then
1442     set n = 20;         # bigint overflow otherwise
1443   end if;
1444   while i <= n do
1445     begin
1446       insert into test.t3 values (i, fac(i));
1447       set i = i + 1;
1448     end;
1449   end while;
1450 end|
1452 call ifac(20)|
1453 select * from t3|
1454 drop table t3|
1455 --replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1456 show function status like '%f%'|
1457 drop procedure ifac|
1458 drop function fac|
1459 --replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1460 show function status like '%f%'|
1463 # primes
1465 --disable_warnings
1466 drop table if exists t3|
1467 --enable_warnings
1469 create table t3 (
1470   i int unsigned not null primary key,
1471   p bigint unsigned not null
1474 insert into t3 values
1475  ( 0,   3), ( 1,   5), ( 2,   7), ( 3,  11), ( 4,  13),
1476  ( 5,  17), ( 6,  19), ( 7,  23), ( 8,  29), ( 9,  31),
1477  (10,  37), (11,  41), (12,  43), (13,  47), (14,  53),
1478  (15,  59), (16,  61), (17,  67), (18,  71), (19,  73),
1479  (20,  79), (21,  83), (22,  89), (23,  97), (24, 101),
1480  (25, 103), (26, 107), (27, 109), (28, 113), (29, 127),
1481  (30, 131), (31, 137), (32, 139), (33, 149), (34, 151),
1482  (35, 157), (36, 163), (37, 167), (38, 173), (39, 179),
1483  (40, 181), (41, 191), (42, 193), (43, 197), (44, 199)|
1485 --disable_warnings
1486 drop procedure if exists opp|
1487 --enable_warnings
1488 create procedure opp(n bigint unsigned, out pp bool)
1489 begin
1490   declare r double;
1491   declare b, s bigint unsigned default 0;
1493   set r = sqrt(n);
1495  again:
1496   loop
1497     if s = 45 then
1498       set b = b+200, s = 0;
1499     else
1500       begin
1501         declare p bigint unsigned;
1503         select t.p into p from test.t3 t where t.i = s;
1504         if b+p > r then
1505           set pp = 1;
1506           leave again;
1507         end if;
1508         if mod(n, b+p) = 0 then
1509           set pp = 0;
1510           leave again;
1511         end if;
1512         set s = s+1;
1513       end;
1514     end if;
1515   end loop;
1516 end|
1518 --disable_warnings
1519 drop procedure if exists ip|
1520 --enable_warnings
1521 create procedure ip(m int unsigned)
1522 begin
1523   declare p bigint unsigned;
1524   declare i int unsigned;
1526   set i=45, p=201;
1528   while i < m do
1529     begin
1530       declare pp bool default 0;
1532       call opp(p, pp);
1533       if pp then
1534         insert into test.t3 values (i, p);
1535         set i = i+1;
1536       end if;
1537       set p = p+2;
1538     end;
1539   end while;
1540 end|
1541 show create procedure opp|
1542 --replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1543 show procedure status where name like '%p%' and db='test'|
1545 # This isn't the fastest way in the world to compute prime numbers, so
1546 # don't be too ambitious. ;-)
1547 call ip(200)|
1548 # We don't want to select the entire table here, just pick a few
1549 # examples.
1550 # The expected result is:
1551 #    i      p
1552 #   ---   ----
1553 #    45    211
1554 #   100    557
1555 #   199   1229
1556 select * from t3 where i=45 or i=100 or i=199|
1557 drop table t3|
1558 drop procedure opp|
1559 drop procedure ip|
1560 --replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1561 show procedure status where name like '%p%' and db='test'|
1565 # Comment & suid
1568 --disable_warnings
1569 drop procedure if exists bar|
1570 --enable_warnings
1571 create procedure bar(x char(16), y int)
1572  comment "111111111111" sql security invoker
1573  insert into test.t1 values (x, y)|
1574 --replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1575 show procedure status like 'bar'|
1576 alter procedure bar comment "2222222222" sql security definer|
1577 alter procedure bar comment "3333333333"|
1578 alter procedure bar|
1579 show create procedure bar|
1580 --replace_column 4 'root@localhost' 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
1581 show procedure status like 'bar'|
1582 drop procedure bar|
1585 # rexecution
1587 --disable_warnings
1588 drop procedure if exists p1|
1589 --enable_warnings
1590 create procedure p1 ()
1591   select (select s1 from t3) from t3|
1593 create table t3 (s1 int)|
1595 call p1()|
1596 insert into t3 values (1)|
1597 call p1()|
1598 drop procedure p1|
1599 drop table t3|
1602 # backticks
1604 --disable_warnings
1605 drop function if exists foo|
1606 --enable_warnings
1607 create function `foo` () returns int
1608   return 5|
1609 select `foo` ()|
1610 drop function `foo`|
1613 # Implicit LOCK/UNLOCK TABLES for table access in functions
1616 --disable_warnings
1617 drop function if exists t1max|
1618 --enable_warnings
1619 create function t1max() returns int
1620 begin
1621   declare x int;
1622   select max(data) into x from t1;
1623   return x;
1624 end|
1626 insert into t1 values ("foo", 3), ("bar", 2), ("zip", 5), ("zap", 1)|
1627 select t1max()|
1628 drop function t1max|
1630 create table t3 (
1631   v char(16) not null primary key,
1632   c int unsigned not null
1635 create function getcount(s char(16)) returns int
1636 begin
1637   declare x int;
1639   select count(*) into x from t3 where v = s;
1640   if x = 0 then
1641     insert into t3 values (s, 1);
1642   else
1643     update t3 set c = c+1 where v = s;
1644   end if;
1645   return x;
1646 end|
1647 select * from t1 where data = getcount("bar")|
1648 select * from t3|
1649 select getcount("zip")|
1650 select getcount("zip")|
1651 select * from t3|
1652 select getcount(id) from t1 where data = 3|
1653 select getcount(id) from t1 where data = 5|
1654 select * from t3|
1655 drop table t3|
1656 drop function getcount|
1659 # Test cases for different combinations of condition handlers in nested
1660 # begin-end blocks in stored procedures.
1662 # Note that the standard specifies that the most specific handler should
1663 # be triggered even if it's an outer handler masked by a less specific
1664 # handler in an inner block.
1665 # Note also that '02000' is more specific than NOT FOUND; there might be
1666 # other '02xxx' states, even if we currently do not issue them in any
1667 # situation (e.g. '02001').
1669 # The combinations we test are these:
1671 #                                         Inner
1672 #              errcode      sqlstate     not found    sqlwarning   sqlexception
1673 #  Outer      +------------+------------+------------+------------+------------+
1674 #errcode      | h_ee (i)   | h_es (o)   | h_en (o)   | h_ew (o)   | h_ex (o)   |
1675 #sqlstate     | h_se (i)   | h_ss (i)   | h_sn (o)   | h_sw (o)   | h_sx (o)   |
1676 #not found    | h_ne (i)   | h_ns (i)   | h_nn (i)   |            |            |
1677 #sqlwarning   | h_we (i)   | h_ws (i)   |            | h_ww (i)   |            |
1678 #sqlexception | h_xe (i)   | h_xs (i)   |            |            | h_xx (i)   |
1679 #             +------------+---------------------------------------------------+
1681 # (i) means that the inner handler is the one that should be invoked,
1682 # (o) means that the outer handler should be invoked.
1684 # ('not found', 'sqlwarning' and 'sqlexception' are mutually exclusive, hence
1685 #  no tests for those combinations.)
1688 --disable_warnings
1689 drop table if exists t3|
1690 drop procedure if exists h_ee|
1691 drop procedure if exists h_es|
1692 drop procedure if exists h_en|
1693 drop procedure if exists h_ew|
1694 drop procedure if exists h_ex|
1695 drop procedure if exists h_se|
1696 drop procedure if exists h_ss|
1697 drop procedure if exists h_sn|
1698 drop procedure if exists h_sw|
1699 drop procedure if exists h_sx|
1700 drop procedure if exists h_ne|
1701 drop procedure if exists h_ns|
1702 drop procedure if exists h_nn|
1703 drop procedure if exists h_we|
1704 drop procedure if exists h_ws|
1705 drop procedure if exists h_ww|
1706 drop procedure if exists h_xe|
1707 drop procedure if exists h_xs|
1708 drop procedure if exists h_xx|
1709 --enable_warnings
1711 # smallint    - to get out of range warnings
1712 # primary key - to get constraint errors
1713 create table t3 (a smallint primary key)|
1715 insert into t3 (a) values (1)|
1717 create procedure h_ee()
1718     deterministic
1719 begin
1720   declare continue handler for 1062 -- ER_DUP_ENTRY
1721     select 'Outer (bad)' as 'h_ee';
1723   begin
1724     declare continue handler for 1062 -- ER_DUP_ENTRY
1725         select 'Inner (good)' as 'h_ee';
1727     insert into t3 values (1);
1728   end;
1729 end|
1731 create procedure h_es()
1732     deterministic
1733 begin
1734   declare continue handler for 1062 -- ER_DUP_ENTRY
1735     select 'Outer (good)' as 'h_es';
1737   begin
1738     -- integrity constraint violation
1739     declare continue handler for sqlstate '23000'
1740       select 'Inner (bad)' as 'h_es';
1742     insert into t3 values (1);
1743   end;
1744 end|
1746 create procedure h_en()
1747     deterministic
1748 begin
1749   declare continue handler for 1329 -- ER_SP_FETCH_NO_DATA
1750     select 'Outer (good)' as 'h_en';
1752   begin
1753     declare x int;
1754     declare continue handler for sqlstate '02000' -- no data
1755       select 'Inner (bad)' as 'h_en';
1757     select a into x from t3 where a = 42;
1758   end;
1759 end|
1761 create procedure h_ew()
1762     deterministic
1763 begin
1764   declare continue handler for 1264 -- ER_WARN_DATA_OUT_OF_RANGE
1765     select 'Outer (good)' as 'h_ew';
1767   begin
1768     declare continue handler for sqlwarning
1769       select 'Inner (bad)' as 'h_ew';
1771     insert into t3 values (123456789012);
1772   end;
1773   delete from t3;
1774   insert into t3 values (1);
1775 end|
1777 create procedure h_ex()
1778     deterministic
1779 begin
1780   declare continue handler for 1062 -- ER_DUP_ENTRY
1781     select 'Outer (good)' as 'h_ex';
1783   begin
1784     declare continue handler for sqlexception
1785       select 'Inner (bad)' as 'h_ex';
1787     insert into t3 values (1);
1788   end;
1789 end|
1791 create procedure h_se()
1792     deterministic
1793 begin
1794   -- integrity constraint violation
1795   declare continue handler for sqlstate '23000' 
1796     select 'Outer (bad)' as 'h_se';
1798   begin
1799     declare continue handler for 1062 -- ER_DUP_ENTRY
1800       select 'Inner (good)' as 'h_se';
1802     insert into t3 values (1);
1803   end;
1804 end|
1806 create procedure h_ss()
1807     deterministic
1808 begin
1809   -- integrity constraint violation
1810   declare continue handler for sqlstate '23000' 
1811     select 'Outer (bad)' as 'h_ss';
1813   begin
1814     -- integrity constraint violation
1815     declare continue handler for sqlstate '23000' 
1816       select 'Inner (good)' as 'h_ss';
1818     insert into t3 values (1);
1819   end;
1820 end|
1822 create procedure h_sn()
1823     deterministic
1824 begin
1825   -- Note: '02000' is more specific than NOT FOUND ;
1826   --       there might be other not found states 
1827   declare continue handler for sqlstate '02000' -- no data
1828     select 'Outer (good)' as 'h_sn';
1830   begin
1831     declare x int;
1832     declare continue handler for not found
1833       select 'Inner (bad)' as 'h_sn';
1835     select a into x from t3 where a = 42;
1836   end;
1837 end|
1839 create procedure h_sw()
1840     deterministic
1841 begin
1842   -- data exception - numeric value out of range
1843   declare continue handler for sqlstate '22003'
1844     select 'Outer (good)' as 'h_sw';
1846   begin
1847     declare continue handler for sqlwarning
1848       select 'Inner (bad)' as 'h_sw';
1850     insert into t3 values (123456789012);
1851   end;
1852   delete from t3;
1853   insert into t3 values (1);
1854 end|
1856 create procedure h_sx()
1857     deterministic
1858 begin
1859   -- integrity constraint violation
1860   declare continue handler for sqlstate '23000' 
1861     select 'Outer (good)' as 'h_sx';
1863   begin
1864     declare continue handler for sqlexception
1865       select 'Inner (bad)' as 'h_sx';
1867     insert into t3 values (1);
1868   end;
1869 end|
1871 create procedure h_ne()
1872     deterministic
1873 begin
1874   declare continue handler for not found
1875     select 'Outer (bad)' as 'h_ne';
1877   begin
1878     declare x int;
1879     declare continue handler for 1329 -- ER_SP_FETCH_NO_DATA
1880       select 'Inner (good)' as 'h_ne';
1882     select a into x from t3 where a = 42;
1883   end;
1884 end|
1886 create procedure h_ns()
1887     deterministic
1888 begin
1889   declare continue handler for not found
1890     select 'Outer (bad)' as 'h_ns';
1892   begin
1893     declare x int;
1894     declare continue handler for sqlstate '02000' -- no data
1895       select 'Inner (good)' as 'h_ns';
1897     select a into x from t3 where a = 42;
1898   end;
1899 end|
1901 create procedure h_nn()
1902     deterministic
1903 begin
1904   declare continue handler for not found
1905     select 'Outer (bad)' as 'h_nn';
1907   begin
1908     declare x int;
1909     declare continue handler for not found
1910       select 'Inner (good)' as 'h_nn';
1912     select a into x from t3 where a = 42;
1913   end;
1914 end|
1916 create procedure h_we()
1917     deterministic
1918 begin
1919   declare continue handler for sqlwarning
1920     select 'Outer (bad)' as 'h_we';
1922   begin
1923     declare continue handler for 1264 -- ER_WARN_DATA_OUT_OF_RANGE
1924       select 'Inner (good)' as 'h_we';
1926     insert into t3 values (123456789012);
1927   end;
1928   delete from t3;
1929   insert into t3 values (1);
1930 end|
1932 create procedure h_ws()
1933     deterministic
1934 begin
1935   declare continue handler for sqlwarning
1936     select 'Outer (bad)' as 'h_ws';
1938   begin
1939     -- data exception - numeric value out of range
1940     declare continue handler for sqlstate '22003'
1941       select 'Inner (good)' as 'h_ws';
1943     insert into t3 values (123456789012);
1944   end;
1945   delete from t3;
1946   insert into t3 values (1);
1947 end|
1949 create procedure h_ww()
1950     deterministic
1951 begin
1952   declare continue handler for sqlwarning
1953     select 'Outer (bad)' as 'h_ww';
1955   begin
1956     declare continue handler for sqlwarning
1957       select 'Inner (good)' as 'h_ww';
1959     insert into t3 values (123456789012);
1960   end;
1961   delete from t3;
1962   insert into t3 values (1);
1963 end|
1965 create procedure h_xe()
1966     deterministic
1967 begin
1968   declare continue handler for sqlexception
1969     select 'Outer (bad)' as 'h_xe';
1971   begin
1972     declare continue handler for 1062 -- ER_DUP_ENTRY
1973       select 'Inner (good)' as 'h_xe';
1975     insert into t3 values (1);
1976   end;
1977 end|
1979 create procedure h_xs()
1980     deterministic
1981 begin
1982   declare continue handler for sqlexception
1983     select 'Outer (bad)' as 'h_xs';
1985   begin
1986     -- integrity constraint violation
1987     declare continue handler for sqlstate '23000'
1988       select 'Inner (good)' as 'h_xs';
1990     insert into t3 values (1);
1991   end;
1992 end|
1994 create procedure h_xx()
1995     deterministic
1996 begin
1997   declare continue handler for sqlexception
1998     select 'Outer (bad)' as 'h_xx';
2000   begin
2001     declare continue handler for sqlexception
2002       select 'Inner (good)' as 'h_xx';
2004     insert into t3 values (1);
2005   end;
2006 end|
2008 call h_ee()|
2009 call h_es()|
2010 call h_en()|
2011 call h_ew()|
2012 call h_ex()|
2013 call h_se()|
2014 call h_ss()|
2015 call h_sn()|
2016 call h_sw()|
2017 call h_sx()|
2018 call h_ne()|
2019 call h_ns()|
2020 call h_nn()|
2021 call h_we()|
2022 call h_ws()|
2023 call h_ww()|
2024 call h_xe()|
2025 call h_xs()|
2026 call h_xx()|
2028 drop table t3|
2029 drop procedure h_ee|
2030 drop procedure h_es|
2031 drop procedure h_en|
2032 drop procedure h_ew|
2033 drop procedure h_ex|
2034 drop procedure h_se|
2035 drop procedure h_ss|
2036 drop procedure h_sn|
2037 drop procedure h_sw|
2038 drop procedure h_sx|
2039 drop procedure h_ne|
2040 drop procedure h_ns|
2041 drop procedure h_nn|
2042 drop procedure h_we|
2043 drop procedure h_ws|
2044 drop procedure h_ww|
2045 drop procedure h_xe|
2046 drop procedure h_xs|
2047 drop procedure h_xx|
2051 # Test cases for old bugs
2055 # BUG#822
2057 --disable_warnings
2058 drop procedure if exists bug822|
2059 --enable_warnings
2060 create procedure bug822(a_id char(16), a_data int)
2061 begin
2062   declare n int;
2063   select count(*) into n from t1 where id = a_id and data = a_data;
2064   if n = 0 then
2065     insert into t1 (id, data) values (a_id, a_data);
2066   end if;
2067 end|
2069 delete from t1|
2070 call bug822('foo', 42)|
2071 call bug822('foo', 42)|
2072 call bug822('bar', 666)|
2073 select * from t1 order by data|
2074 delete from t1|
2075 drop procedure bug822|
2078 # BUG#1495
2080 --disable_warnings
2081 drop procedure if exists bug1495|
2082 --enable_warnings
2083 create procedure bug1495()
2084 begin
2085   declare x int;
2087   select data into x from t1 order by id limit 1;
2088   if x > 10 then
2089     insert into t1 values ("less", x-10);
2090   else
2091     insert into t1 values ("more", x+10);
2092   end if;
2093 end|
2095 insert into t1 values ('foo', 12)|
2096 call bug1495()|
2097 delete from t1 where id='foo'|
2098 insert into t1 values ('bar', 7)|
2099 call bug1495()|
2100 delete from t1 where id='bar'|
2101 select * from t1 order by data|
2102 delete from t1|
2103 drop procedure bug1495|
2106 # BUG#1547
2108 --disable_warnings
2109 drop procedure if exists bug1547|
2110 --enable_warnings
2111 create procedure bug1547(s char(16))
2112 begin
2113   declare x int;
2115   select data into x from t1 where s = id limit 1;
2116   if x > 10 then
2117     insert into t1 values ("less", x-10);
2118   else
2119     insert into t1 values ("more", x+10);
2120   end if;
2121 end|
2123 insert into t1 values ("foo", 12), ("bar", 7)|
2124 call bug1547("foo")|
2125 call bug1547("bar")|
2126 select * from t1 order by id|
2127 delete from t1|
2128 drop procedure bug1547|
2131 # BUG#1656
2133 --disable_warnings
2134 drop table if exists t70|
2135 --enable_warnings
2136 create table t70 (s1 int,s2 int)|
2137 insert into t70 values (1,2)|
2139 --disable_warnings
2140 drop procedure if exists bug1656|
2141 --enable_warnings
2142 create procedure bug1656(out p1 int, out p2 int)
2143   select * into p1, p1 from t70|
2145 call bug1656(@1, @2)|
2146 select @1, @2|
2147 drop table t70|
2148 drop procedure bug1656|
2151 # BUG#1862
2153 create table t3(a int)|
2155 --disable_warnings
2156 drop procedure if exists bug1862|
2157 --enable_warnings
2158 create procedure bug1862()
2159 begin
2160   insert into t3 values(2);    
2161   flush tables;
2162 end|
2164 call bug1862()|
2165 # the second call caused a segmentation
2166 call bug1862()|
2167 select * from t3|
2168 drop table t3|
2169 drop procedure bug1862|
2172 # BUG#1874
2174 --disable_warnings
2175 drop procedure if exists bug1874|
2176 --enable_warnings
2177 create procedure bug1874()
2178 begin
2179   declare x int;
2180   declare y double;
2181   select max(data) into x from t1;
2182   insert into t2 values ("max", x, 0);
2183   select min(data) into x from t1;
2184   insert into t2 values ("min", x, 0);
2185   select sum(data) into x from t1;
2186   insert into t2 values ("sum", x, 0);
2187   select avg(data) into y from t1;
2188   insert into t2 values ("avg", 0, y);
2189 end|
2191 insert into t1 (data) values (3), (1), (5), (9), (4)|
2192 call bug1874()|
2193 select * from t2 order by i|
2194 delete from t1|
2195 delete from t2|
2196 drop procedure bug1874|
2199 # BUG#2260
2201 --disable_warnings
2202 drop procedure if exists bug2260|
2203 --enable_warnings
2204 create procedure bug2260()
2205 begin
2206   declare v1 int;
2207   declare c1 cursor for select data from t1;
2208   declare continue handler for not found set @x2 = 1;
2210   open c1;
2211   fetch c1 into v1;
2212   set @x2 = 2;
2213   close c1;
2214 end|
2216 call bug2260()|
2217 select @x2|
2218 drop procedure bug2260|
2221 # BUG#2267 "Lost connect if stored procedure has SHOW FUNCTION STATUS"
2223 --disable_warnings
2224 drop procedure if exists bug2267_1|
2225 --enable_warnings
2226 create procedure bug2267_1()
2227 begin
2228   show procedure status where db='test';
2229 end|
2231 --disable_warnings
2232 drop procedure if exists bug2267_2|
2233 --enable_warnings
2234 create procedure bug2267_2()
2235 begin
2236   show function status where db='test';
2237 end|
2239 --disable_warnings
2240 drop procedure if exists bug2267_3|
2241 --enable_warnings
2242 create procedure bug2267_3()
2243 begin
2244   show create procedure bug2267_1;
2245 end|
2247 --disable_warnings
2248 drop procedure if exists bug2267_4|
2249 drop function if exists bug2267_4|
2250 --enable_warnings
2251 create procedure bug2267_4()
2252 begin
2253   show create function bug2267_4;
2254 end|
2255 create function bug2267_4() returns int return 100|
2257 --replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
2258 call bug2267_1()|
2259 --replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
2260 call bug2267_2()|
2261 call bug2267_3()|
2262 call bug2267_4()|
2264 drop procedure bug2267_1|
2265 drop procedure bug2267_2|
2266 drop procedure bug2267_3|
2267 drop procedure bug2267_4|
2268 drop function bug2267_4|
2271 # BUG#2227
2273 --disable_warnings
2274 drop procedure if exists bug2227|
2275 --enable_warnings
2276 create procedure bug2227(x int)
2277 begin
2278   declare y float default 2.6;
2279   declare z char(16) default "zzz";
2281   select 1.3, x, y, 42, z;
2282 end|
2284 call bug2227(9)|
2285 drop procedure bug2227|
2288 # BUG#2614 "Stored procedure with INSERT ... SELECT that does not
2289 #           contain any tables crashes server"
2291 --disable_warnings
2292 drop procedure if exists bug2614|
2293 --enable_warnings
2294 create procedure bug2614()
2295 begin
2296   drop table if exists t3;
2297   create table t3 (id int default '0' not null);
2298   insert into t3 select 12;
2299   insert into t3 select * from t3;
2300 end|
2302 --disable_warnings
2303 call bug2614()|
2304 --enable_warnings
2305 call bug2614()|
2306 drop table t3|
2307 drop procedure bug2614|
2310 # BUG#2674
2312 --disable_warnings
2313 drop function if exists bug2674|
2314 --enable_warnings
2315 create function bug2674() returns int
2316   return @@sort_buffer_size|
2318 set @osbs = @@sort_buffer_size|
2319 set @@sort_buffer_size = 262000|
2320 select bug2674()|
2321 drop function bug2674|
2322 set @@sort_buffer_size = @osbs|
2325 # BUG#3259
2327 --disable_warnings
2328 drop procedure if exists bug3259_1 |
2329 --enable_warnings
2330 create procedure bug3259_1 () begin end|
2331 --disable_warnings
2332 drop procedure if exists BUG3259_2 |
2333 --enable_warnings
2334 create procedure BUG3259_2 () begin end|
2335 --disable_warnings
2336 drop procedure if exists Bug3259_3 |
2337 --enable_warnings
2338 create procedure Bug3259_3 () begin end|
2340 call BUG3259_1()|
2341 call BUG3259_1()|
2342 call bug3259_2()|
2343 call Bug3259_2()|
2344 call bug3259_3()|
2345 call bUG3259_3()|
2347 drop procedure bUg3259_1|
2348 drop procedure BuG3259_2|
2349 drop procedure BUG3259_3|
2352 # BUG#2772
2354 --disable_warnings
2355 drop function if exists bug2772|
2356 --enable_warnings
2357 create function bug2772() returns char(10) character set latin2
2358   return 'a'|
2360 select bug2772()|
2361 drop function bug2772|
2364 # BUG#2776
2366 --disable_warnings
2367 drop procedure if exists bug2776_1|
2368 --enable_warnings
2369 create procedure bug2776_1(out x int)
2370 begin
2371   declare v int;
2373   set v = default;
2374   set x = v;
2375 end|
2377 --disable_warnings
2378 drop procedure if exists bug2776_2|
2379 --enable_warnings
2380 create procedure bug2776_2(out x int)
2381 begin
2382   declare v int default 42;
2384   set v = default;
2385   set x = v;
2386 end|
2388 set @x = 1|
2389 call bug2776_1(@x)|
2390 select @x|
2391 call bug2776_2(@x)|
2392 select @x|
2393 drop procedure bug2776_1|
2394 drop procedure bug2776_2|
2397 # BUG#2780
2399 create table t3 (s1 smallint)|
2401 insert into t3 values (123456789012)|
2403 --disable_warnings
2404 drop procedure if exists bug2780|
2405 --enable_warnings
2406 create procedure bug2780()
2407 begin
2408   declare exit handler for sqlwarning set @x = 1; 
2410   set @x = 0;
2411   insert into t3 values (123456789012);
2412   insert into t3 values (0);
2413 end|
2415 call bug2780()|
2416 select @x|
2417 select * from t3|
2419 drop procedure bug2780|
2420 drop table t3|
2423 # BUG#1863
2425 create table t3 (content varchar(10) )|
2426 insert into t3 values ("test1")|
2427 insert into t3 values ("test2")|
2428 create table t4 (f1 int, rc int, t3 int)|
2430 --disable_warnings
2431 drop procedure if exists bug1863|
2432 --enable_warnings
2433 create procedure bug1863(in1 int)
2434 begin 
2436   declare ind int default 0;
2437   declare t1 int;
2438   declare t2 int;
2439   declare t3 int;
2441   declare rc int default 0;
2442   declare continue handler for 1065 set rc = 1;
2444   drop temporary table if exists temp_t1;
2445   create temporary table temp_t1 (
2446     f1 int auto_increment, f2 varchar(20), primary key (f1)
2447   );
2449   insert into temp_t1 (f2) select content from t3;
2451   select f2 into t3 from temp_t1 where f1 = 10;
2453   if (rc) then
2454        insert into t4 values (1, rc, t3);
2455   end if;
2457   insert into t4 values (2, rc, t3);
2459 end|
2461 call bug1863(10)|
2462 call bug1863(10)|
2463 select * from t4|
2465 drop procedure bug1863|
2466 drop temporary table temp_t1;
2467 drop table t3, t4|
2470 # BUG#2656
2473 create table t3 ( 
2474   OrderID  int not null,
2475   MarketID int,
2476   primary key (OrderID)
2479 create table t4 ( 
2480   MarketID int not null,
2481   Market varchar(60),
2482   Status char(1),
2483   primary key (MarketID)
2486 insert t3 (OrderID,MarketID) values (1,1)|
2487 insert t3 (OrderID,MarketID) values (2,2)|
2488 insert t4 (MarketID,Market,Status) values (1,"MarketID One","A")|
2489 insert t4 (MarketID,Market,Status) values (2,"MarketID Two","A")|
2491 --disable_warnings
2492 drop procedure if exists bug2656_1|
2493 --enable_warnings
2494 create procedure bug2656_1()
2495 begin 
2496   select
2497     m.Market
2498   from  t4 m JOIN t3 o 
2499         ON o.MarketID != 1 and o.MarketID = m.MarketID;
2500 end |
2502 --disable_warnings
2503 drop procedure if exists bug2656_2|
2504 --enable_warnings
2505 create procedure bug2656_2()
2506 begin 
2507   select
2508     m.Market
2509   from  
2510     t4 m, t3 o
2511   where       
2512     m.MarketID != 1 and m.MarketID = o.MarketID;
2513         
2514 end |
2516 call bug2656_1()|
2517 call bug2656_1()|
2518 call bug2656_2()|
2519 call bug2656_2()|
2520 drop procedure bug2656_1|
2521 drop procedure bug2656_2|
2522 drop table t3, t4|
2526 # BUG#3426
2528 --disable_warnings
2529 drop procedure if exists bug3426|
2530 --enable_warnings
2531 create procedure bug3426(in_time int unsigned, out x int)
2532 begin
2533   if in_time is null then
2534     set @stamped_time=10;
2535     set x=1;
2536   else
2537     set @stamped_time=in_time;
2538     set x=2;
2539   end if;
2540 end|
2542 # so that from_unixtime() has a deterministic result
2543 set time_zone='+03:00';
2545 call bug3426(1000, @i)|
2546 select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2547 call bug3426(NULL, @i)|
2548 select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2549 # Clear SP cache
2550 alter procedure bug3426 sql security invoker|
2551 call bug3426(NULL, @i)|
2552 select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2553 call bug3426(1000, @i)|
2554 select @i, from_unixtime(@stamped_time, '%d-%m-%Y %h:%i:%s') as time|
2556 drop procedure bug3426|
2559 # BUG#3734
2561 create table t3 (
2562   id int unsigned auto_increment not null primary key,
2563   title VARCHAR(200),
2564   body text,
2565   fulltext (title,body)
2568 insert into t3 (title,body) values
2569   ('MySQL Tutorial','DBMS stands for DataBase ...'),
2570   ('How To Use MySQL Well','After you went through a ...'),
2571   ('Optimizing MySQL','In this tutorial we will show ...'),
2572   ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
2573   ('MySQL vs. YourSQL','In the following database comparison ...'),
2574   ('MySQL Security','When configured properly, MySQL ...')|
2576 --disable_warnings
2577 drop procedure if exists bug3734 |
2578 --enable_warnings
2579 create procedure bug3734 (param1 varchar(100))
2580   select * from t3 where match (title,body) against (param1)|
2582 call bug3734('database')|
2583 call bug3734('Security')|
2585 drop procedure bug3734|
2586 drop table t3|
2589 # BUG#3863
2591 --disable_warnings
2592 drop procedure if exists bug3863|
2593 --enable_warnings
2594 create procedure bug3863()
2595 begin
2596   set @a = 0;
2597   while @a < 5 do
2598     set @a = @a + 1;
2599   end while;
2600 end|
2602 call bug3863()|
2603 select @a|
2604 call bug3863()|
2605 select @a|
2607 drop procedure bug3863|
2610 # BUG#2460
2613 create table t3 (
2614   id int(10) unsigned not null default 0,
2615   rid int(10) unsigned not null default 0,
2616   msg text not null,
2617   primary key (id),
2618   unique key rid (rid, id)
2621 --disable_warnings
2622 drop procedure if exists bug2460_1|
2623 --enable_warnings
2624 create procedure bug2460_1(in v int)
2625 begin
2626     ( select n0.id from t3 as n0 where n0.id = v )
2627   union
2628     ( select n0.id from t3 as n0, t3 as n1
2629         where n0.id = n1.rid and n1.id = v )
2630   union
2631     ( select n0.id from t3 as n0, t3 as n1, t3 as n2
2632         where n0.id = n1.rid and n1.id = n2.rid and n2.id = v );
2633 end|
2635 call bug2460_1(2)|
2636 call bug2460_1(2)|
2637 insert into t3 values (1, 1, 'foo'), (2, 1, 'bar'), (3, 1, 'zip zap')|
2638 call bug2460_1(2)|
2639 call bug2460_1(2)|
2641 --disable_warnings
2642 drop procedure if exists bug2460_2|
2643 --enable_warnings
2644 create procedure bug2460_2()
2645 begin
2646   drop table if exists t3;
2647   create temporary table t3 (s1 int);
2648   insert into t3 select 1 union select 1;
2649 end|
2651 call bug2460_2()|
2652 call bug2460_2()|
2653 select * from t3|
2655 drop procedure bug2460_1|
2656 drop procedure bug2460_2|
2657 drop table t3|
2661 # BUG#2564
2663 set @@sql_mode = ''|
2664 --disable_warnings
2665 drop procedure if exists bug2564_1|
2666 --enable_warnings
2667 create procedure bug2564_1()
2668     comment 'Joe''s procedure'
2669   insert into `t1` values ("foo", 1)|
2671 set @@sql_mode = 'ANSI_QUOTES'|
2672 --disable_warnings
2673 drop procedure if exists bug2564_2|
2674 --enable_warnings
2675 create procedure bug2564_2()
2676   insert into "t1" values ('foo', 1)|
2678 delimiter $|
2679 set @@sql_mode = ''$
2680 --disable_warnings
2681 drop function if exists bug2564_3$
2682 --enable_warnings
2683 create function bug2564_3(x int, y int) returns int
2684   return x || y$
2686 set @@sql_mode = 'ANSI'$
2687 --disable_warnings
2688 drop function if exists bug2564_4$
2689 --enable_warnings
2690 create function bug2564_4(x int, y int) returns int
2691   return x || y$
2692 delimiter |$
2694 set @@sql_mode = ''|
2695 show create procedure bug2564_1|
2696 show create procedure bug2564_2|
2697 show create function bug2564_3|
2698 show create function bug2564_4|
2700 drop procedure bug2564_1|
2701 drop procedure bug2564_2|
2702 drop function bug2564_3|
2703 drop function bug2564_4|
2706 # BUG#3132
2708 --disable_warnings
2709 drop function if exists bug3132|
2710 --enable_warnings
2711 create function bug3132(s char(20)) returns char(50)
2712   return concat('Hello, ', s, '!')|
2714 select bug3132('Bob') union all select bug3132('Judy')|
2715 drop function bug3132|
2718 # BUG#3843
2720 --disable_warnings
2721 drop procedure if exists bug3843|
2722 --enable_warnings
2723 create procedure bug3843()
2724   analyze table t1|
2726 # Testing for packets out of order
2727 call bug3843()|
2728 call bug3843()|
2729 select 1+2|
2731 drop procedure bug3843|
2734 # BUG#3368
2736 create table t3 ( s1 char(10) )|
2737 insert into t3 values ('a'), ('b')|
2739 --disable_warnings
2740 drop procedure if exists bug3368|
2741 --enable_warnings
2742 create procedure bug3368(v char(10))
2743 begin
2744   select group_concat(v) from t3;
2745 end|
2747 call bug3368('x')|
2748 call bug3368('yz')|
2749 drop procedure bug3368|
2750 drop table t3|
2753 # BUG#4579
2755 create table t3 (f1 int, f2 int)|
2756 insert into t3 values (1,1)|
2758 --disable_warnings
2759 drop procedure if exists bug4579_1|
2760 --enable_warnings
2761 create procedure bug4579_1 ()
2762 begin
2763   declare sf1 int;
2765   select f1 into sf1 from t3 where f1=1 and f2=1;
2766   update t3 set f2 = f2 + 1 where f1=1 and f2=1;
2767   call bug4579_2();
2768 end|
2770 --disable_warnings
2771 drop procedure if exists bug4579_2|
2772 --enable_warnings
2773 create procedure bug4579_2 ()
2774 begin
2775 end|
2777 call bug4579_1()|
2778 call bug4579_1()|
2779 call bug4579_1()|
2781 drop procedure bug4579_1|
2782 drop procedure bug4579_2|
2783 drop table t3|
2786 # BUG#2773: Function's data type ignored in stored procedures
2788 --disable_warnings
2789 drop procedure if exists bug2773|
2790 --enable_warnings
2792 create function bug2773() returns int return null|
2793 create table t3 as select bug2773()|
2794 show create table t3|
2795 drop table t3|
2796 drop function bug2773|
2799 # BUG#3788: Stored procedure packet error
2801 --disable_warnings
2802 drop procedure if exists bug3788|
2803 --enable_warnings
2805 create function bug3788() returns date return cast("2005-03-04" as date)|
2806 select bug3788()|
2807 drop function bug3788|
2809 create function bug3788() returns binary(1) return 5|
2810 select bug3788()|
2811 drop function bug3788|
2815 # BUG#4726
2817 create table t3 (f1 int, f2 int, f3 int)|
2818 insert into t3 values (1,1,1)|
2820 --disable_warnings
2821 drop procedure if exists bug4726|
2822 --enable_warnings
2823 create procedure bug4726()
2824 begin
2825    declare tmp_o_id INT;
2826    declare tmp_d_id INT default 1;
2828    while tmp_d_id <= 2 do
2829    begin
2830      select f1 into tmp_o_id from t3 where f2=1 and f3=1;
2831      set tmp_d_id = tmp_d_id + 1;
2832    end;
2833    end while;
2834 end|
2836 call bug4726()|
2837 call bug4726()|
2838 call bug4726()|
2840 drop procedure bug4726|
2841 drop table t3|
2844 # BUG#4318
2847 --disable_parsing # Don't know if HANDLER commands can work with SPs, or at all..
2848 create table t3 (s1 int)|
2849 insert into t3 values (3), (4)|
2851 --disable_warnings
2852 drop procedure if exists bug4318|
2853 --enable_warnings
2854 create procedure bug4318()
2855   handler t3 read next|
2857 handler t3 open|
2858 # Expect no results, as tables are closed, but there shouldn't be any errors
2859 call bug4318()|
2860 call bug4318()|
2861 handler t3 close|
2863 drop procedure bug4318|
2864 drop table t3|
2865 --enable_parsing
2868 # BUG#4902: Stored procedure with SHOW WARNINGS leads to packet error
2870 # Added tests for most other show commands we could find too.
2871 # (Skipping those already tested, and the ones depending on optional handlers.)
2873 # Note: This will return a large number of results of different formats,
2874 #       which makes it impossible to filter with --replace_column.
2875 #       It's possible that some of these are not deterministic across
2876 #       platforms. If so, just remove the offending command.
2878 --disable_warnings
2879 drop procedure if exists bug4902|
2880 --enable_warnings
2881 create procedure bug4902()
2882 begin
2883   show charset like 'foo';
2884   show collation like 'foo';
2885   show column types;
2886   show create table t1;
2887   show create database test;
2888   show databases like 'foo';
2889   show errors;
2890   show columns from t1;
2891   show keys from t1;
2892   show open tables like 'foo';
2893   # Removed because result will differ in embedded mode.
2894   #show privileges;
2895   show status like 'foo';
2896   show tables like 'foo';
2897   show variables like 'foo';
2898   show warnings;
2899 end|
2900 --disable_parsing
2901 --replace_regex /table_id: [0-9]+/table_id: #/
2902 show binlog events|
2903 show storage engines|
2904 show master status|
2905 show slave hosts|
2906 show slave status|
2907 --enable_parsing
2909 call bug4902()|
2910 call bug4902()|
2912 drop procedure bug4902|
2915 # BUG#4904
2917 --disable_warnings
2918 drop procedure if exists bug4904|
2919 --enable_warnings
2920 create procedure bug4904()
2921 begin
2922   declare continue handler for sqlstate 'HY000' begin end;
2924   create table t2 as select * from t3;
2925 end|
2927 -- error 1146
2928 call bug4904()|
2930 drop procedure bug4904|
2932 create table t3 (s1 char character set latin1, s2 char character set latin2)|
2934 --disable_warnings
2935 drop procedure if exists bug4904|
2936 --enable_warnings
2937 create procedure bug4904 ()
2938 begin
2939   declare continue handler for sqlstate 'HY000' begin end;
2941   select s1 from t3 union select s2 from t3; 
2942 end|
2944 call bug4904()|
2946 drop procedure bug4904|
2947 drop table t3|
2950 # BUG#336
2952 --disable_warnings
2953 drop procedure if exists bug336|
2954 --enable_warnings
2955 create procedure bug336(out y int)
2956 begin
2957   declare x int;
2958   set x = (select sum(t.data) from test.t1 t);
2959   set y = x;
2960 end|
2962 insert into t1 values ("a", 2), ("b", 3)|
2963 call bug336(@y)|
2964 select @y|
2965 delete from t1|
2966 drop procedure bug336|
2969 # BUG#3157
2971 --disable_warnings
2972 drop procedure if exists bug3157|
2973 --enable_warnings
2974 create procedure bug3157()
2975 begin
2976   if exists(select * from t1) then
2977     set @n= @n + 1;
2978   end if;
2979   if (select count(*) from t1) then
2980     set @n= @n + 1;
2981   end if;
2982 end|
2984 set @n = 0|
2985 insert into t1 values ("a", 1)|
2986 call bug3157()|
2987 select @n|
2988 delete from t1|
2989 drop procedure bug3157|
2992 # BUG#5251: mysql changes creation time of a procedure/function when altering
2994 --disable_warnings
2995 drop procedure if exists bug5251|
2996 --enable_warnings
2997 create procedure bug5251()
2998 begin
2999 end|
3001 select created into @c1 from mysql.proc
3002   where db='test' and name='bug5251'|
3003 --sleep 2
3004 alter procedure bug5251 comment 'foobar'|
3005 select count(*) from mysql.proc
3006   where  db='test' and name='bug5251' and created = @c1|
3008 drop procedure bug5251|
3011 # BUG#5279: Stored procedure packets out of order if CHECKSUM TABLE
3013 --disable_warnings
3014 drop procedure if exists bug5251|
3015 --enable_warnings
3016 create procedure bug5251()
3017   checksum table t1|
3019 call bug5251()|
3020 call bug5251()|
3021 drop procedure bug5251|
3024 # BUG#5287: Stored procedure crash if leave outside loop
3026 --disable_warnings
3027 drop procedure if exists bug5287|
3028 --enable_warnings
3029 create procedure bug5287(param1 int)
3030 label1:
3031   begin
3032     declare c cursor for select 5;
3034     loop
3035       if param1 >= 0 then
3036         leave label1;
3037       end if;
3038     end loop;
3039 end|
3040 call bug5287(1)|
3041 drop procedure bug5287|
3045 # BUG#5307: Stored procedure allows statement after BEGIN ... END
3047 --disable_warnings
3048 drop procedure if exists bug5307|
3049 --enable_warnings
3050 create procedure bug5307()
3051 begin
3052 end; set @x = 3|
3054 call bug5307()|
3055 select @x|
3056 drop procedure bug5307|
3059 # BUG#5258: Stored procedure modified date is 0000-00-00
3060 # (This was a design flaw)
3061 --disable_warnings
3062 drop procedure if exists bug5258|
3063 --enable_warnings
3064 create procedure bug5258()
3065 begin
3066 end|
3068 --disable_warnings
3069 drop procedure if exists bug5258_aux|
3070 --enable_warnings
3071 create procedure bug5258_aux()
3072 begin
3073   declare c, m char(19);
3075   select created,modified into c,m from mysql.proc where name = 'bug5258';
3076   if c = m then
3077     select 'Ok';
3078   else
3079     select c, m;
3080   end if;
3081 end|
3083 call bug5258_aux()|
3085 drop procedure bug5258|
3086 drop procedure bug5258_aux|
3089 # BUG#4487: Stored procedure connection aborted if uninitialized char
3091 --disable_warnings
3092 drop function if exists bug4487|
3093 --enable_warnings
3094 create function bug4487() returns char
3095 begin
3096   declare v char;
3097   return v;
3098 end|
3100 select bug4487()|
3101 drop function bug4487|
3105 # BUG#4941: Stored procedure crash fetching null value into variable.
3107 --disable_warnings
3108 drop procedure if exists bug4941|
3109 --enable_warnings
3110 --disable_warnings
3111 drop procedure if exists bug4941|
3112 --enable_warnings
3113 create procedure bug4941(out x int)
3114 begin
3115   declare c cursor for select i from t2 limit 1;
3116   open c;
3117   fetch c into x;
3118   close c;
3119 end|
3121 insert into t2 values (null, null, null)|
3122 set @x = 42|
3123 call bug4941(@x)|
3124 select @x|
3125 delete from t1|
3126 drop procedure bug4941|
3129 # BUG#4905: Stored procedure doesn't clear for "Rows affected"
3131 --disable_warnings
3132 drop procedure if exists bug4905|
3133 --enable_warnings
3135 create table t3 (s1 int,primary key (s1))|
3137 --disable_warnings
3138 drop procedure if exists bug4905|
3139 --enable_warnings
3140 create procedure bug4905()
3141 begin
3142   declare v int;
3143   declare continue handler for sqlstate '23000' set v = 5;
3145   insert into t3 values (1);
3146 end|
3148 call bug4905()|
3149 select row_count()|
3150 call bug4905()|
3151 select row_count()|
3152 call bug4905()|
3153 select row_count()|
3154 select * from t3|
3156 drop procedure bug4905|
3157 drop table t3|
3160 # BUG#6022: Stored procedure shutdown problem with self-calling function.
3163 --disable_parsing # until we implement support for recursive stored functions.
3164 --disable_warnings
3165 drop function if exists bug6022|
3166 --enable_warnings
3168 --disable_warnings
3169 drop function if exists bug6022|
3170 --enable_warnings
3171 create function bug6022(x int) returns int
3172 begin
3173   if x < 0 then
3174     return 0;
3175   else
3176     return bug6022(x-1);
3177   end if;
3178 end|
3180 select bug6022(5)|
3181 drop function bug6022|
3182 --enable_parsing
3185 # BUG#6029: Stored procedure specific handlers should have priority
3187 --disable_warnings
3188 drop procedure if exists bug6029|
3189 --enable_warnings
3191 --disable_warnings
3192 drop procedure if exists bug6029|
3193 --enable_warnings
3194 create procedure bug6029()
3195 begin
3196   declare exit handler for 1136  select '1136';
3197   declare exit handler for sqlstate '23000'  select 'sqlstate 23000';
3198   declare continue handler for sqlexception  select 'sqlexception';
3200   insert into t3 values (1);
3201   insert into t3 values (1,2);
3202 end|
3204 create table t3 (s1 int, primary key (s1))|
3205 insert into t3 values (1)|
3206 call bug6029()|
3207 delete from t3|
3208 call bug6029()|
3210 drop procedure bug6029|
3211 drop table t3|
3214 # BUG#8540: Local variable overrides an alias
3216 --disable_warnings
3217 drop procedure if exists bug8540|
3218 --enable_warnings
3220 create procedure bug8540()
3221 begin
3222   declare x int default 1;
3223   select x as y, x+0 as z;
3224 end|
3226 call bug8540()|
3227 drop procedure bug8540|
3230 # BUG#6642: Stored procedure crash if expression with set function
3232 create table t3 (s1 int)|
3234 --disable_warnings
3235 drop procedure if exists bug6642|
3236 --enable_warnings
3238 create procedure bug6642()
3239   select abs(count(s1)) from t3|
3241 call bug6642()|
3242 call bug6642()|
3243 drop procedure bug6642|
3246 # BUG#7013: Stored procedure crash if group by ... with rollup
3248 insert into t3 values (0),(1)|
3249 --disable_warnings
3250 drop procedure if exists bug7013|
3251 --enable_warnings
3252 create procedure bug7013()
3253   select s1,count(s1) from t3 group by s1 with rollup|
3254 call bug7013()|
3255 call bug7013()|
3256 drop procedure bug7013|
3259 # BUG#7743: 'Lost connection to MySQL server during query' on Stored Procedure
3261 --disable_warnings
3262 drop table if exists t4|
3263 --enable_warnings
3264 create table t4 (
3265   a mediumint(8) unsigned not null auto_increment,
3266   b smallint(5) unsigned not null,
3267   c char(32) not null,
3268   primary key  (a)
3269 ) engine=myisam default charset=latin1|
3270 insert into t4 values (1, 2, 'oneword')|
3271 insert into t4 values (2, 2, 'anotherword')|
3273 --disable_warnings
3274 drop procedure if exists bug7743|
3275 --enable_warnings
3276 create procedure bug7743 ( searchstring char(28) )
3277 begin
3278   declare var mediumint(8) unsigned;
3279   select a into var from t4 where b = 2 and c = binary searchstring limit 1;
3280   select var;
3281 end|
3283 call bug7743("oneword")|
3284 call bug7743("OneWord")|
3285 call bug7743("anotherword")|
3286 call bug7743("AnotherWord")|
3287 drop procedure bug7743|
3288 drop table t4|
3291 # BUG#7992: SELECT .. INTO variable .. within Stored Procedure crashes
3292 #           the server
3294 delete from t3|
3295 insert into t3 values(1)|
3296 drop procedure if exists bug7992_1|
3297 drop procedure if exists bug7992_2|
3298 create procedure bug7992_1()
3299 begin
3300   declare i int;
3301   select max(s1)+1 into i from t3;
3302 end|
3303 create procedure bug7992_2()
3304   insert into t3 (s1) select max(t4.s1)+1 from t3 as t4|
3306 call bug7992_1()|
3307 call bug7992_1()|
3308 call bug7992_2()|
3309 call bug7992_2()|
3311 drop procedure bug7992_1|
3312 drop procedure bug7992_2|
3313 drop table t3|
3316 # BUG#8116: calling simple stored procedure twice in a row results
3317 #           in server crash
3319 create table t3 (  userid bigint(20) not null default 0 )|
3321 --disable_warnings
3322 drop procedure if exists bug8116|
3323 --enable_warnings
3324 create procedure bug8116(in _userid int)
3325    select * from t3 where userid = _userid|
3327 call bug8116(42)|
3328 call bug8116(42)|
3329 drop procedure bug8116|
3330 drop table t3|
3333 # BUG#6857: current_time() in STORED PROCEDURES
3335 --disable_warnings
3336 drop procedure if exists bug6857|
3337 --enable_warnings
3338 create procedure bug6857(counter int)
3339 begin
3340   declare t0, t1 int;
3341   declare plus bool default 0;
3343   set t0 = current_time();
3344   while counter > 0 do
3345     set counter = counter - 1;
3346   end while;
3347   set t1 = current_time();
3348   if t1 > t0 then
3349     set plus = 1;
3350   end if;
3351   select plus;
3352 end|
3354 # QQ: This is currently disabled. Not only does it slow down a normal test
3355 #     run, it makes running with valgrind (or similar tools) extremely
3356 #     painful.
3357 # Make sure this takes at least one second on all machines in all builds.
3358 # 30000 makes it about 3 seconds on an old 1.1GHz linux.
3359 #call bug6857(300000)|
3361 drop procedure bug6857|
3364 # BUG#8757: Stored Procedures: Scope of Begin and End Statements do not
3365 #           work properly.
3366 --disable_warnings
3367 drop procedure if exists bug8757|
3368 --enable_warnings
3369 create procedure bug8757()
3370 begin
3371   declare x int;
3372   declare c1 cursor for select data from t1 limit 1;
3374   begin
3375     declare y int;
3376     declare c2 cursor for select i from t2 limit 1;
3378     open c2;
3379     fetch c2 into y;
3380     close c2;
3381     select 2,y;
3382   end;
3383   open c1;
3384   fetch c1 into x;
3385   close c1;
3386   select 1,x;
3387 end|
3389 delete from t1|
3390 delete from t2|
3391 insert into t1 values ("x", 1)|
3392 insert into t2 values ("y", 2, 0.0)|
3394 call bug8757()|
3396 delete from t1|
3397 delete from t2|
3398 drop procedure bug8757|
3402 # BUG#8762: Stored Procedures: Inconsistent behavior
3403 #           of DROP PROCEDURE IF EXISTS statement.
3404 --disable_warnings
3405 drop procedure if exists bug8762|
3406 --enable_warnings
3407 # Doesn't exist
3408 drop procedure if exists bug8762; create procedure bug8762() begin end|
3409 # Does exist
3410 drop procedure if exists bug8762; create procedure bug8762() begin end|
3411 drop procedure bug8762|
3415 # BUG#5240: Stored procedure crash if function has cursor declaration
3417 --disable_warnings
3418 drop function if exists bug5240|
3419 --enable_warnings
3420 create function bug5240 () returns int
3421 begin
3422   declare x int;
3423   declare c cursor for select data from t1 limit 1;
3425   open c;
3426   fetch c into x;
3427   close c;
3428   return x;
3429 end|
3431 delete from t1|
3432 insert into t1 values ("answer", 42)|
3433 select id, bug5240() from t1|
3434 drop function bug5240|
3437 # BUG#7992: rolling back temporary Item tree changes in SP
3439 --disable_warnings
3440 drop procedure if exists p1|
3441 --enable_warnings
3442 create table t3(id int)|
3443 insert into t3 values(1)|
3444 create procedure bug7992()
3445 begin
3446   declare i int;
3447   select max(id)+1 into i from t3;
3448 end|
3450 call bug7992()|
3451 call bug7992()|
3452 drop procedure bug7992|
3453 drop table t3|
3454 delimiter ;|
3457 # BUG#8849: problem with insert statement with table alias's
3459 # Rolling back changes to AND/OR structure of ON and WHERE clauses  in SP
3462 delimiter |;
3463 create table t3 (
3464   lpitnumber int(11) default null,
3465   lrecordtype int(11) default null
3468 create table t4 (
3469   lbsiid int(11) not null default '0',
3470   ltradingmodeid int(11) not null default '0',
3471   ltradingareaid int(11) not null default '0',
3472   csellingprice decimal(19,4) default null,
3473   primary key  (lbsiid,ltradingmodeid,ltradingareaid)
3476 create table t5 (
3477   lbsiid int(11) not null default '0',
3478   ltradingareaid int(11) not null default '0',
3479   primary key  (lbsiid,ltradingareaid)
3482 --disable_warnings
3483 drop procedure if exists bug8849|
3484 --enable_warnings
3485 create procedure bug8849()
3486 begin
3487   insert into t5
3488   (
3489    t5.lbsiid,
3490    t5.ltradingareaid
3491   )
3492   select distinct t3.lpitnumber, t4.ltradingareaid
3493   from
3494     t4 join t3 on
3495       t3.lpitnumber = t4.lbsiid
3496       and t3.lrecordtype = 1
3497     left join t4 as price01 on
3498       price01.lbsiid = t4.lbsiid and
3499       price01.ltradingmodeid = 1 and
3500       t4.ltradingareaid = price01.ltradingareaid;
3501 end|
3503 call bug8849()|
3504 call bug8849()|
3505 call bug8849()|
3506 drop procedure bug8849|
3507 drop tables t3,t4,t5|
3510 # BUG#8937: Stored Procedure: AVG() works as SUM() in SELECT ... INTO statement
3512 --disable_warnings
3513 drop procedure if exists bug8937|
3514 --enable_warnings
3515 create procedure bug8937()
3516 begin
3517   declare s,x,y,z int;
3518   declare a float;
3520   select sum(data),avg(data),min(data),max(data) into s,x,y,z from t1;
3521   select s,x,y,z;
3522   select avg(data) into a from t1;
3523   select a;
3524 end|
3526 delete from t1|
3527 insert into t1 (data) values (1), (2), (3), (4), (6)|
3528 call bug8937()|
3530 drop procedure bug8937|
3531 delete from t1|
3535 # BUG#6900: Stored procedure inner handler ignored
3536 # BUG#9074: STORED PROC: The scope of every handler declared is not
3537 #                        properly applied
3539 --disable_warnings
3540 drop procedure if exists bug6900|
3541 drop procedure if exists bug9074|
3542 drop procedure if exists bug6900_9074|
3543 --enable_warnings
3545 create table t3 (w char unique, x char)|
3546 insert into t3 values ('a', 'b')|
3548 create procedure bug6900()
3549 begin
3550   declare exit handler for sqlexception select '1';
3552   begin
3553     declare exit handler for sqlexception select '2';
3555     insert into t3 values ('x', 'y', 'z');
3556   end;
3557 end|
3559 create procedure bug9074()
3560 begin
3561   declare x1, x2, x3, x4, x5, x6 int default 0;
3563   begin    
3564     declare continue handler for sqlstate '23000' set x5 = 1;      
3566     insert into t3 values ('a', 'b');      
3567     set x6 = 1;      
3568   end;
3570  begin1_label:
3571   begin
3572     declare continue handler for sqlstate '23000' set x1 = 1;      
3574     insert into t3 values ('a', 'b');      
3575     set x2 = 1;      
3576                                 
3577    begin2_label:
3578     begin  
3579       declare exit handler for sqlstate '23000' set x3 = 1;         
3581       set x4= 1;         
3582       insert into t3 values ('a','b');
3583       set x4= 0;
3584     end begin2_label;
3585   end begin1_label;
3587   select x1, x2, x3, x4, x5, x6;
3588 end|
3590 create procedure bug6900_9074(z int)
3591 begin
3592   declare exit handler for sqlstate '23000' select '23000';
3594   begin
3595     declare exit handler for sqlexception select 'sqlexception';
3597     if z = 1 then
3598       insert into t3 values ('a', 'b');
3599     else
3600       insert into t3 values ('x', 'y', 'z');
3601     end if;
3602   end;
3603 end|
3605 call bug6900()|
3606 call bug9074()|
3607 call bug6900_9074(0)|
3608 call bug6900_9074(1)|
3610 drop procedure bug6900|
3611 drop procedure bug9074|
3612 drop procedure bug6900_9074|
3613 drop table t3|
3617 # BUG#7185: Stored procedure crash if identifier is AVG
3619 --disable_warnings
3620 drop procedure if exists avg|
3621 --enable_warnings
3622 create procedure avg ()
3623 begin
3624 end|
3626 call avg ()|
3627 drop procedure avg|
3631 # BUG#6129: Stored procedure won't display @@sql_mode value
3633 --disable_warnings
3634 drop procedure if exists bug6129|
3635 --enable_warnings
3636 set @old_mode= @@sql_mode;
3637 set @@sql_mode= "ERROR_FOR_DIVISION_BY_ZERO";
3638 create procedure bug6129()
3639   select @@sql_mode|
3640 call bug6129()|
3641 set @@sql_mode= "NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO"|
3642 call bug6129()|
3643 set @@sql_mode= "NO_ZERO_IN_DATE"|
3644 call bug6129()|
3645 set @@sql_mode=@old_mode;
3647 drop procedure bug6129|
3651 # BUG#9856: Stored procedures: crash if handler for sqlexception, not found
3653 --disable_warnings
3654 drop procedure if exists bug9856|
3655 --enable_warnings
3656 create procedure bug9856()
3657 begin
3658   declare v int;
3659   declare c cursor for select data from t1;
3660   declare exit handler for sqlexception, not found select '16';
3662   open c;
3663   fetch c into v;
3664   select v;
3665 end|
3667 delete from t1|
3668 call bug9856()|
3669 call bug9856()|
3670 drop procedure bug9856|
3674 # BUG##9674: Stored Procs: Using declared vars in algebric operation causes
3675 #            system crash.
3677 --disable_warnings
3678 drop procedure if exists bug9674_1|
3679 drop procedure if exists bug9674_2|
3680 --enable_warnings
3681 create procedure bug9674_1(out arg int)
3682 begin
3683   declare temp_in1 int default 0;
3684   declare temp_fl1 int default 0;
3686   set temp_in1 = 100;
3687   set temp_fl1 = temp_in1/10;
3688   set arg = temp_fl1;
3689 end|
3691 create procedure bug9674_2()
3692 begin
3693   declare v int default 100;
3695   select v/10;
3696 end|
3698 call bug9674_1(@sptmp)|
3699 call bug9674_1(@sptmp)|
3700 select @sptmp|
3701 call bug9674_2()|
3702 call bug9674_2()|
3703 drop procedure bug9674_1|
3704 drop procedure bug9674_2|
3708 # BUG#9598: stored procedure call within stored procedure overwrites IN variable
3710 --disable_warnings
3711 drop procedure if exists bug9598_1|
3712 drop procedure if exists bug9598_2|
3713 --enable_warnings
3714 create procedure bug9598_1(in var_1 char(16),
3715                            out var_2 integer, out var_3 integer)
3716 begin
3717   set var_2 = 50;
3718   set var_3 = 60;
3719 end|
3721 create procedure bug9598_2(in v1 char(16),
3722                            in v2 integer,
3723                            in v3 integer,
3724                            in v4 integer,
3725                            in v5 integer)
3726 begin
3727   select v1,v2,v3,v4,v5;
3728   call bug9598_1(v1,@tmp1,@tmp2);
3729   select v1,v2,v3,v4,v5;
3730 end|
3732 call bug9598_2('Test',2,3,4,5)|
3733 select @tmp1, @tmp2|
3735 drop procedure bug9598_1|
3736 drop procedure bug9598_2|
3740 # BUG#9902: Crash with simple stored function using user defined variables
3742 --disable_warnings
3743 drop procedure if exists bug9902|
3744 --enable_warnings
3745 create function bug9902() returns int(11)
3746 begin
3747   set @x = @x + 1;
3748   return @x;
3749 end|
3751 set @qcs1 = @@query_cache_size|
3752 set global query_cache_size = 100000|
3753 set @x = 1|
3754 insert into t1 values ("qc", 42)|
3755 select bug9902() from t1|
3756 select bug9902() from t1|
3757 select @x|
3759 set global query_cache_size = @qcs1|
3760 delete from t1|
3761 drop function bug9902|
3765 # BUG#9102: Stored proccedures: function which returns blob causes crash
3767 --disable_warnings
3768 drop function if exists bug9102|
3769 --enable_warnings
3770 create function bug9102() returns blob return 'a'|
3771 select bug9102()|
3772 drop function bug9102|
3776 # BUG#7648: Stored procedure crash when invoking a function that returns a bit
3778 --disable_warnings
3779 drop function if exists bug7648|
3780 --enable_warnings
3781 create function bug7648() returns bit(8) return 'a'|
3782 select bug7648()|
3783 drop function bug7648|
3787 # BUG#9775: crash if create function that returns enum or set
3789 --disable_warnings
3790 drop function if exists bug9775|
3791 --enable_warnings
3792 create function bug9775(v1 char(1)) returns enum('a','b') return v1|
3793 select bug9775('a'),bug9775('b'),bug9775('c')|
3794 drop function bug9775|
3795 create function bug9775(v1 int) returns enum('a','b') return v1|
3796 select bug9775(1),bug9775(2),bug9775(3)|
3797 drop function bug9775|
3799 create function bug9775(v1 char(1)) returns set('a','b') return v1|
3800 select bug9775('a'),bug9775('b'),bug9775('a,b'),bug9775('c')|
3801 drop function bug9775|
3802 create function bug9775(v1 int) returns set('a','b') return v1|
3803 select bug9775(1),bug9775(2),bug9775(3),bug9775(4)|
3804 drop function bug9775|
3808 # BUG#8861: If Return is a YEAR data type, value is not shown in year format
3810 --disable_warnings
3811 drop function if exists bug8861|
3812 --enable_warnings
3813 create function bug8861(v1 int) returns year return v1|
3814 select bug8861(05)|
3815 set @x = bug8861(05)|
3816 select @x|
3817 drop function bug8861|
3821 # BUG#9004: Inconsistent behaviour of SP re. warnings
3823 --disable_warnings
3824 drop procedure if exists bug9004_1|
3825 drop procedure if exists bug9004_2|
3826 --enable_warnings
3827 create procedure bug9004_1(x char(16))
3828 begin
3829   insert into t1 values (x, 42);
3830   insert into t1 values (x, 17);
3831 end|
3832 create procedure bug9004_2(x char(16))
3833   call bug9004_1(x)|
3835 # Truncation warnings expected...
3836 call bug9004_1('12345678901234567')|
3837 call bug9004_2('12345678901234567890')|
3839 delete from t1|
3840 drop procedure bug9004_1|
3841 drop procedure bug9004_2|
3844 # BUG#7293: Stored procedure crash with soundex
3846 --disable_warnings
3847 drop procedure if exists bug7293|
3848 --enable_warnings
3849 insert into t1 values ('secret', 0)| 
3850 create procedure bug7293(p1 varchar(100))
3851 begin
3852   if exists (select id from t1 where soundex(p1)=soundex(id)) then
3853     select 'yes';
3854   end if;
3855 end;| 
3856 call bug7293('secret')| 
3857 call bug7293 ('secrete')| 
3858 drop procedure bug7293|
3859 delete from t1|
3863 # BUG#9841: Unexpected read lock when trying to update a view in a
3864 #           stored procedure
3866 --disable_warnings
3867 drop procedure if exists bug9841|
3868 drop view if exists v1|
3869 --enable_warnings
3871 create view v1 as select * from t1, t2 where id = s|
3872 create procedure bug9841 ()
3873   update v1 set data = 10| 
3874 call bug9841()|
3876 drop view v1|
3877 drop procedure bug9841|
3881 # BUG#5963 subqueries in SET/IF
3883 --disable_warnings
3884 drop procedure if exists bug5963|
3885 --enable_warnings
3887 create procedure bug5963_1 () begin declare v int; set v = (select s1 from t3); select v; end;|
3888 create table t3 (s1 int)|
3889 insert into t3 values (5)|
3890 call bug5963_1()|
3891 call bug5963_1()|
3892 drop procedure bug5963_1|
3893 drop table t3|
3895 create procedure bug5963_2 (cfk_value int) 
3896 begin 
3897   if cfk_value in (select cpk from t3) then 
3898     set @x = 5; 
3899   end if; 
3900   end; 
3902 create table t3 (cpk int)| 
3903 insert into t3 values (1)| 
3904 call bug5963_2(1)|
3905 call bug5963_2(1)|
3906 drop procedure bug5963_2|
3907 drop table t3|
3911 # BUG#9559: Functions: Numeric Operations using -ve value gives incorrect
3912 #           results.
3914 --disable_warnings
3915 drop function if exists bug9559|
3916 --enable_warnings
3917 create function bug9559()
3918   returns int
3919 begin
3920   set @y = -6/2;
3921   return @y;
3922 end|
3924 select bug9559()|
3926 drop function bug9559|
3930 # BUG#10961: Stored procedures: crash if select * from dual
3932 --disable_warnings
3933 drop procedure if exists bug10961|
3934 --enable_warnings
3935 # "select * from dual" results in an error, so the cursor will not open
3936 create procedure bug10961()
3937 begin
3938   declare v char;
3939   declare x int;
3940   declare c cursor for select * from dual;
3941   declare continue handler for sqlexception select x;
3943   set x = 1;
3944   open c;
3945   set x = 2;
3946   fetch c into v;
3947   set x = 3;
3948   close c;
3949 end|
3951 call bug10961()|
3952 call bug10961()|
3954 drop procedure bug10961|
3957 # BUG #6866: Second call of a stored procedure using a view with on expressions
3960 --disable_warnings
3961 DROP PROCEDURE IF EXISTS bug6866|
3962 --enable_warnings
3964 DROP VIEW IF EXISTS tv|
3965 DROP TABLE IF EXISTS tt1,tt2,tt3|
3967 CREATE TABLE tt1 (a1 int, a2 int, a3 int, data varchar(10))|
3968 CREATE TABLE tt2 (a2 int, data2 varchar(10))|
3969 CREATE TABLE tt3 (a3 int, data3 varchar(10))|
3971 INSERT INTO tt1 VALUES (1, 1, 4, 'xx')|
3973 INSERT INTO tt2 VALUES (1, 'a')|
3974 INSERT INTO tt2 VALUES (2, 'b')|
3975 INSERT INTO tt2 VALUES (3, 'c')|
3977 INSERT INTO tt3 VALUES (4, 'd')|
3978 INSERT INTO tt3 VALUES (5, 'e')|
3979 INSERT INTO tt3 VALUES (6, 'f')|
3981 CREATE VIEW tv AS
3982 SELECT tt1.*, tt2.data2, tt3.data3
3983   FROM tt1 INNER JOIN tt2 ON tt1.a2 = tt2.a2
3984          LEFT JOIN tt3 ON tt1.a3 = tt3.a3
3985     ORDER BY tt1.a1, tt2.a2, tt3.a3|
3987 CREATE PROCEDURE bug6866 (_a1 int)
3988 BEGIN
3989 SELECT * FROM tv WHERE a1 = _a1;
3990 END|
3992 CALL bug6866(1)|
3993 CALL bug6866(1)|
3994 CALL bug6866(1)|
3996 DROP PROCEDURE bug6866;
3998 DROP VIEW tv|
3999 DROP TABLE tt1, tt2, tt3|
4002 # BUG#10136: items cleunup
4004 --disable_warnings
4005 DROP PROCEDURE IF EXISTS bug10136|
4006 --enable_warnings
4007 create table t3 ( name char(5) not null primary key, val float not null)|
4008 insert into t3 values ('aaaaa', 1), ('bbbbb', 2), ('ccccc', 3)|
4009 create procedure bug10136()
4010 begin
4011   declare done int default 3;
4013   repeat
4014     select * from t3;
4015     set done = done - 1;
4016   until done <= 0 end repeat;
4018 end|
4019 call bug10136()|
4020 call bug10136()|
4021 call bug10136()|
4022 drop procedure bug10136|
4023 drop table t3|
4026 # BUG#11529: crash server after use stored procedure
4028 --disable_warnings
4029 drop procedure if exists bug11529|
4030 --enable_warnings
4031 create procedure bug11529()
4032 begin
4033   declare c cursor for select id, data from t1 where data in (10,13);
4035   open c;
4036   begin
4037     declare vid char(16);
4038     declare vdata int;
4039     declare exit handler for not found begin end;
4041     while true do
4042       fetch c into vid, vdata;
4043     end while;
4044   end;
4045   close c;
4046 end|
4048 insert into t1 values
4049   ('Name1', 10),
4050   ('Name2', 11),
4051   ('Name3', 12),
4052   ('Name4', 13),
4053   ('Name5', 14)|
4055 call bug11529()|
4056 call bug11529()|
4057 delete from t1|
4058 drop procedure bug11529|
4062 # BUG#6063: Stored procedure labels are subject to restrictions (partial)
4063 # BUG#7088: Stored procedures: labels won't work if character set is utf8
4065 --disable_warnings
4066 drop procedure if exists bug6063|
4067 drop procedure if exists bug7088_1|
4068 drop procedure if exists bug7088_2|
4069 --enable_warnings
4071 --disable_parsing # temporarily disabled until Bar fixes BUG#11986
4072 create procedure bug6063()
4073   lâbel: begin end|
4074 call bug6063()|
4075 # QQ Known bug: this will not show the label correctly.
4076 show create procedure bug6063|
4078 set character set utf8|
4079 create procedure bug7088_1()
4080   label1: begin end label1|
4081 create procedure bug7088_2()
4082   läbel1: begin end|
4083 call bug7088_1()|
4084 call bug7088_2()|
4085 set character set default|
4086 show create procedure bug7088_1|
4087 show create procedure bug7088_2|
4089 drop procedure bug6063|
4090 drop procedure bug7088_1|
4091 drop procedure bug7088_2|
4092 --enable_parsing
4095 # BUG#9565: "Wrong locking in stored procedure if a sub-sequent procedure
4096 #           is called".
4098 --disable_warnings
4099 drop procedure if exists bug9565_sub|
4100 drop procedure if exists bug9565|
4101 --enable_warnings
4102 create procedure bug9565_sub()
4103 begin
4104   select * from t1;
4105 end|
4106 create procedure bug9565()
4107 begin
4108   insert into t1 values ("one", 1);
4109   call bug9565_sub();
4110 end|
4111 call bug9565()|
4112 delete from t1|
4113 drop procedure bug9565_sub|
4114 drop procedure bug9565|
4118 # BUG#9538: SProc: Creation fails if we try to SET system variable
4119 #           using @@var_name in proc
4121 --disable_warnings
4122 drop procedure if exists bug9538|
4123 --enable_warnings
4124 create procedure bug9538()
4125   set @@sort_buffer_size = 1000000|
4127 set @x = @@sort_buffer_size|
4128 set @@sort_buffer_size = 2000000|
4129 select @@sort_buffer_size|
4130 call bug9538()|
4131 select @@sort_buffer_size|
4132 set @@sort_buffer_size = @x|
4134 drop procedure bug9538|
4138 # BUG#8692: Cursor fetch of empty string
4140 --disable_warnings
4141 drop procedure if exists bug8692|
4142 --enable_warnings
4143 create table t3 (c1 varchar(5), c2 char(5), c3 enum('one','two'), c4 text, c5 blob, c6 char(5), c7 varchar(5))|
4144 insert into t3 values ('', '', '', '', '', '', NULL)|
4146 create procedure bug8692()
4147 begin 
4148     declare v1 VARCHAR(10); 
4149     declare v2 VARCHAR(10); 
4150     declare v3 VARCHAR(10); 
4151     declare v4 VARCHAR(10); 
4152     declare v5 VARCHAR(10); 
4153     declare v6 VARCHAR(10); 
4154     declare v7 VARCHAR(10); 
4155     declare c8692 cursor for select c1,c2,c3,c4,c5,c6,c7 from t3; 
4156     open c8692; 
4157     fetch c8692 into v1,v2,v3,v4,v5,v6,v7;
4158     select v1, v2, v3, v4, v5, v6, v7;
4159 end|
4161 call bug8692()|
4162 drop procedure bug8692|
4163 drop table t3|
4166 # Bug#10055 "Using stored function with information_schema causes empty
4167 #            result set"
4169 --disable_warnings
4170 drop function if exists bug10055|
4171 --enable_warnings
4172 create function bug10055(v char(255)) returns char(255) return lower(v)|
4173 # This select should not crash server and should return all fields in t1
4174 select t.column_name, bug10055(t.column_name)
4175 from information_schema.columns as t
4176 where t.table_schema = 'test' and t.table_name = 't1'|
4177 drop function bug10055|
4180 # Bug #12297 "SP crashes the server if data inserted inside a lon loop"
4181 # The test for memleak bug, so actually there is no way to test it
4182 # from the suite. The test below could be used to check SP memory
4183 # consumption by passing large input parameter.
4186 --disable_warnings
4187 drop procedure if exists bug12297|
4188 --enable_warnings
4190 create procedure bug12297(lim int)
4191 begin
4192   set @x = 0;
4193   repeat
4194     insert into t1(id,data)
4195     values('aa', @x);
4196     set @x = @x + 1;
4197   until @x >= lim
4198   end repeat;
4199 end|
4201 call bug12297(10)|
4202 drop procedure bug12297|
4205 # Bug #11247 "Stored procedures: Function calls in long loops leak memory"
4206 # One more memleak bug test. One could use this test to check that the memory
4207 # isn't leaking by increasing the input value for p_bug11247.
4210 --disable_warnings
4211 drop function if exists f_bug11247|
4212 drop procedure if exists p_bug11247|
4213 --enable_warnings
4215 create function f_bug11247(param int)
4216   returns int
4217 return param + 1|
4219 create procedure p_bug11247(lim int)
4220 begin
4221   declare v int default 0;
4223   while v < lim do
4224     set v= f_bug11247(v);
4225   end while;
4226 end|
4228 call p_bug11247(10)|
4229 drop function f_bug11247|
4230 drop procedure p_bug11247|
4232 # BUG#12168: "'DECLARE CONTINUE HANDLER FOR NOT FOUND ...' in conditional
4233 # handled incorrectly"
4235 --disable_warnings
4236 drop procedure if exists bug12168|
4237 drop table if exists t3, t4|
4238 --enable_warnings
4240 create table t3 (a int)|
4241 insert into t3 values (1),(2),(3),(4)|
4243 create table t4 (a int)|
4245 create procedure bug12168(arg1 char(1))
4246 begin
4247   declare b, c integer;
4248   if arg1 = 'a' then
4249     begin
4250       declare c1 cursor for select a from t3 where a % 2;
4251       declare continue handler for not found set b = 1;
4252       set b = 0;
4253       open c1;
4254       c1_repeat: repeat
4255         fetch c1 into c;
4256         if (b = 1) then
4257           leave c1_repeat;
4258         end if;
4260         insert into t4 values (c);
4261         until b = 1
4262       end repeat;
4263     end;
4264   end if;
4265   if arg1 = 'b' then
4266     begin
4267       declare c2 cursor for select a from t3 where not a % 2;
4268       declare continue handler for not found set b = 1;
4269       set b = 0;
4270       open c2;
4271       c2_repeat: repeat
4272         fetch c2 into c;
4273         if (b = 1) then
4274           leave c2_repeat;
4275         end if;
4277         insert into t4 values (c);
4278         until b = 1
4279       end repeat;
4280     end;
4281   end if;
4282 end|
4284 call bug12168('a')|
4285 select * from t4|
4286 truncate t4|
4287 call bug12168('b')|
4288 select * from t4|
4289 truncate t4|
4290 call bug12168('a')|
4291 select * from t4|
4292 truncate t4|
4293 call bug12168('b')|
4294 select * from t4|
4295 truncate t4|
4296 drop table t3, t4|
4297 drop procedure if exists bug12168|
4300 # Bug #11333 "Stored Procedure: Memory blow up on repeated SELECT ... INTO
4301 # query"
4302 # One more memleak bug. Use the test to check memory consumption.
4305 --disable_warnings
4306 drop table if exists t3|
4307 drop procedure if exists bug11333|
4308 --enable_warnings
4310 create table t3 (c1 char(128))|
4312 insert into t3 values 
4313   ('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')|
4316 create procedure bug11333(i int)
4317 begin
4318     declare tmp varchar(128);
4319     set @x = 0;
4320     repeat
4321         select c1 into tmp from t3
4322           where c1 = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
4323         set @x = @x + 1;
4324         until @x >= i
4325     end repeat;
4326 end|
4328 call bug11333(10)|
4330 drop procedure bug11333|
4331 drop table t3|
4334 # BUG#9048: Creating a function with char binary IN parameter fails
4336 --disable_warnings
4337 drop function if exists bug9048|
4338 --enable_warnings
4339 create function bug9048(f1 char binary) returns char
4340 begin
4341   set f1= concat( 'hello', f1 );
4342   return f1;
4343 end|
4344 drop function bug9048|
4346 # This was disabled in 5.1.12. See bug #20701
4347 # When collation support in SP is implemented, then this test should
4348 # be removed.
4350 --error ER_NOT_SUPPORTED_YET
4351 create function bug9048(f1 char binary) returns char binary
4352 begin
4353   set f1= concat( 'hello', f1 );
4354   return f1;
4355 end|
4357 # Bug #12849 Stored Procedure: Crash on procedure call with CHAR type
4358 # 'INOUT' parameter
4361 --disable_warnings
4362 drop procedure if exists bug12849_1|
4363 --enable_warnings
4364 create procedure bug12849_1(inout x char) select x into x|
4365 set @var='a'|
4366 call bug12849_1(@var)|
4367 select @var|
4368 drop procedure bug12849_1|
4370 --disable_warnings
4371 drop procedure if exists bug12849_2|
4372 --enable_warnings
4373 create procedure bug12849_2(inout foo varchar(15))
4374 begin
4375 select concat(foo, foo) INTO foo;
4376 end|
4377 set @var='abcd'|
4378 call bug12849_2(@var)|
4379 select @var|
4380 drop procedure bug12849_2|
4383 # BUG#13133: Local variables in stored procedures are not initialized correctly.
4385 --disable_warnings
4386 drop procedure if exists bug131333|
4387 drop function if exists bug131333|
4388 --enable_warnings
4389 create procedure bug131333()
4390 begin
4391   begin
4392     declare a int;
4394     select a;
4395     set a = 1;
4396     select a;
4397   end;
4398   begin
4399     declare b int;
4401     select b;
4402   end;
4403 end|
4405 create function bug131333()
4406   returns int
4407 begin
4408   begin
4409     declare a int;
4411     set a = 1;
4412   end;
4413   begin
4414     declare b int;
4416     return b;
4417   end;
4418 end|
4420 call bug131333()|
4421 select bug131333()|
4423 drop procedure bug131333|
4424 drop function bug131333|
4427 # BUG#12379: PROCEDURE with HANDLER calling FUNCTION with error get
4428 #            strange result
4430 --disable_warnings
4431 drop function if exists bug12379|
4432 drop procedure if exists bug12379_1|
4433 drop procedure if exists bug12379_2|
4434 drop procedure if exists bug12379_3|
4435 drop table if exists t3|
4436 --enable_warnings
4438 create table t3 (c1 char(1) primary key not null)|
4440 create function bug12379()
4441   returns integer
4442 begin
4443    insert into t3 values('X');
4444    insert into t3 values('X');
4445    return 0;
4446 end|
4448 create procedure bug12379_1()
4449 begin
4450    declare exit handler for sqlexception select 42;
4452    select bug12379();
4453 END|
4454 create procedure bug12379_2()
4455 begin
4456    declare exit handler for sqlexception begin end;
4458    select bug12379();
4459 end|
4460 create procedure bug12379_3()
4461 begin
4462    select bug12379();
4463 end|
4465 --error ER_DUP_ENTRY
4466 select bug12379()|
4467 select 1|
4468 # statement-based binlogging will show warning which row-based won't;
4469 # so we hide it (this warning is already tested in rpl_stm_sp.test)
4470 --disable_warnings
4471 call bug12379_1()|
4472 select 2|
4473 call bug12379_2()|
4474 --enable_warnings
4475 select 3|
4476 --error ER_DUP_ENTRY
4477 call bug12379_3()|
4478 select 4|
4480 drop function bug12379|
4481 drop procedure bug12379_1|
4482 drop procedure bug12379_2|
4483 drop procedure bug12379_3|
4484 drop table t3|
4487 # Bug #13124    Stored Procedure using SELECT INTO crashes server
4490 --disable_warnings
4491 drop procedure if exists bug13124|
4492 --enable_warnings
4493 create procedure bug13124()
4494 begin
4495   declare y integer;
4496   set @x=y;
4497 end|
4498 call bug13124()|
4499 drop procedure  bug13124|
4502 # Bug #12979  Stored procedures: crash if inout decimal parameter
4505 # check NULL inout parameters processing
4507 --disable_warnings
4508 drop procedure if exists bug12979_1|
4509 --enable_warnings
4510 create procedure bug12979_1(inout d decimal(5)) set d = d / 2|
4511 set @bug12979_user_var = NULL|
4512 call bug12979_1(@bug12979_user_var)|
4513 drop procedure bug12979_1|
4515 # check NULL local variables processing
4517 --disable_warnings
4518 drop procedure if exists bug12979_2|
4519 --enable_warnings
4520 create procedure bug12979_2()
4521 begin
4522 declare internal_var decimal(5);
4523 set internal_var= internal_var / 2;
4524 select internal_var;
4525 end|
4526 call bug12979_2()|
4527 drop procedure bug12979_2|
4531 # BUG#6127: Stored procedure handlers within handlers don't work
4533 --disable_warnings
4534 drop table if exists t3|
4535 drop procedure if exists bug6127|
4536 --enable_warnings
4537 create table t3 (s1 int unique)|
4539 set @sm=@@sql_mode|
4540 set sql_mode='traditional'|
4542 create procedure bug6127()
4543 begin
4544   declare continue handler for sqlstate '23000'
4545     begin
4546       declare continue handler for sqlstate '22003'
4547         insert into t3 values (0);
4549       insert into t3 values (1000000000000000);
4550     end;
4552   insert into t3 values (1);
4553   insert into t3 values (1);
4554 end|
4556 call bug6127()|
4557 select * from t3|
4558 --error ER_DUP_ENTRY
4559 call bug6127()|
4560 select * from t3|
4561 set sql_mode=@sm|
4562 drop table t3|
4563 drop procedure bug6127|
4567 # BUG#12589: Assert when creating temp. table from decimal stored procedure
4568 #            variable
4570 --disable_warnings
4571 drop procedure if exists bug12589_1|
4572 drop procedure if exists bug12589_2|
4573 drop procedure if exists bug12589_3|
4574 --enable_warnings
4575 create procedure bug12589_1()
4576 begin
4577   declare spv1 decimal(3,3);
4578   set spv1= 123.456;
4580   set spv1 = 'test';
4581   create temporary table tm1 as select spv1;
4582   show create table tm1;
4583   drop temporary table tm1;
4584 end|
4586 create procedure bug12589_2()
4587 begin
4588   declare spv1 decimal(6,3);
4589   set spv1= 123.456;
4591   create temporary table tm1 as select spv1;
4592   show create table tm1;
4593   drop temporary table tm1;
4594 end|
4596 create procedure bug12589_3()
4597 begin
4598   declare spv1 decimal(6,3);
4599   set spv1= -123.456;
4601   create temporary table tm1 as select spv1;
4602   show create table tm1;
4603   drop temporary table tm1;
4604 end|
4606 # Note: The type of the field will match the value, not the declared
4607 #       type of the variable. (This is a type checking issue which
4608 #       might be changed later.)
4610 # Warning expected from "set spv1 = 'test'", the value is set to decimal "0".
4611 call bug12589_1()|
4612 # No warnings here
4613 call bug12589_2()|
4614 call bug12589_3()|
4615 drop procedure bug12589_1|
4616 drop procedure bug12589_2|
4617 drop procedure bug12589_3|
4620 # BUG#7049: Stored procedure CALL errors are ignored
4622 --disable_warnings
4623 drop table if exists t3|
4624 drop procedure if exists bug7049_1|
4625 drop procedure if exists bug7049_2|
4626 drop procedure if exists bug7049_3|
4627 drop procedure if exists bug7049_4|
4628 drop function if exists bug7049_1|
4629 drop function if exists bug7049_2|
4630 --enable_warnings
4632 create table t3 ( x int unique )|
4634 create procedure bug7049_1()
4635 begin
4636   insert into t3 values (42);
4637   insert into t3 values (42);
4638 end|
4640 create procedure bug7049_2()
4641 begin
4642   declare exit handler for sqlexception
4643     select 'Caught it' as 'Result';
4645   call bug7049_1();
4646   select 'Missed it' as 'Result';
4647 end|
4649 create procedure bug7049_3()
4650   call bug7049_1()|
4652 create procedure bug7049_4()
4653 begin
4654   declare exit handler for sqlexception
4655     select 'Caught it' as 'Result';
4657   call bug7049_3();
4658   select 'Missed it' as 'Result';
4659 end|
4661 create function bug7049_1()
4662   returns int
4663 begin
4664   insert into t3 values (42);
4665   insert into t3 values (42);
4666   return 42;
4667 end|
4669 create function bug7049_2()
4670   returns int
4671 begin
4672   declare x int default 0;
4673   declare continue handler for sqlexception
4674     set x = 1;
4676   set x = bug7049_1();
4677   return x;
4678 end|
4680 call bug7049_2()|
4681 select * from t3|
4682 delete from t3|
4683 call bug7049_4()|
4684 select * from t3|
4685 select bug7049_2()|
4687 drop table t3|
4688 drop procedure bug7049_1|
4689 drop procedure bug7049_2|
4690 drop procedure bug7049_3|
4691 drop procedure bug7049_4|
4692 drop function bug7049_1|
4693 drop function bug7049_2|
4697 # BUG#13941: replace() string fuction behaves badly inside stored procedure
4698 # (BUG#13914: IFNULL is returning garbage in stored procedure)
4700 --disable_warnings
4701 drop function if exists bug13941|
4702 drop procedure if exists bug13941|
4703 --enable_warnings
4705 create function bug13941(p_input_str text)
4706   returns text
4707 begin
4708   declare p_output_str text;
4710   set p_output_str = p_input_str;
4712   set p_output_str = replace(p_output_str, 'xyzzy', 'plugh');
4713   set p_output_str = replace(p_output_str, 'test', 'prova');
4714   set p_output_str = replace(p_output_str, 'this', 'questo');
4715   set p_output_str = replace(p_output_str, ' a ', 'una ');
4716   set p_output_str = replace(p_output_str, 'is', '');
4718   return p_output_str;
4719 end|
4721 create procedure bug13941(out sout varchar(128))
4722 begin
4723   set sout = 'Local';
4724   set sout = ifnull(sout, 'DEF');
4725 end|
4727 # Note: The bug showed different behaviour in different types of builds,
4728 #  giving garbage results in some, and seemingly working in others.
4729 #  Running with valgrind (or purify) is the safe way to check that it's
4730 #  really working correctly.
4731 select bug13941('this is a test')|
4732 call bug13941(@a)|
4733 select @a|
4735 drop function bug13941|
4736 drop procedure bug13941|
4740 # BUG#13095: Cannot create VIEWs in prepared statements
4743 delimiter ;|
4745 --disable_warnings
4746 DROP PROCEDURE IF EXISTS bug13095;
4747 DROP TABLE IF EXISTS bug13095_t1;
4748 DROP VIEW IF EXISTS bug13095_v1;
4749 --enable_warnings
4751 delimiter |;
4753 CREATE PROCEDURE bug13095(tbl_name varchar(32))
4754 BEGIN
4755   SET @str =
4756     CONCAT("CREATE TABLE ", tbl_name, "(stuff char(15))");
4757   SELECT @str;
4758   PREPARE stmt FROM @str;
4759   EXECUTE stmt;
4761   SET @str =
4762     CONCAT("INSERT INTO ", tbl_name, " VALUES('row1'),('row2'),('row3')" );
4763   SELECT @str;
4764   PREPARE stmt FROM @str;
4765   EXECUTE stmt;
4767   SET @str =
4768     CONCAT("CREATE VIEW bug13095_v1(c1) AS SELECT stuff FROM ", tbl_name);
4769   SELECT @str;
4770   PREPARE stmt FROM @str;
4771   EXECUTE stmt;
4773   SELECT * FROM bug13095_v1;
4775   SET @str =
4776     "DROP VIEW bug13095_v1";
4777   SELECT @str;
4778   PREPARE stmt FROM @str;
4779   EXECUTE stmt;
4780 END|
4782 delimiter ;|
4784 CALL bug13095('bug13095_t1');
4786 --disable_warnings
4787 DROP PROCEDURE IF EXISTS bug13095;
4788 DROP VIEW IF EXISTS bug13095_v1;
4789 DROP TABLE IF EXISTS bug13095_t1;
4790 --enable_warnings
4792 delimiter |;
4795 # BUG#1473: Dumping of stored functions seems to cause corruption in
4796 #           the function body
4798 --disable_warnings
4799 drop function if exists bug14723|
4800 drop procedure if exists bug14723|
4801 --enable_warnings
4803 delimiter ;;|
4804 /*!50003 create function bug14723()
4805  returns bigint(20)
4806 main_loop: begin
4807   return 42;
4808 end */;;
4809 show create function bug14723;;
4810 select bug14723();;
4812 /*!50003 create procedure bug14723()
4813 main_loop: begin
4814   select 42;
4815 end */;;
4816 show create procedure bug14723;;
4817 call bug14723();;
4819 delimiter |;;
4821 drop function bug14723|
4822 drop procedure bug14723|
4825 # Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
4826 # Check that when fetching from a cursor, COUNT(*) works properly.
4828 create procedure bug14845()
4829 begin
4830   declare a char(255);
4831   declare done int default 0;
4832   declare c cursor for select count(*) from t1 where 1 = 0;
4833   declare continue handler for sqlstate '02000' set done = 1;
4834   open c;
4835   repeat
4836     fetch c into a;
4837     if not done then
4838       select a;
4839     end if;
4840   until done end repeat;
4841   close c;
4842 end|
4843 call bug14845()|
4844 drop procedure bug14845|
4847 # BUG#13549 "Server crash with nested stored procedures".
4848 # Server should not crash when during execution of stored procedure
4849 # we have to parse trigger/function definition and this new trigger/
4850 # function has more local variables declared than invoking stored
4851 # procedure and last of these variables is used in argument of NOT
4852 # operator.
4854 --disable_warnings
4855 drop procedure if exists bug13549_1|
4856 drop procedure if exists bug13549_2|
4857 --enable_warnings
4858 CREATE PROCEDURE `bug13549_2`()
4859 begin
4860   call bug13549_1();
4861 end|
4862 CREATE PROCEDURE `bug13549_1`()
4863 begin
4864   declare done int default 0;
4865   set done= not done;
4866 end|
4867 CALL bug13549_2()|
4868 drop procedure bug13549_2|
4869 drop procedure bug13549_1|
4872 # BUG#10100: function (and stored procedure?) recursivity problem
4874 --disable_warnings
4875 drop function if exists bug10100f|
4876 drop procedure if exists bug10100p|
4877 drop procedure if exists bug10100t|
4878 drop procedure if exists bug10100pt|
4879 drop procedure if exists bug10100pv|
4880 drop procedure if exists bug10100pd|
4881 drop procedure if exists bug10100pc|
4882 --enable_warnings
4883 # routines with simple recursion
4884 create function bug10100f(prm int) returns int
4885 begin
4886   if prm > 1 then
4887     return prm * bug10100f(prm - 1);
4888   end if;
4889   return 1;
4890 end|
4891 create procedure bug10100p(prm int, inout res int)
4892 begin
4893   set res = res * prm;
4894   if prm > 1 then
4895     call bug10100p(prm - 1, res);  
4896   end if;
4897 end|
4898 create procedure bug10100t(prm int)
4899 begin
4900   declare res int;
4901   set res = 1;
4902   call bug10100p(prm, res);
4903   select res;
4904 end|
4906 # a procedure which use tables and recursion
4907 create table t3 (a int)|
4908 insert into t3 values (0)|
4909 create view v1 as select a from t3|
4910 create procedure bug10100pt(level int, lim int)
4911 begin
4912   if level < lim then
4913     update t3 set a=level;
4914     FLUSH TABLES;
4915     call bug10100pt(level+1, lim);
4916   else
4917     select * from t3;
4918   end if;
4919 end|
4920 # view & recursion
4921 create procedure bug10100pv(level int, lim int)
4922 begin
4923   if level < lim then
4924     update v1 set a=level;
4925     FLUSH TABLES;
4926     call bug10100pv(level+1, lim);
4927   else
4928     select * from v1;
4929   end if;
4930 end|
4931 # dynamic sql & recursion
4932 prepare stmt2 from "select * from t3;"|
4933 create procedure bug10100pd(level int, lim int)
4934 begin
4935   if level < lim then
4936     select level;
4937     prepare stmt1 from "update t3 set a=a+2";
4938     execute stmt1;
4939     FLUSH TABLES;
4940     execute stmt1;
4941     FLUSH TABLES;
4942     execute stmt1;
4943     FLUSH TABLES;
4944     deallocate prepare stmt1;
4945     execute stmt2;
4946     select * from t3;
4947     call bug10100pd(level+1, lim);
4948   else
4949     execute stmt2;
4950   end if;
4951 end|
4952 # cursor & recursion
4953 create procedure bug10100pc(level int, lim int)
4954 begin
4955   declare lv int;
4956   declare c cursor for select a from t3;
4957   open c;
4958   if level < lim then
4959     select level;
4960     fetch c into lv;
4961     select lv;
4962     update t3 set a=level+lv;
4963     FLUSH TABLES;
4964     call bug10100pc(level+1, lim);
4965   else
4966     select * from t3;
4967   end if;
4968   close c;
4969 end|
4971 set @@max_sp_recursion_depth=4|
4972 select @@max_sp_recursion_depth|
4973 -- error ER_SP_NO_RECURSION
4974 select bug10100f(3)|
4975 -- error ER_SP_NO_RECURSION
4976 select bug10100f(6)|
4977 call bug10100t(5)|
4978 call bug10100pt(1,5)|
4979 call bug10100pv(1,5)|
4980 update t3 set a=1|
4981 call bug10100pd(1,5)|
4982 select * from t3|
4983 update t3 set a=1|
4984 call bug10100pc(1,5)|
4985 select * from t3|
4986 set @@max_sp_recursion_depth=0|
4987 select @@max_sp_recursion_depth|
4988 -- error ER_SP_NO_RECURSION
4989 select bug10100f(5)|
4990 -- error ER_SP_RECURSION_LIMIT
4991 call bug10100t(5)|
4993 #end of the stack checking
4994 deallocate prepare stmt2|
4996 drop function bug10100f|
4997 drop procedure bug10100p|
4998 drop procedure bug10100t|
4999 drop procedure bug10100pt|
5000 drop procedure bug10100pv|
5001 drop procedure bug10100pd|
5002 drop procedure bug10100pc|
5003 drop view v1|
5006 # BUG#13729: Stored procedures: packet error after exception handled
5008 --disable_warnings
5009 drop procedure if exists bug13729|
5010 drop table if exists t3|
5011 --enable_warnings
5013 create table t3 (s1 int, primary key (s1))|
5015 insert into t3 values (1),(2)|
5017 create procedure bug13729()
5018 begin
5019   declare continue handler for sqlexception select 55;
5021   update t3 set s1 = 1;
5022 end|
5024 call bug13729()|
5025 # Used to cause Packets out of order
5026 select * from t3|
5028 drop procedure bug13729|
5029 drop table t3|
5032 # BUG#14643: Stored Procedure: Continuing after failed var. initialization
5033 #            crashes server.
5035 --disable_warnings
5036 drop procedure if exists bug14643_1|
5037 drop procedure if exists bug14643_2|
5038 --enable_warnings
5040 create procedure bug14643_1()
5041 begin
5042   declare continue handler for sqlexception select 'boo' as 'Handler';
5044   begin
5045     declare v int default undefined_var;
5047     if v = 1 then
5048       select 1;
5049     else
5050       select v, isnull(v);
5051     end if;
5052   end;
5053 end|
5055 create procedure bug14643_2()
5056 begin
5057   declare continue handler for sqlexception select 'boo' as 'Handler';
5059   case undefined_var
5060   when 1 then
5061     select 1;
5062   else
5063     select 2;
5064   end case;
5066   select undefined_var;
5067 end|
5069 call bug14643_1()|
5070 call bug14643_2()|
5072 drop procedure bug14643_1|
5073 drop procedure bug14643_2|
5076 # BUG#14304: auto_increment field incorrect set in SP
5078 --disable_warnings
5079 drop procedure if exists bug14304|
5080 drop table if exists t3, t4|
5081 --enable_warnings
5083 create table t3(a int primary key auto_increment)|
5084 create table t4(a int primary key auto_increment)|
5086 create procedure bug14304()
5087 begin
5088   insert into t3 set a=null;
5089   insert into t4 set a=null;
5090   insert into t4 set a=null;
5091   insert into t4 set a=null;
5092   insert into t4 set a=null;
5093   insert into t4 set a=null;
5094   insert into t4 select null as a;
5095   
5096   insert into t3 set a=null;
5097   insert into t3 set a=null;
5098   
5099   select * from t3;
5100 end|
5102 call bug14304()|
5104 drop procedure bug14304|
5105 drop table t3, t4|
5108 # BUG#14376: MySQL crash on scoped variable (re)initialization
5110 --disable_warnings
5111 drop procedure if exists bug14376|
5112 --enable_warnings
5114 create procedure bug14376()
5115 begin
5116   declare x int default x;
5117 end|
5119 # Not the error we want, but that's what we got for now...
5120 --error ER_BAD_FIELD_ERROR
5121 call bug14376()|
5122 drop procedure bug14376|
5124 create procedure bug14376()
5125 begin
5126   declare x int default 42;
5128   begin
5129     declare x int default x;
5131     select x;
5132   end;
5133 end|
5135 call bug14376()|
5137 drop procedure bug14376|
5139 create procedure bug14376(x int)
5140 begin
5141   declare x int default x;
5143   select x;
5144 end|
5146 call bug14376(4711)|
5148 drop procedure bug14376|
5151 # Bug#5967 "Stored procedure declared variable used instead of column"
5152 # The bug should be fixed later.
5153 # Test precedence of names of parameters, variable declarations, 
5154 # variable declarations in nested compound statements, table columns,
5155 # table columns in cursor declarations.
5156 # According to the standard, table columns take precedence over
5157 # variable declarations. In MySQL 5.0 it's vice versa.
5160 --disable_warnings
5161 drop procedure if exists bug5967|
5162 drop table if exists t3|
5163 --enable_warnings
5164 create table t3 (a varchar(255))|
5165 insert into t3 (a) values ("a - table column")|
5166 create procedure bug5967(a varchar(255))
5167 begin
5168   declare i varchar(255);
5169   declare c cursor for select a from t3;
5170   select a;
5171   select a from t3 into i;
5172   select i as 'Parameter takes precedence over table column';                     open c;
5173   fetch c into i;
5174   close c;
5175   select i as 'Parameter takes precedence over table column in cursors';
5176   begin
5177     declare a varchar(255) default 'a - local variable';
5178     declare c1 cursor for select a from t3;
5179     select a as 'A local variable takes precedence over parameter';
5180     open c1;
5181     fetch c1 into i;
5182     close c1;
5183     select i as 'A local variable takes precedence over parameter in cursors';
5184     begin
5185       declare a varchar(255) default 'a - local variable in a nested compound statement';
5186       declare c2 cursor for select a from t3;
5187       select a as 'A local variable in a nested compound statement takes precedence over a local variable in the outer statement';
5188       select a from t3 into i;
5189       select i as  'A local variable in a nested compound statement takes precedence over table column';
5190       open c2;
5191       fetch c2 into i;
5192       close c2;
5193       select i as  'A local variable in a nested compound statement takes precedence over table column in cursors';
5194     end;
5195   end;
5196 end|
5197 call bug5967("a - stored procedure parameter")|
5198 drop procedure bug5967|
5201 # Bug#13012 "SP: REPAIR/BACKUP/RESTORE TABLE crashes the server"
5203 --let $backupdir = $MYSQLTEST_VARDIR/tmp/
5204 --error 0,1
5205 --remove_file $backupdir/t1.frm
5206 --error 0,1
5207 --remove_file $backupdir/t1.MYD
5209 --disable_warnings
5210 drop procedure if exists bug13012|
5211 # Disable warnings also for BACKUP/RESTORE: they are deprecated.
5212 --replace_result $MYSQLTEST_VARDIR <MYSQLTEST_VARDIR>
5213 eval create procedure bug13012()
5214     BEGIN
5215        REPAIR TABLE t1;
5216        BACKUP TABLE t1 to '$backupdir';
5217        DROP TABLE t1;
5218        RESTORE TABLE t1 FROM '$backupdir';
5219     END|
5220 call bug13012()|
5222 --enable_warnings
5223 --remove_file $backupdir/t1.frm
5224 --remove_file $backupdir/t1.MYD
5226 drop procedure bug13012|
5228 create view v1 as select * from t1|
5229 create procedure bug13012()
5230 BEGIN
5231   REPAIR TABLE t1,t2,t3,v1;
5232   OPTIMIZE TABLE t1,t2,t3,v1;
5233   ANALYZE TABLE t1,t2,t3,v1;
5234 END|
5235 call bug13012()|
5236 call bug13012()|
5237 call bug13012()|
5238 drop procedure bug13012|
5239 drop view v1|
5240 select * from t1 order by data|
5243 # A test case for Bug#15392 "Server crashes during prepared statement
5244 # execute": make sure that stored procedure check for error conditions
5245 # properly and do not continue execution if an error has been set. 
5247 # It's necessary to use several DBs because in the original code
5248 # the successful return of mysql_change_db overrode the error from
5249 # execution.
5250 drop schema if exists mysqltest1|
5251 drop schema if exists mysqltest2|
5252 drop schema if exists mysqltest3|
5253 create schema mysqltest1|
5254 create schema mysqltest2|
5255 create schema mysqltest3|
5256 use mysqltest3|
5258 create procedure mysqltest1.p1 (out prequestid varchar(100))
5259 begin
5260   call mysqltest2.p2('call mysqltest3.p3(1, 2)');
5261 end|
5263 create procedure mysqltest2.p2(in psql text)
5264 begin
5265   declare lsql text;
5266   set @lsql= psql;
5267   prepare lstatement from @lsql;
5268   execute lstatement;
5269   deallocate prepare lstatement;
5270 end|
5272 create procedure mysqltest3.p3(in p1 int)
5273 begin
5274   select p1;
5275 end|
5277 --error ER_SP_WRONG_NO_OF_ARGS
5278 call mysqltest1.p1(@rs)|
5279 --error ER_SP_WRONG_NO_OF_ARGS
5280 call mysqltest1.p1(@rs)|
5281 --error ER_SP_WRONG_NO_OF_ARGS
5282 call mysqltest1.p1(@rs)|
5283 drop schema if exists mysqltest1|
5284 drop schema if exists mysqltest2|
5285 drop schema if exists mysqltest3|
5286 use test|
5289 # Bug#15441 "Running SP causes Server to Crash": check that an SP variable
5290 # can not be used in VALUES() function.
5292 --disable_warnings
5293 drop table if exists t3|
5294 drop procedure if exists bug15441|
5295 --enable_warnings
5296 create table t3 (id int not null primary key, county varchar(25))|
5297 insert into t3 (id, county) values (1, 'York')|
5299 # First check that a stored procedure that refers to a parameter in VALUES()
5300 # function won't parse.
5302 create procedure bug15441(c varchar(25))
5303 begin
5304   update t3 set id=2, county=values(c);
5305 end|
5306 --error ER_BAD_FIELD_ERROR
5307 call bug15441('county')|
5308 drop procedure bug15441|
5310 # Now check the case when there is an ambiguity between column names
5311 # and stored procedure parameters: the parser shall resolve the argument
5312 # of VALUES() function to the column name.
5314 # It's hard to deduce what county refers to in every case (INSERT statement):
5315 # 1st county refers to the column
5316 # 2nd county refers to the procedure parameter
5317 # 3d and 4th county refers to the column, again, but
5318 # for 4th county it has the value of SP parameter
5320 # In UPDATE statement, just check that values() function returns NULL for
5321 # non- INSERT...UPDATE statements, as stated in the manual.
5323 create procedure bug15441(county varchar(25))
5324 begin
5325   declare c varchar(25) default "hello";
5327   insert into t3 (id, county) values (1, county)
5328   on duplicate key update county= values(county);
5329   select * from t3;
5331   update t3 set id=2, county=values(id);
5332   select * from t3;
5333 end|
5334 call bug15441('Yale')|
5335 drop table t3|
5336 drop procedure bug15441|
5339 # BUG#14498: Stored procedures: hang if undefined variable and exception
5341 --disable_warnings
5342 drop procedure if exists bug14498_1|
5343 drop procedure if exists bug14498_2|
5344 drop procedure if exists bug14498_3|
5345 drop procedure if exists bug14498_4|
5346 drop procedure if exists bug14498_5|
5347 --enable_warnings
5349 create procedure bug14498_1()
5350 begin
5351   declare continue handler for sqlexception select 'error' as 'Handler';
5353   if v then
5354     select 'yes' as 'v';
5355   else
5356     select 'no' as 'v';
5357   end if;
5358   select 'done' as 'End';
5359 end|
5361 create procedure bug14498_2()
5362 begin
5363   declare continue handler for sqlexception select 'error' as 'Handler';
5365   while v do
5366     select 'yes' as 'v';
5367   end while;
5368   select 'done' as 'End';
5369 end|
5371 create procedure bug14498_3()
5372 begin
5373   declare continue handler for sqlexception select 'error' as 'Handler';
5375   repeat
5376     select 'maybe' as 'v';
5377   until v end repeat;
5378   select 'done' as 'End';
5379 end|
5381 create procedure bug14498_4()
5382 begin
5383   declare continue handler for sqlexception select 'error' as 'Handler';
5385   case v
5386   when 1 then
5387     select '1' as 'v';
5388   when 2 then
5389     select '2' as 'v';
5390   else
5391     select '?' as 'v';
5392   end case;
5393   select 'done' as 'End';
5394 end|
5396 create procedure bug14498_5()
5397 begin
5398   declare continue handler for sqlexception select 'error' as 'Handler';
5400   case
5401   when v = 1 then
5402     select '1' as 'v';
5403   when v = 2 then
5404     select '2' as 'v';
5405   else
5406     select '?' as 'v';
5407   end case;
5408   select 'done' as 'End';
5409 end|
5411 call bug14498_1()|
5412 call bug14498_2()|
5413 call bug14498_3()|
5414 call bug14498_4()|
5415 call bug14498_5()|
5417 drop procedure bug14498_1|
5418 drop procedure bug14498_2|
5419 drop procedure bug14498_3|
5420 drop procedure bug14498_4|
5421 drop procedure bug14498_5|
5424 # BUG#15231: Stored procedure bug with not found condition handler
5426 --disable_warnings
5427 drop table if exists t3|
5428 drop procedure if exists bug15231_1|
5429 drop procedure if exists bug15231_2|
5430 drop procedure if exists bug15231_3|
5431 drop procedure if exists bug15231_4|
5432 --enable_warnings
5434 create table t3 (id int not null)|
5435   
5436 create procedure bug15231_1()
5437 begin
5438   declare xid integer;
5439   declare xdone integer default 0;
5440   declare continue handler for not found set xdone = 1;
5442   set xid=null;
5443   call bug15231_2(xid);
5444   select xid, xdone;
5445 end|
5447 create procedure bug15231_2(inout ioid integer)
5448 begin
5449   select "Before NOT FOUND condition is triggered" as '1';
5450   select id into ioid from t3 where id=ioid;
5451   select "After NOT FOUND condtition is triggered" as '2';
5453   if ioid is null then
5454     set ioid=1;
5455   end if;
5456 end|
5458 create procedure bug15231_3()
5459 begin
5460   declare exit handler for sqlwarning
5461     select 'Caught it (wrong)' as 'Result';
5463   call bug15231_4();
5464 end|
5466 create procedure bug15231_4()
5467 begin
5468   declare x decimal(2,1);
5470   set x = 'zap';
5471   select 'Missed it (correct)' as 'Result';
5472 end|
5474 call bug15231_1()|
5475 call bug15231_3()|
5477 drop table if exists t3|
5478 drop procedure if exists bug15231_1|
5479 drop procedure if exists bug15231_2|
5480 drop procedure if exists bug15231_3|
5481 drop procedure if exists bug15231_4|
5485 # BUG#15011: error handler in nested block not activated
5487 --disable_warnings
5488 drop procedure if exists bug15011|
5489 --enable_warnings
5491 create table t3 (c1 int primary key)|
5493 insert into t3 values (1)|
5495 create procedure bug15011()
5496   deterministic
5497 begin
5498   declare continue handler for 1062
5499     select 'Outer' as 'Handler';
5501   begin
5502     declare continue handler for 1062
5503       select 'Inner' as 'Handler';
5505     insert into t3 values (1);
5506   end;
5507 end|
5509 call bug15011()|
5511 drop procedure bug15011|
5512 drop table t3|
5516 # BUG#17476: Stored procedure not returning data when it is called first
5517 #            time per connection
5519 --disable_warnings
5520 drop procedure if exists bug17476|
5521 --enable_warnings
5523 create table t3 ( d date )|
5524 insert into t3 values
5525   ( '2005-01-01' ), ( '2005-01-02' ), ( '2005-01-03' ),
5526   ( '2005-01-04' ), ( '2005-02-01' ), ( '2005-02-02' )|
5528 create procedure bug17476(pDateFormat varchar(10))
5529   select date_format(t3.d, pDateFormat), count(*)
5530     from t3 
5531     group by date_format(t3.d, pDateFormat)|
5533 call bug17476('%Y-%m')|
5534 call bug17476('%Y-%m')|
5536 drop table t3|
5537 drop procedure bug17476|
5541 # BUG#16887: Cursor causes server segfault
5543 --disable_warnings
5544 drop table if exists t3|
5545 drop procedure if exists bug16887|
5546 --enable_warnings
5548 create table t3 ( c varchar(1) )|
5550 insert into t3 values
5551   (' '),('.'),(';'),(','),('-'),('_'),('('),(')'),('/'),('\\')|
5553 create procedure bug16887()
5554 begin
5555   declare i int default 10;
5557  again:
5558   while i > 0 do
5559   begin
5560     declare breakchar varchar(1);
5561     declare done int default 0;
5562     declare t3_cursor cursor for select c from t3;
5563     declare continue handler for not found set done = 1;
5565     set i = i - 1;
5566     select i;
5568     if i = 3 then
5569       iterate again;
5570     end if;
5572     open t3_cursor;
5574     loop
5575       fetch t3_cursor into breakchar;
5577       if done = 1 then
5578         begin
5579           close t3_cursor;
5580           iterate again;
5581         end;
5582       end if;
5583      end loop;
5584    end;
5585    end while;
5586 end|
5588 call bug16887()|
5590 drop table t3|
5591 drop procedure bug16887|
5594 # BUG#16474: SP crashed MySQL
5595 # (when using "order by localvar", where 'localvar' is just that.
5597 --disable_warnings
5598 drop procedure if exists bug16474_1|
5599 drop procedure if exists bug16474_2|
5600 --enable_warnings
5602 delete from t1|
5603 insert into t1 values ('c', 2), ('b', 3), ('a', 1)|
5605 create procedure bug16474_1()
5606 begin
5607   declare x int;
5609   select id from t1 order by x, id;
5610 end|
5613 # BUG#14945: Truncate table doesn't reset the auto_increment counter
5615 --disable_warnings
5616 drop procedure if exists bug14945|
5617 --enable_warnings
5618 create table t3 (id int not null auto_increment primary key)|
5619 create procedure bug14945() deterministic truncate t3|
5620 insert into t3 values (null)|
5621 call bug14945()|
5622 insert into t3 values (null)|
5623 select * from t3|
5624 drop table t3|
5625 drop procedure bug14945|
5627 # This does NOT order by column index; variable is an expression.
5628 create procedure bug16474_2(x int)
5629   select id from t1 order by x, id|
5631 call bug16474_1()|
5632 call bug16474_2(1)|
5633 call bug16474_2(2)|
5634 drop procedure bug16474_1|
5635 drop procedure bug16474_2|
5637 # For reference: user variables are expressions too and do not affect ordering.
5638 set @x = 2|
5639 select * from t1 order by @x, data|
5641 delete from t1|
5645 # BUG#15728: LAST_INSERT_ID function inside a stored function returns 0
5647 # The solution is not to reset last_insert_id on enter to sub-statement.
5649 --disable_warnings
5650 drop function if exists bug15728|
5651 drop table if exists t3|
5652 --enable_warnings
5654 create table t3 (
5655   id int not null auto_increment,
5656   primary key (id)
5658 create function bug15728() returns int(11)
5659   return last_insert_id()|
5661 insert into t3 values (0)|
5662 select last_insert_id()|
5663 select bug15728()|
5665 drop function bug15728|
5666 drop table t3|
5670 # BUG#18787: Server crashed when calling a stored procedure containing
5671 #            a misnamed function
5673 --disable_warnings
5674 drop procedure if exists bug18787|
5675 --enable_warnings
5676 create procedure bug18787()
5677 begin
5678   declare continue handler for sqlexception begin end;
5680   select no_such_function();
5681 end|
5683 call bug18787()|
5684 drop procedure bug18787|
5688 # BUG#18344: DROP DATABASE does not drop associated routines
5689 # (... if the database name is longer than 21 characters)
5691 #               1234567890123456789012
5692 create database bug18344_012345678901| 
5693 use bug18344_012345678901|
5694 create procedure bug18344() begin end|
5695 create procedure bug18344_2() begin end|
5697 create database bug18344_0123456789012| 
5698 use bug18344_0123456789012|
5699 create procedure bug18344() begin end|
5700 create procedure bug18344_2() begin end|
5702 use test|
5704 select schema_name from information_schema.schemata where 
5705   schema_name like 'bug18344%'|
5706 select routine_name,routine_schema from information_schema.routines where
5707   routine_schema like 'bug18344%'|
5709 drop database bug18344_012345678901| 
5710 drop database bug18344_0123456789012| 
5712 # Should be nothing left.
5713 select schema_name from information_schema.schemata where 
5714   schema_name like 'bug18344%'|
5715 select routine_name,routine_schema from information_schema.routines where
5716   routine_schema like 'bug18344%'|
5720 # BUG#12472/BUG#15137 'CREATE TABLE ... SELECT ... which explicitly or
5721 # implicitly uses stored function gives "Table not locked" error'.
5723 --disable_warnings
5724 drop function if exists bug12472|
5725 --enable_warnings
5726 create function bug12472() returns int return (select count(*) from t1)|
5727 # Check case when function is used directly
5728 create table t3 as select bug12472() as i|
5729 show create table t3|
5730 select * from t3|
5731 drop table t3|
5732 # Check case when function is used indirectly through view
5733 create view v1 as select bug12472() as j|
5734 create table t3 as select * from v1|
5735 show create table t3|
5736 select * from t3|
5737 drop table t3|
5738 drop view v1|
5739 drop function bug12472|
5743 # BUG#18587: Function that accepts and returns TEXT garbles data if longer than
5744 # 766 chars
5747 # Prepare.
5749 --disable_warnings
5750 DROP FUNCTION IF EXISTS bug18589_f1|
5751 DROP PROCEDURE IF EXISTS bug18589_p1|
5752 DROP PROCEDURE IF EXISTS bug18589_p2|
5753 --enable_warnings
5755 CREATE FUNCTION bug18589_f1(arg TEXT) RETURNS TEXT
5756 BEGIN
5757   RETURN CONCAT(arg, "");
5758 END|
5760 CREATE PROCEDURE bug18589_p1(arg TEXT, OUT ret TEXT)
5761 BEGIN
5762   SET ret = CONCAT(arg, "");
5763 END|
5765 CREATE PROCEDURE bug18589_p2(arg TEXT)
5766 BEGIN
5767   DECLARE v TEXT;
5768   CALL bug18589_p1(arg, v);
5769   SELECT v;
5770 END|
5772 # Test case.
5774 SELECT bug18589_f1(REPEAT("a", 767))|
5776 SET @bug18589_v1 = ""|
5777 CALL bug18589_p1(REPEAT("a", 767), @bug18589_v1)|
5778 SELECT @bug18589_v1|
5780 CALL bug18589_p2(REPEAT("a", 767))|
5782 # Cleanup.
5784 DROP FUNCTION bug18589_f1|
5785 DROP PROCEDURE bug18589_p1|
5786 DROP PROCEDURE bug18589_p2|
5790 # BUG#18037: Server crash when returning system variable in stored procedures
5791 # BUG#19633: Stack corruption in fix_fields()/THD::rollback_item_tree_changes()
5794 # Prepare.
5796 --disable_warnings
5797 DROP FUNCTION IF EXISTS bug18037_f1|
5798 DROP PROCEDURE IF EXISTS bug18037_p1|
5799 DROP PROCEDURE IF EXISTS bug18037_p2|
5800 --enable_warnings
5802 # Test case.
5804 CREATE FUNCTION bug18037_f1() RETURNS INT
5805 BEGIN
5806   RETURN @@server_id;
5807 END|
5809 CREATE PROCEDURE bug18037_p1()
5810 BEGIN
5811   DECLARE v INT DEFAULT @@server_id;
5812 END|
5814 CREATE PROCEDURE bug18037_p2()
5815 BEGIN
5816   CASE @@server_id
5817   WHEN -1 THEN
5818     SELECT 0;
5819   ELSE
5820     SELECT 1;
5821   END CASE;
5822 END|
5824 SELECT bug18037_f1()|
5825 CALL bug18037_p1()|
5826 CALL bug18037_p2()|
5828 # Cleanup.
5830 DROP FUNCTION bug18037_f1|
5831 DROP PROCEDURE bug18037_p1|
5832 DROP PROCEDURE bug18037_p2|
5835 # Bug#17199: "Table not found" error occurs if the query contains a call
5836 #            to a function from another database.
5837 #            See also ps.test for an additional test case for this bug.
5839 use test|
5840 create table t3 (i int)|
5841 insert into t3 values (1), (2)|
5842 create database mysqltest1|
5843 use mysqltest1|
5844 create function bug17199() returns varchar(2) deterministic return 'ok'|
5845 use test|
5846 select *, mysqltest1.bug17199() from t3|
5848 # Bug#18444: Fully qualified stored function names don't work correctly
5849 #            in select statements
5851 use mysqltest1|
5852 create function bug18444(i int) returns int no sql deterministic return i + 1|
5853 use test|
5854 select mysqltest1.bug18444(i) from t3|
5855 drop database mysqltest1|
5857 # Check that current database has no influence to a stored procedure
5859 create database mysqltest1 charset=utf8|
5860 create database mysqltest2 charset=utf8|
5861 create procedure mysqltest1.p1()
5862 begin
5863 -- alters the default collation of database test 
5864   alter database character set koi8r;
5865 end|
5866 use mysqltest1|
5867 call p1()|
5868 show create database mysqltest1|
5869 show create database mysqltest2|
5870 alter database mysqltest1 character set utf8|
5871 use mysqltest2|
5872 call mysqltest1.p1()|
5873 show create database mysqltest1|
5874 show create database mysqltest2|
5875 drop database mysqltest1|
5876 drop database mysqltest2|
5878 # Restore the old environemnt
5879 use test|
5881 # Bug#15217 "Using a SP cursor on a table created with PREPARE fails with
5882 #           weird error". Check that the code that is supposed to work at
5883 #           the first execution of a stored procedure actually works for
5884 #           sp_instr_copen.
5886 --disable_warnings
5887 drop table if exists t3|
5888 drop procedure if exists bug15217|
5889 --enable_warnings
5890 create table t3 as select 1|
5891 create procedure bug15217()
5892 begin
5893   declare var1 char(255);
5894   declare cur1 cursor for select * from t3;
5895   open cur1;
5896   fetch cur1 into var1;
5897   select concat('data was: /', var1, '/');
5898   close cur1;
5899 end |
5900 # Returns expected result
5901 call bug15217()|
5902 flush tables |
5903 # Returns error with garbage as column name
5904 call bug15217()|
5905 drop table t3|
5906 drop procedure bug15217|
5910 # BUG#21013: Performance Degrades when importing data that uses
5911 # Trigger and Stored Procedure
5913 # This is a performance and memory leak test.  Run with large number
5914 # passed to bug21013() procedure.
5916 --disable_warnings
5917 DROP PROCEDURE IF EXISTS bug21013 |
5918 --enable_warnings
5920 CREATE PROCEDURE bug21013(IN lim INT)
5921 BEGIN
5922   DECLARE i INT DEFAULT 0;
5923   WHILE (i < lim) DO
5924     SET @b = LOCATE(_latin1'b', @a, 1);
5925     SET i = i + 1;
5926   END WHILE;
5927 END |
5929 SET @a = _latin2"aaaaaaaaaa" |
5930 CALL bug21013(10) |
5932 DROP PROCEDURE bug21013 |
5936 # BUG#16211: Stored function return type for strings is ignored
5939 # Prepare: create database with fixed, pre-defined character set.
5941 --disable_warnings
5942 DROP DATABASE IF EXISTS mysqltest1|
5943 DROP DATABASE IF EXISTS mysqltest2|
5944 --enable_warnings
5946 CREATE DATABASE mysqltest1 DEFAULT CHARACTER SET utf8|
5947 CREATE DATABASE mysqltest2 DEFAULT CHARACTER SET utf8|
5949 # Test case:
5951 use mysqltest1|
5953 #   - Create two stored functions -- with and without explicit CHARSET-clause
5954 #     for return value;
5956 CREATE FUNCTION bug16211_f1() RETURNS CHAR(10)
5957   RETURN ""|
5959 CREATE FUNCTION bug16211_f2() RETURNS CHAR(10) CHARSET koi8r
5960   RETURN ""|
5962 CREATE FUNCTION mysqltest2.bug16211_f3() RETURNS CHAR(10)
5963   RETURN ""|
5965 CREATE FUNCTION mysqltest2.bug16211_f4() RETURNS CHAR(10) CHARSET koi8r
5966   RETURN ""|
5968 #   - Check that CHARSET-clause is specified for the second function;
5970 SHOW CREATE FUNCTION bug16211_f1|
5971 SHOW CREATE FUNCTION bug16211_f2|
5973 SHOW CREATE FUNCTION mysqltest2.bug16211_f3|
5974 SHOW CREATE FUNCTION mysqltest2.bug16211_f4|
5976 SELECT dtd_identifier
5977 FROM INFORMATION_SCHEMA.ROUTINES
5978 WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f1"|
5980 SELECT dtd_identifier
5981 FROM INFORMATION_SCHEMA.ROUTINES
5982 WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f2"|
5984 SELECT dtd_identifier
5985 FROM INFORMATION_SCHEMA.ROUTINES
5986 WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f3"|
5988 SELECT dtd_identifier
5989 FROM INFORMATION_SCHEMA.ROUTINES
5990 WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f4"|
5992 SELECT CHARSET(bug16211_f1())|
5993 SELECT CHARSET(bug16211_f2())|
5995 SELECT CHARSET(mysqltest2.bug16211_f3())|
5996 SELECT CHARSET(mysqltest2.bug16211_f4())|
5998 #   - Alter database character set.
6000 ALTER DATABASE mysqltest1 CHARACTER SET cp1251|
6001 ALTER DATABASE mysqltest2 CHARACTER SET cp1251|
6003 #   - Check that CHARSET-clause has not changed.
6005 SHOW CREATE FUNCTION bug16211_f1|
6006 SHOW CREATE FUNCTION bug16211_f2|
6008 SHOW CREATE FUNCTION mysqltest2.bug16211_f3|
6009 SHOW CREATE FUNCTION mysqltest2.bug16211_f4|
6011 SELECT dtd_identifier
6012 FROM INFORMATION_SCHEMA.ROUTINES
6013 WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f1"|
6015 SELECT dtd_identifier
6016 FROM INFORMATION_SCHEMA.ROUTINES
6017 WHERE ROUTINE_SCHEMA = "mysqltest1" AND ROUTINE_NAME = "bug16211_f2"|
6019 SELECT dtd_identifier
6020 FROM INFORMATION_SCHEMA.ROUTINES
6021 WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f3"|
6023 SELECT dtd_identifier
6024 FROM INFORMATION_SCHEMA.ROUTINES
6025 WHERE ROUTINE_SCHEMA = "mysqltest2" AND ROUTINE_NAME = "bug16211_f4"|
6027 SELECT CHARSET(bug16211_f1())|
6028 SELECT CHARSET(bug16211_f2())|
6030 SELECT CHARSET(mysqltest2.bug16211_f3())|
6031 SELECT CHARSET(mysqltest2.bug16211_f4())|
6033 # Cleanup.
6035 use test|
6037 DROP DATABASE mysqltest1|
6038 DROP DATABASE mysqltest2|
6042 # BUG#16676: Database CHARSET not used for stored procedures
6045 # Prepare: create database with fixed, pre-defined character set.
6047 --disable_warnings
6048 DROP DATABASE IF EXISTS mysqltest1|
6049 --enable_warnings
6051 CREATE DATABASE mysqltest1 DEFAULT CHARACTER SET utf8|
6053 # Test case:
6055 use mysqltest1|
6057 #   - Create two stored procedures -- with and without explicit CHARSET-clause;
6059 CREATE PROCEDURE bug16676_p1(
6060   IN p1 CHAR(10),
6061   INOUT p2 CHAR(10),
6062   OUT p3 CHAR(10))
6063 BEGIN
6064   SELECT CHARSET(p1), COLLATION(p1);
6065   SELECT CHARSET(p2), COLLATION(p2);
6066   SELECT CHARSET(p3), COLLATION(p3);
6067 END|
6069 CREATE PROCEDURE bug16676_p2(
6070   IN p1 CHAR(10) CHARSET koi8r,
6071   INOUT p2 CHAR(10) CHARSET cp1251,
6072   OUT p3 CHAR(10) CHARSET greek)
6073 BEGIN
6074   SELECT CHARSET(p1), COLLATION(p1);
6075   SELECT CHARSET(p2), COLLATION(p2);
6076   SELECT CHARSET(p3), COLLATION(p3);
6077 END|
6079 #   - Call procedures.
6081 SET @v2 = 'b'|
6082 SET @v3 = 'c'|
6084 CALL bug16676_p1('a', @v2, @v3)|
6085 CALL bug16676_p2('a', @v2, @v3)|
6087 # Cleanup.
6089 use test|
6091 DROP DATABASE mysqltest1|
6093 # BUG#8153: Stored procedure with subquery and continue handler, wrong result
6096 --disable_warnings
6097 drop table if exists t3|
6098 drop table if exists t4|
6099 drop procedure if exists bug8153_subselect|
6100 drop procedure if exists bug8153_subselect_a|
6101 drop procedure if exists bug8153_subselect_b|
6102 drop procedure if exists bug8153_proc_a|
6103 drop procedure if exists bug8153_proc_b|
6104 --enable_warnings
6106 create table t3 (a int)|
6107 create table t4 (a int)|
6108 insert into t3 values (1), (1), (2), (3)|
6109 insert into t4 values (1), (1)|
6111 ## Testing the use case reported in Bug#8153
6113 create procedure bug8153_subselect()
6114 begin
6115   declare continue handler for sqlexception
6116   begin
6117     select 'statement failed';
6118   end;
6119   update t3 set a=a+1 where (select a from t4 where a=1) is null;
6120   select 'statement after update';
6121 end|
6123 call bug8153_subselect()|
6124 select * from t3|
6126 call bug8153_subselect()|
6127 select * from t3|
6129 drop procedure bug8153_subselect|
6131 ## Testing a subselect with a non local handler
6133 create procedure bug8153_subselect_a()
6134 begin
6135   declare continue handler for sqlexception
6136   begin
6137     select 'in continue handler';
6138   end;
6140   select 'reachable code a1';
6141   call bug8153_subselect_b();
6142   select 'reachable code a2';
6143 end|
6145 create procedure bug8153_subselect_b()
6146 begin
6147   select 'reachable code b1';
6148   update t3 set a=a+1 where (select a from t4 where a=1) is null;
6149   select 'unreachable code b2';
6150 end|
6152 call bug8153_subselect_a()|
6153 select * from t3|
6155 call bug8153_subselect_a()|
6156 select * from t3|
6158 drop procedure bug8153_subselect_a|
6159 drop procedure bug8153_subselect_b|
6161 ## Testing extra use cases, found while investigating
6162 ## This is related to BUG#18787, with a non local handler
6164 create procedure bug8153_proc_a()
6165 begin
6166   declare continue handler for sqlexception
6167   begin
6168     select 'in continue handler';
6169   end;
6171   select 'reachable code a1';
6172   call bug8153_proc_b();
6173   select 'reachable code a2';
6174 end|
6176 create procedure bug8153_proc_b()
6177 begin
6178   select 'reachable code b1';
6179   select no_such_function();
6180   select 'unreachable code b2';
6181 end|
6183 call bug8153_proc_a()|
6185 drop procedure bug8153_proc_a|
6186 drop procedure bug8153_proc_b|
6187 drop table t3|
6188 drop table t4|
6191 # BUG#19862: Sort with filesort by function evaluates function twice
6193 --disable_warnings
6194 drop procedure if exists bug19862|
6195 --enable_warnings
6196 CREATE TABLE t11 (a INT)|
6197 CREATE TABLE t12 (a INT)|
6198 CREATE FUNCTION bug19862(x INT) RETURNS INT
6199   BEGIN
6200     INSERT INTO t11 VALUES (x);
6201     RETURN x+1;
6202   END|
6203 INSERT INTO t12 VALUES (1), (2)|
6204 SELECT bug19862(a) FROM t12 ORDER BY 1|
6205 SELECT * FROM t11|
6206 DROP TABLE t11, t12|
6207 DROP FUNCTION bug19862|
6210 # Bug#21002 "Derived table not selecting from a "real" table fails in JOINs"
6211 #         
6212 # A regression caused by the fix for Bug#18444: for derived tables we should
6213 # set an empty string as the current database. They do not belong to any
6214 # database and must be usable even if there is no database
6215 # selected.
6216 --disable_warnings
6217 drop table if exists t3|
6218 drop database if exists mysqltest1|
6219 --enable_warnings
6220 create table t3 (a int)|
6221 insert into t3 (a) values (1), (2)|
6223 create database mysqltest1|
6224 use mysqltest1|
6225 drop database mysqltest1|
6227 # No current database
6228 select database()|
6230 select * from (select 1 as a) as t1 natural join (select * from test.t3) as t2|
6231 use test|
6232 drop table t3|
6235 # Test for BUG#16899: Possible buffer overflow in handling of DEFINER-clause.
6237 # Prepare.
6239 --disable_warnings
6240 DROP PROCEDURE IF EXISTS bug16899_p1|
6241 DROP FUNCTION IF EXISTS bug16899_f1|
6242 --enable_warnings
6244 --error ER_WRONG_STRING_LENGTH
6245 CREATE DEFINER=1234567890abcdefGHIKL@localhost PROCEDURE bug16899_p1()
6246 BEGIN
6247   SET @a = 1;
6248 END|
6250 --error ER_WRONG_STRING_LENGTH
6251 CREATE DEFINER=some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY
6252   FUNCTION bug16899_f1() RETURNS INT
6253 BEGIN
6254   RETURN 1;
6255 END|
6259 # BUG#21416: SP: Recursion level higher than zero needed for non-recursive call
6261 --disable_warnings
6262 drop procedure if exists bug21416|
6263 --enable_warnings
6264 create procedure bug21416() show create procedure bug21416|
6265 call bug21416()|
6266 drop procedure bug21416|
6270 # BUG#21414: SP: Procedure undroppable, to some extent
6272 --disable_warnings
6273 DROP PROCEDURE IF EXISTS bug21414|
6274 --enable_warnings
6276 CREATE PROCEDURE bug21414() SELECT 1|
6278 FLUSH TABLES WITH READ LOCK|
6280 --error ER_CANT_UPDATE_WITH_READLOCK
6281 DROP PROCEDURE bug21414|
6283 UNLOCK TABLES|
6285 --echo The following should succeed.
6286 DROP PROCEDURE bug21414|
6290 # BUG#21311: Possible stack overrun if SP has non-latin1 name
6292 set names utf8|
6293 --disable_warnings
6294 drop database if exists това_е_дълго_име_за_база_данни_нали|
6295 --enable_warnings
6296 create database това_е_дълго_име_за_база_данни_нали|
6297 INSERT INTO mysql.proc VALUES ('това_е_дълго_име_за_база_данни_нали','това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго','PROCEDURE','това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго','SQL','CONTAINS_SQL','NO','DEFINER','','','bad_body','root@localhost',now(), now(),'','', 'utf8', 'utf8_general_ci', 'utf8_general_ci', 'n/a')|
6298 --error ER_SP_PROC_TABLE_CORRUPT
6299 call това_е_дълго_име_за_база_данни_нали.това_е_процедура_с_доста_дълго_име_нали_и_още_по_дълго()|
6300 drop database това_е_дълго_име_за_база_данни_нали|
6304 # BUG#21493: Crash on the second call of a procedure containing
6305 #            a select statement that uses an IN aggregating subquery  
6308 CREATE TABLE t3 (
6309   Member_ID varchar(15) NOT NULL,
6310   PRIMARY KEY (Member_ID)
6313 CREATE TABLE t4 (
6314   ID int(10) unsigned NOT NULL auto_increment,
6315   Member_ID varchar(15) NOT NULL default '',
6316   Action varchar(12) NOT NULL,
6317   Action_Date datetime NOT NULL,
6318   Track varchar(15) default NULL,
6319   User varchar(12) default NULL,
6320   Date_Updated timestamp NOT NULL default CURRENT_TIMESTAMP on update
6321     CURRENT_TIMESTAMP,
6322   PRIMARY KEY (ID),
6323   KEY Action (Action),
6324   KEY Action_Date (Action_Date)
6328 INSERT INTO t3(Member_ID) VALUES
6329   ('111111'), ('222222'), ('333333'), ('444444'), ('555555'), ('666666')|
6331 INSERT INTO t4(Member_ID, Action, Action_Date, Track) VALUES
6332   ('111111', 'Disenrolled', '2006-03-01', 'CAD' ),
6333   ('111111', 'Enrolled', '2006-03-01', 'CAD' ),
6334   ('111111', 'Disenrolled', '2006-07-03', 'CAD' ),
6335   ('222222', 'Enrolled', '2006-03-07', 'CAD' ),
6336   ('222222', 'Enrolled', '2006-03-07', 'CHF' ),
6337   ('222222', 'Disenrolled', '2006-08-02', 'CHF' ),
6338   ('333333', 'Enrolled', '2006-03-01', 'CAD' ),
6339   ('333333', 'Disenrolled', '2006-03-01', 'CAD' ),
6340   ('444444', 'Enrolled', '2006-03-01', 'CAD' ),
6341   ('555555', 'Disenrolled', '2006-03-01', 'CAD' ),
6342   ('555555', 'Enrolled', '2006-07-21', 'CAD' ),
6343   ('555555', 'Disenrolled', '2006-03-01', 'CHF' ),
6344   ('666666', 'Enrolled', '2006-02-09', 'CAD' ),
6345   ('666666', 'Enrolled', '2006-05-12', 'CHF' ),
6346   ('666666', 'Disenrolled', '2006-06-01', 'CAD' )|
6348 --disable_warnings
6349 DROP FUNCTION IF EXISTS bug21493|
6350 --enable_warnings
6352 CREATE FUNCTION bug21493(paramMember VARCHAR(15)) RETURNS varchar(45)
6353 BEGIN
6354 DECLARE tracks VARCHAR(45);
6355 SELECT GROUP_CONCAT(Track SEPARATOR ', ') INTO tracks FROM t4
6356   WHERE Member_ID=paramMember AND Action='Enrolled' AND 
6357         (Track,Action_Date) IN (SELECT Track, MAX(Action_Date) FROM t4
6358                                   WHERE Member_ID=paramMember GROUP BY Track);
6359 RETURN tracks;
6360 END|
6362 SELECT bug21493('111111')|
6363 SELECT bug21493('222222')|
6365 SELECT bug21493(Member_ID) FROM t3|
6367 DROP FUNCTION bug21493|
6368 DROP TABLE t3,t4|
6371 # Bug#20028 Function with select return no data
6374 --disable_warnings
6375 drop function if exists func_20028_a|
6376 drop function if exists func_20028_b|
6377 drop function if exists func_20028_c|
6378 drop procedure if exists proc_20028_a|
6379 drop procedure if exists proc_20028_b|
6380 drop procedure if exists proc_20028_c|
6381 drop table if exists table_20028|
6382 --enable_warnings
6384 create table table_20028 (i int)|
6386 SET @save_sql_mode=@@sql_mode|
6388 SET sql_mode=''|
6390 create function func_20028_a() returns integer
6391 begin
6392   declare temp integer;
6393   select i into temp from table_20028 limit 1;
6394   return ifnull(temp, 0);
6395 end|
6397 create function func_20028_b() returns integer
6398 begin
6399   return func_20028_a();
6400 end|
6402 create function func_20028_c() returns integer
6403 begin
6404   declare div_zero integer;
6405   set SQL_MODE='TRADITIONAL';
6406   select 1/0 into div_zero;
6407   return div_zero;
6408 end|
6410 create procedure proc_20028_a()
6411 begin
6412   declare temp integer;
6413   select i into temp from table_20028 limit 1;
6414 end|
6416 create procedure proc_20028_b()
6417 begin
6418   call proc_20028_a();
6419 end|
6421 create procedure proc_20028_c()
6422 begin
6423   declare div_zero integer;
6424   set SQL_MODE='TRADITIONAL';
6425   select 1/0 into div_zero;
6426 end|
6428 select func_20028_a()|
6429 select func_20028_b()|
6430 --error ER_DIVISION_BY_ZERO
6431 select func_20028_c()|
6432 call proc_20028_a()|
6433 call proc_20028_b()|
6434 --error ER_DIVISION_BY_ZERO
6435 call proc_20028_c()|
6437 SET sql_mode='TRADITIONAL'|
6439 drop function func_20028_a|
6440 drop function func_20028_b|
6441 drop function func_20028_c|
6442 drop procedure proc_20028_a|
6443 drop procedure proc_20028_b|
6444 drop procedure proc_20028_c|
6446 create function func_20028_a() returns integer
6447 begin
6448   declare temp integer;
6449   select i into temp from table_20028 limit 1;
6450   return ifnull(temp, 0);
6451 end|
6453 create function func_20028_b() returns integer
6454 begin
6455   return func_20028_a();
6456 end|
6458 create function func_20028_c() returns integer
6459 begin
6460   declare div_zero integer;
6461   set SQL_MODE='';
6462   select 1/0 into div_zero;
6463   return div_zero;
6464 end|
6466 create procedure proc_20028_a()
6467 begin
6468   declare temp integer;
6469   select i into temp from table_20028 limit 1;
6470 end|
6472 create procedure proc_20028_b()
6473 begin
6474   call proc_20028_a();
6475 end|
6477 create procedure proc_20028_c()
6478 begin
6479   declare div_zero integer;
6480   set SQL_MODE='';
6481   select 1/0 into div_zero;
6482 end|
6484 select func_20028_a()|
6485 select func_20028_b()|
6486 select func_20028_c()|
6487 call proc_20028_a()|
6488 call proc_20028_b()|
6489 call proc_20028_c()|
6491 SET @@sql_mode=@save_sql_mode|
6493 drop function func_20028_a|
6494 drop function func_20028_b|
6495 drop function func_20028_c|
6496 drop procedure proc_20028_a|
6497 drop procedure proc_20028_b|
6498 drop procedure proc_20028_c|
6499 drop table table_20028|
6502 # Bug#21462 Stored procedures with no arguments require parenthesis
6505 --disable_warnings
6506 drop procedure if exists proc_21462_a|
6507 drop procedure if exists proc_21462_b|
6508 --enable_warnings
6510 create procedure proc_21462_a()
6511 begin
6512   select "Called A";
6513 end|
6515 create procedure proc_21462_b(x int)
6516 begin
6517   select "Called B";
6518 end|
6520 call proc_21462_a|
6521 call proc_21462_a()|
6522 -- error ER_SP_WRONG_NO_OF_ARGS
6523 call proc_21462_a(1)|
6525 -- error ER_SP_WRONG_NO_OF_ARGS
6526 call proc_21462_b|
6527 -- error ER_SP_WRONG_NO_OF_ARGS
6528 call proc_21462_b()|
6529 call proc_21462_b(1)|
6531 drop procedure proc_21462_a|
6532 drop procedure proc_21462_b|
6536 # Bug#19733 "Repeated alter, or repeated create/drop, fails"
6537 # Check that CREATE/DROP INDEX is re-execution friendly.
6539 --disable_warnings
6540 drop table if exists t3|
6541 drop procedure if exists proc_bug19733|
6542 --enable_warnings
6543 create table t3 (s1 int)|
6545 create procedure proc_bug19733()
6546 begin
6547   declare v int default 0;
6548   while v < 100 do
6549     create index i on t3 (s1);
6550     drop index i on t3;
6551     set v = v + 1;
6552   end while;
6553 end|
6555 call proc_bug19733()|
6556 call proc_bug19733()|
6557 call proc_bug19733()|
6559 drop procedure proc_bug19733|
6560 drop table t3|
6564 # BUG#20492: Subsequent calls to stored procedure yeild incorrect
6565 # result if join is used 
6567 # Optimized ON expression in join wasn't properly saved for reuse.
6569 --disable_warnings
6570 DROP PROCEDURE IF EXISTS p1|
6571 DROP VIEW IF EXISTS v1, v2|
6572 DROP TABLE IF EXISTS t3, t4|
6573 --enable_warnings
6575 CREATE TABLE t3 (t3_id INT)|
6577 INSERT INTO t3 VALUES (0)|
6578 INSERT INTO t3 VALUES (1)|
6580 CREATE TABLE t4 (t4_id INT)|
6582 INSERT INTO t4 VALUES (2)|
6584 CREATE VIEW v1 AS
6585 SELECT t3.t3_id, t4.t4_id
6586 FROM t3 JOIN t4 ON t3.t3_id = 0|
6588 CREATE VIEW v2 AS
6589 SELECT t3.t3_id AS t3_id_1, v1.t3_id AS t3_id_2, v1.t4_id
6590 FROM t3 LEFT JOIN v1 ON t3.t3_id = 0|
6592 CREATE PROCEDURE p1() SELECT * FROM v2|
6594 # Results should not differ.
6595 CALL p1()|
6596 CALL p1()|
6598 DROP PROCEDURE p1|
6599 DROP VIEW v1, v2|
6600 DROP TABLE t3, t4|
6602 --echo End of 5.0 tests
6604 --echo Begin of 5.1 tests
6607 # BUG#18239: Possible to overload internal functions with stored functions
6610 delimiter ;|
6612 --disable_warnings
6613 drop function if exists pi;
6614 --enable_warnings
6616 create function pi() returns varchar(50)
6617 return "pie, my favorite desert.";
6619 SET @save_sql_mode=@@sql_mode;
6621 SET SQL_MODE='IGNORE_SPACE';
6623 select pi(), pi ();
6625 # Non deterministic warnings from db_load_routine
6626 --disable_warnings
6627 select test.pi(), test.pi ();
6628 --enable_warnings
6630 SET SQL_MODE='';
6632 select pi(), pi ();
6634 # Non deterministic warnings from db_load_routine
6635 --disable_warnings
6636 select test.pi(), test.pi ();
6637 --enable_warnings
6639 SET @@sql_mode=@save_sql_mode;
6641 drop function pi;
6642 # End of BUG#18239
6645 # BUG#22619: Spaces considered harmful
6648 --disable_warnings
6649 drop function if exists test.database;
6650 drop function if exists test.current_user;
6651 drop function if exists test.md5;
6652 --enable_warnings
6654 create database nowhere;
6655 use nowhere;
6656 drop database nowhere;
6658 SET @save_sql_mode=@@sql_mode;
6660 SET SQL_MODE='IGNORE_SPACE';
6662 select database(), database ();
6663 select current_user(), current_user ();
6664 select md5("aaa"), md5 ("aaa");
6666 SET SQL_MODE='';
6668 select database(), database ();
6669 select current_user(), current_user ();
6670 select md5("aaa"), md5 ("aaa");
6672 use test;
6674 create function `database`() returns varchar(50)
6675 return "Stored function database";
6677 create function `current_user`() returns varchar(50)
6678 return "Stored function current_user";
6680 create function md5(x varchar(50)) returns varchar(50)
6681 return "Stored function md5";
6683 SET SQL_MODE='IGNORE_SPACE';
6685 select database(), database ();
6686 select current_user(), current_user ();
6687 select md5("aaa"), md5 ("aaa");
6689 # Non deterministic warnings from db_load_routine
6690 --disable_warnings
6691 select test.database(), test.database ();
6692 select test.current_user(), test.current_user ();
6693 select test.md5("aaa"), test.md5 ("aaa");
6694 --enable_warnings
6696 SET SQL_MODE='';
6698 select database(), database ();
6699 select current_user(), current_user ();
6700 select md5("aaa"), md5 ("aaa");
6702 # Non deterministic warnings from db_load_routine
6703 --disable_warnings
6704 select test.database(), test.database ();
6705 select test.current_user(), test.current_user ();
6706 select test.md5("aaa"), test.md5 ("aaa");
6707 --enable_warnings
6709 SET @@sql_mode=@save_sql_mode;
6711 drop function test.database;
6712 drop function test.current_user;
6713 drop function md5;
6715 use test;
6716 delimiter |;
6717 # End of BUG#22619
6719 --echo End of 5.1 tests
6722 # BUG#23760: ROW_COUNT() and store procedure not owrking together
6724 --disable_warnings
6725 DROP TABLE IF EXISTS bug23760|
6726 DROP TABLE IF EXISTS bug23760_log|
6727 DROP PROCEDURE IF EXISTS bug23760_update_log|
6728 DROP PROCEDURE IF EXISTS bug23760_test_row_count|
6729 DROP FUNCTION IF EXISTS bug23760_rc_test|
6730 --enable_warnings
6731 CREATE TABLE bug23760 (
6732   id INT NOT NULL AUTO_INCREMENT ,
6733   num INT NOT NULL ,
6734   PRIMARY KEY ( id ) 
6737 CREATE TABLE bug23760_log (
6738  id INT NOT NULL AUTO_INCREMENT ,
6739  reason VARCHAR(50)NULL ,
6740  ammount INT NOT NULL ,
6741   PRIMARY KEY ( id ) 
6744 CREATE PROCEDURE bug23760_update_log(r Varchar(50), a INT)
6745 BEGIN
6746   INSERT INTO bug23760_log (reason, ammount) VALUES(r, a);
6747 END|
6749 CREATE PROCEDURE bug23760_test_row_count()
6750 BEGIN
6751   UPDATE bug23760 SET num = num + 1;
6752   CALL bug23760_update_log('Test is working', ROW_COUNT());
6753   UPDATE bug23760 SET num = num - 1;
6754 END|
6757 CREATE PROCEDURE bug23760_test_row_count2(level INT)
6758 BEGIN
6759   IF level THEN
6760     UPDATE bug23760 SET num = num + 1;
6761     CALL bug23760_update_log('Test2 is working', ROW_COUNT());
6762     CALL bug23760_test_row_count2(level - 1);
6763   END IF;
6764 END|
6766 CREATE FUNCTION bug23760_rc_test(in_var INT) RETURNS INT RETURN in_var|
6768 INSERT INTO bug23760 (num) VALUES (0), (1), (1), (2), (3), (5), (8)|
6769 SELECT ROW_COUNT()|
6771 CALL bug23760_test_row_count()|
6772 SELECT * FROM bug23760_log ORDER BY id|
6774 SET @save_max_sp_recursion= @@max_sp_recursion_depth|
6775 SELECT @save_max_sp_recursion|
6776 SET max_sp_recursion_depth= 5|
6777 SELECT @@max_sp_recursion_depth|
6778 CALL bug23760_test_row_count2(2)|
6779 SELECT ROW_COUNT()|
6780 SELECT * FROM bug23760_log ORDER BY id|
6781 SELECT * FROM bug23760 ORDER by ID|
6782 SET max_sp_recursion_depth= @save_max_sp_recursion|
6784 SELECT bug23760_rc_test(123)|
6785 INSERT INTO bug23760 (num) VALUES (13), (21), (34), (55)|
6786 SELECT bug23760_rc_test(ROW_COUNT())|
6788 DROP TABLE bug23760, bug23760_log|
6789 DROP PROCEDURE bug23760_update_log|
6790 DROP PROCEDURE bug23760_test_row_count|
6791 DROP PROCEDURE bug23760_test_row_count2|
6792 DROP FUNCTION bug23760_rc_test|
6795 # BUG#24117: server crash on a FETCH with a cursor on a table which is not in
6796 #            the table cache
6799 --disable_warnings
6800 DROP PROCEDURE IF EXISTS bug24117|
6801 DROP TABLE IF EXISTS t3|
6802 --enable_warnings
6803 CREATE TABLE t3(c1 ENUM('abc'))|
6804 INSERT INTO t3 VALUES('abc')|
6805 CREATE PROCEDURE bug24117()
6806 BEGIN
6807   DECLARE t3c1 ENUM('abc');
6808   DECLARE mycursor CURSOR FOR SELECT c1 FROM t3;
6809   OPEN mycursor;
6810   FLUSH TABLES;
6811   FETCH mycursor INTO t3c1;
6812   CLOSE mycursor;
6813 END|
6814 CALL bug24117()|
6815 DROP PROCEDURE bug24117|
6816 DROP TABLE t3|
6819 # Bug#8407(Stored functions/triggers ignore exception handler)
6822 --disable_warnings
6823 drop function if exists func_8407_a|
6824 drop function if exists func_8407_b|
6825 --enable_warnings
6827 create function func_8407_a() returns int
6828 begin
6829   declare x int;
6831   declare continue handler for sqlexception
6832   begin
6833   end;
6835   select 1 from no_such_view limit 1 into x;
6837   return x;
6838 end|
6840 create function func_8407_b() returns int
6841 begin
6842   declare x int default 0;
6844   declare continue handler for sqlstate '42S02'
6845   begin
6846     set x:= x+1000;
6847   end;
6849   case (select 1 from no_such_view limit 1)
6850     when 1 then set x:= x+1;
6851     when 2 then set x:= x+2;
6852     else set x:= x+100;
6853   end case;
6854   set x:=x + 500;
6855   
6856   return x;
6857 end|
6859 select func_8407_a()|
6860 select func_8407_b()|
6862 drop function func_8407_a|
6863 drop function func_8407_b|
6866 # Bug#26503 (Illegal SQL exception handler code causes the server to crash)
6869 --disable_warnings
6870 drop table if exists table_26503|
6871 drop procedure if exists proc_26503_ok_1|
6872 drop procedure if exists proc_26503_ok_2|
6873 drop procedure if exists proc_26503_ok_3|
6874 drop procedure if exists proc_26503_ok_4|
6875 --enable_warnings
6877 create table table_26503(a int unique)|
6879 create procedure proc_26503_ok_1(v int)
6880 begin
6881   declare i int default 5;
6883   declare continue handler for sqlexception
6884   begin
6885     select 'caught something';
6886     retry:
6887     while i > 0 do
6888       begin
6889         set i = i - 1;
6890         select 'looping', i;
6891         iterate retry;
6892         select 'dead code';
6893       end;
6894     end while retry;
6895     select 'leaving handler';
6896   end;
6898   select 'do something';
6899   insert into table_26503 values (v);
6900   select 'do something again';
6901   insert into table_26503 values (v);
6902 end|
6904 create procedure proc_26503_ok_2(v int)
6905 begin
6906   declare i int default 5;
6908   declare continue handler for sqlexception
6909   begin
6910     select 'caught something';
6911     retry:
6912     while i > 0 do
6913       begin
6914         set i = i - 1;
6915         select 'looping', i;
6916         leave retry;
6917         select 'dead code';
6918       end;
6919     end while;
6920     select 'leaving handler';
6921   end;
6923   select 'do something';
6924   insert into table_26503 values (v);
6925   select 'do something again';
6926   insert into table_26503 values (v);
6927 end|
6929 ## The outer retry label should not prevent using the inner label.
6931 create procedure proc_26503_ok_3(v int)
6932 begin
6933   declare i int default 5;
6935 retry:
6936   begin
6937     declare continue handler for sqlexception
6938     begin
6939       select 'caught something';
6940       retry:
6941       while i > 0 do
6942         begin
6943           set i = i - 1;
6944           select 'looping', i;
6945           iterate retry;
6946           select 'dead code';
6947         end;
6948       end while retry;
6949       select 'leaving handler';
6950     end;
6952     select 'do something';
6953     insert into table_26503 values (v);
6954     select 'do something again';
6955     insert into table_26503 values (v);
6956   end;
6957 end|
6959 ## The outer retry label should not prevent using the inner label.
6961 create procedure proc_26503_ok_4(v int)
6962 begin
6963   declare i int default 5;
6965 retry:
6966   begin
6967     declare continue handler for sqlexception
6968     begin
6969       select 'caught something';
6970       retry:
6971       while i > 0 do
6972         begin
6973           set i = i - 1;
6974           select 'looping', i;
6975           leave retry;
6976           select 'dead code';
6977         end;
6978       end while;
6979       select 'leaving handler';
6980     end;
6982     select 'do something';
6983     insert into table_26503 values (v);
6984     select 'do something again';
6985     insert into table_26503 values (v);
6986   end;
6987 end|
6989 call proc_26503_ok_1(1)|
6990 call proc_26503_ok_2(2)|
6991 call proc_26503_ok_3(3)|
6992 call proc_26503_ok_4(4)|
6994 drop table table_26503|
6995 drop procedure proc_26503_ok_1|
6996 drop procedure proc_26503_ok_2|
6997 drop procedure proc_26503_ok_3|
6998 drop procedure proc_26503_ok_4|
7001 # Bug#25373: Stored functions wasn't compared correctly which leads to a wrong
7002 #            result.
7004 --disable_warnings
7005 DROP FUNCTION IF EXISTS bug25373|
7006 --disable_warnings
7007 CREATE FUNCTION bug25373(p1 INTEGER) RETURNS INTEGER
7008 LANGUAGE SQL DETERMINISTIC
7009 RETURN p1;|
7010 CREATE TABLE t3 (f1 INT, f2 FLOAT)|
7011 INSERT INTO t3 VALUES (1, 3.4), (1, 2), (1, 0.9), (2, 8), (2, 7)|
7012 SELECT SUM(f2), bug25373(f1) FROM t3 GROUP BY bug25373(f1) WITH ROLLUP|
7013 DROP FUNCTION bug25373|
7014 DROP TABLE t3|
7018 # BUG#25082: Default database change on trigger execution breaks replication.
7020 # As it turned out, this bug has actually two bugs. So, here we have two test
7021 # cases -- one in sp.test, the other in sp-security.test.
7025 # Test case 1: error on dropping the current database.
7028 # Prepare.
7030 --disable_warnings
7031 DROP DATABASE IF EXISTS mysqltest1|
7032 DROP DATABASE IF EXISTS mysqltest2|
7033 --enable_warnings
7035 CREATE DATABASE mysqltest1|
7036 CREATE DATABASE mysqltest2|
7038 # Test.
7040 CREATE PROCEDURE mysqltest1.p1()
7041   DROP DATABASE mysqltest2|
7043 use mysqltest2|
7045 CALL mysqltest1.p1()|
7047 SELECT DATABASE()|
7049 # Cleanup.
7051 DROP DATABASE mysqltest1|
7053 use test|
7057 # Bug#20777: Function w BIGINT UNSIGNED shows diff. behaviour --ps-protocol
7058 --disable_warnings
7059 drop function if exists bug20777|
7060 drop table if exists examplebug20777|
7061 --enable_warnings
7062 create function bug20777(f1 bigint unsigned) returns bigint unsigned
7063 begin
7064   set f1 = (f1 - 10); set f1 = (f1 + 10);
7065 return f1;
7066 end|
7067 delimiter ;|
7068 select bug20777(9223372036854775803) as '9223372036854775803   2**63-5';
7069 select bug20777(9223372036854775804) as '9223372036854775804   2**63-4';
7070 select bug20777(9223372036854775805) as '9223372036854775805   2**63-3';
7071 select bug20777(9223372036854775806) as '9223372036854775806   2**63-2';
7072 select bug20777(9223372036854775807) as '9223372036854775807   2**63-1';
7073 select bug20777(9223372036854775808) as '9223372036854775808   2**63+0';
7074 select bug20777(9223372036854775809) as '9223372036854775809   2**63+1';
7075 select bug20777(9223372036854775810) as '9223372036854775810   2**63+2';
7076 select bug20777(-9223372036854775808) as 'lower bounds signed bigint';
7077 select bug20777(9223372036854775807) as 'upper bounds signed bigint';
7078 select bug20777(0) as 'lower bounds unsigned bigint';
7079 select bug20777(18446744073709551615) as 'upper bounds unsigned bigint';
7080 select bug20777(18446744073709551616) as 'upper bounds unsigned bigint + 1';
7081 select bug20777(-1) as 'lower bounds unsigned bigint - 1';
7083 create table examplebug20777 as select 
7084   0 as 'i',
7085   bug20777(9223372036854775806) as '2**63-2',
7086   bug20777(9223372036854775807) as '2**63-1',
7087   bug20777(9223372036854775808) as '2**63',
7088   bug20777(9223372036854775809) as '2**63+1',
7089   bug20777(18446744073709551614) as '2**64-2',
7090   bug20777(18446744073709551615) as '2**64-1', 
7091   bug20777(18446744073709551616) as '2**64',
7092   bug20777(0) as '0',
7093   bug20777(-1) as '-1';
7094 insert into examplebug20777 values (1, 9223372036854775806, 9223372036854775807, 223372036854775808, 9223372036854775809, 18446744073709551614, 18446744073709551615, 8446744073709551616, 0, -1);
7095 show create table examplebug20777;
7096 select * from examplebug20777 order by i;
7098 drop table examplebug20777;
7099 select bug20777(18446744073709551613)+1;
7100 drop function bug20777;
7101 delimiter |;
7105 # BUG#5274: Stored procedure crash if length of CHAR variable too great.
7108 # Prepare.
7110 --disable_warnings
7111 DROP FUNCTION IF EXISTS bug5274_f1|
7112 DROP FUNCTION IF EXISTS bug5274_f2|
7113 --enable_warnings
7115 # Test.
7117 CREATE FUNCTION bug5274_f1(p1 CHAR) RETURNS CHAR
7118   RETURN CONCAT(p1, p1)|
7120 CREATE FUNCTION bug5274_f2() RETURNS CHAR
7121 BEGIN
7122   DECLARE v1 INT DEFAULT 0;
7123   DECLARE v2 CHAR DEFAULT 'x';
7125   WHILE v1 < 30 DO
7126     SET v1 = v1 + 1;
7127     SET v2 = bug5274_f1(v2);
7128   END WHILE;
7130   RETURN v2;
7131 END|
7133 SELECT bug5274_f2()|
7135 # Cleanup.
7137 DROP FUNCTION bug5274_f1|
7138 DROP FUNCTION bug5274_f2|
7141 # Bug#21513 (SP having body starting with quoted label rendered unusable)
7143 --disable_warnings
7144 drop procedure if exists proc_21513|
7145 --enable_warnings
7147 create procedure proc_21513()`my_label`:BEGIN END|
7148 show create procedure proc_21513|
7150 drop procedure proc_21513|
7153 --echo End of 5.0 tests.
7156 # BUG#NNNN: New bug synopsis
7158 #--disable_warnings
7159 #drop procedure if exists bugNNNN|
7160 #--enable_warnings
7161 #create procedure bugNNNN...
7163 # Add bugs above this line. Use existing tables t1 and t2 when
7164 # practical, or create table t3,t4 etc temporarily (and drop them).
7165 # NOTE: The delimiter is `|`, and not `;`. It is changed to `;`
7166 #       at the end of the file!
7169 delimiter ;|
7170 drop table t1,t2;
7172 # Disable warnings to allow test run without InnoDB
7173 --disable_warnings
7174 CREATE TABLE t1 (a int auto_increment primary key) engine=MyISAM;
7175 CREATE TABLE t2 (a int auto_increment primary key, b int) engine=innodb;
7176 --enable_warnings
7177 set @a=0;
7179 delimiter |;
7180 CREATE function bug27354() RETURNS int not deterministic
7181 begin
7182 insert into t1 values (null);
7183 set @a=@a+1;
7184 return @a;
7185 end|
7187 delimiter ;|
7188 update t2 set b=1 where a=bug27354();
7189 select count(t_1.a),count(t_2.a) from t1 as t_1, t2 as t_2 /* must be 0,0 */;
7190 insert into t2 values (1,1),(2,2),(3,3);
7191 update t2 set b=-b where a=bug27354();
7192 select * from t2 /* must return 1,-1 ... */;
7193 select count(*) from t1 /* must be 3 */;
7196 drop table t1,t2;
7197 drop function   bug27354;
7200 # Bug #28605: SHOW CREATE VIEW with views using stored_procedures no longer
7201 # showing SP names.
7203 CREATE TABLE t1 (a INT); 
7204 INSERT INTO t1 VALUES (1),(2);
7206 CREATE FUNCTION metered(a INT) RETURNS INT RETURN 12;
7208 CREATE VIEW v1 AS SELECT test.metered(a) as metered FROM t1;
7210 SHOW CREATE VIEW v1;
7212 DROP VIEW v1;
7213 DROP FUNCTION metered;
7214 DROP TABLE t1;
7217 # Bug#29834: Accessing a view column by name in SP/PS causes a memory leak.
7219 # This is leak test. Run with large number assigned to $execute_cnt,
7220 # $p1_cnt, $p2_cnt, @p1_p2_cnt, $f1_normal_cnt or $f1_prep_cnt variables.
7223 let $execute_cnt= 2;
7224 let $p1_cnt= 2;
7225 let $p2_cnt= 2;
7226 SET @p1_p2_cnt= 2;
7227 let $f1_normal_cnt= 2;
7228 let $f1_prep_cnt= 2;
7230 CREATE TABLE t1 (c1 INT);
7231 CREATE VIEW v1 AS SELECT * FROM t1;
7233 PREPARE s1 FROM 'SELECT c1 FROM v1';
7234 while ($execute_cnt)
7236   EXECUTE s1;
7237   dec $execute_cnt;
7240 DELIMITER |;
7242 CREATE PROCEDURE p1(IN loops BIGINT(19) UNSIGNED)
7243 BEGIN
7244   WHILE loops > 0 DO
7245     SELECT c1 FROM v1;
7246     SET loops = loops - 1;
7247   END WHILE;
7248 END|
7250 CREATE PROCEDURE p2(IN loops BIGINT(19) UNSIGNED)
7251 BEGIN
7252   WHILE loops > 0 DO
7253     SELECT c1 FROM v1;
7254     CALL p1(@p1_p2_cnt);
7255     SET loops = loops - 1;
7256   END WHILE;
7257 END|
7259 CREATE FUNCTION f1(loops INT UNSIGNED)
7260   RETURNS INT
7261 BEGIN
7262   DECLARE tmp INT;
7263   WHILE loops > 0 DO
7264     SELECT c1 INTO tmp FROM v1;
7265     SET loops = loops - 1;
7266   END WHILE;
7267   RETURN loops;
7268 END|
7270 DELIMITER ;|
7272 eval CALL p1($p1_cnt);
7273 eval CALL p2($p2_cnt);
7275 eval SELECT f1($f1_normal_cnt);
7277 eval PREPARE s1 FROM 'SELECT f1($f1_prep_cnt)';
7278 EXECUTE s1;
7279 EXECUTE s1;
7281 DROP PROCEDURE p1;
7282 DROP PROCEDURE p2;
7283 DROP FUNCTION f1;
7284 DROP VIEW v1;
7285 DROP TABLE t1;
7288 # Bug#28551 "The warning 'No database selected' is reported when calling
7289 # stored procedures"
7291 --disable_warnings
7292 drop database if exists mysqltest_db1;
7293 --enable_warnings
7294 create database mysqltest_db1;
7295 create procedure mysqltest_db1.sp_bug28551() begin end;
7296 call mysqltest_db1.sp_bug28551();
7297 show warnings;
7298 drop database mysqltest_db1;
7300 # Bug#29050 Creation of a legal stored procedure fails if a database is not
7301 # selected prior
7303 --disable_warnings
7304 drop database if exists mysqltest_db1;
7305 drop table if exists test.t1;
7306 --enable_warnings
7307 create database mysqltest_db1;
7308 use mysqltest_db1;
7309 # For the sake of its side effect
7310 drop database mysqltest_db1; 
7311 # Now we have no current database selected.
7312 create table test.t1 (id int);
7313 insert into test.t1 (id) values (1);
7314 delimiter //;
7315 create procedure test.sp_bug29050() begin select * from t1; end//
7316 delimiter ;//
7317 show warnings;
7318 call test.sp_bug29050();
7319 show warnings;
7320 # Restore the old current database
7321 use test;
7322 drop procedure sp_bug29050;
7323 drop table t1;
7326 # Bug #30120 SP with local variables with non-ASCII names crashes server.
7329 SET NAMES latin1;
7331 DELIMITER |;
7333 CREATE PROCEDURE p1()
7334 BEGIN
7335   DECLARE áâä INT;
7336   SELECT áâä;
7337 END|
7339 DELIMITER ;|
7341 CALL p1();
7343 SET NAMES default;
7344 DROP PROCEDURE p1;
7347 # Bug#25411 (trigger code truncated)
7350 --disable_warnings
7351 drop procedure if exists proc_25411_a;
7352 drop procedure if exists proc_25411_b;
7353 drop procedure if exists proc_25411_c;
7354 --enable_warnings
7356 delimiter $$;
7358 create procedure proc_25411_a()
7359 begin
7360   /* real comment */
7361   select 1;
7362   /*! select 2; */
7363   select 3;
7364   /*!00000 select 4; */
7365   /*!99999 select 5; */
7369 create procedure proc_25411_b(
7370 /* real comment */
7371 /*! p1 int, */
7372 /*!00000 p2 int */
7373 /*!99999 ,p3 int */
7375 begin
7376   select p1, p2;
7380 create procedure proc_25411_c()
7381 begin
7382   select 1/*!,2*//*!00000,3*//*!99999,4*/;
7383   select 1/*! ,2*//*!00000 ,3*//*!99999 ,4*/;
7384   select 1/*!,2 *//*!00000,3 *//*!99999,4 */;
7385   select 1/*! ,2 *//*!00000 ,3 *//*!99999 ,4 */;
7386   select 1 /*!,2*/ /*!00000,3*/ /*!99999,4*/ ;
7390 delimiter ;$$
7392 show create procedure proc_25411_a;
7393 call proc_25411_a();
7395 show create procedure proc_25411_b;
7396 select name, param_list, body from mysql.proc where name like "%25411%";
7397 call proc_25411_b(10, 20);
7399 show create procedure proc_25411_c;
7400 call proc_25411_c();
7402 drop procedure proc_25411_a;
7403 drop procedure proc_25411_b;
7404 drop procedure proc_25411_c;
7408 # Bug#26302 (MySQL server cuts off trailing "*/" from comments in SP/func)
7411 --disable_warnings
7412 drop procedure if exists proc_26302;
7413 --enable_warnings
7415 create procedure proc_26302()
7416 select 1 /* testing */;
7418 show create procedure proc_26302;
7420 select ROUTINE_NAME, ROUTINE_DEFINITION from information_schema.ROUTINES
7421 where ROUTINE_NAME = "proc_26302";
7423 drop procedure proc_26302;
7426 # Bug #29338: no optimization for stored functions with a trivial body
7427 # always returning constant.
7430 CREATE FUNCTION f1() RETURNS INT DETERMINISTIC RETURN 2;
7431 CREATE FUNCTION f2(I INT) RETURNS INT DETERMINISTIC RETURN 3;
7433 CREATE TABLE t1 (c1 INT, INDEX(c1));
7435 INSERT INTO t1 VALUES (1), (2), (3), (4), (5);
7437 CREATE VIEW v1 AS SELECT c1 FROM t1;
7439 EXPLAIN SELECT * FROM t1 WHERE c1=1;
7440 EXPLAIN SELECT * FROM t1 WHERE c1=f1();
7442 EXPLAIN SELECT * FROM v1 WHERE c1=1;
7443 EXPLAIN SELECT * FROM v1 WHERE c1=f1();
7445 EXPLAIN SELECT * FROM t1 WHERE c1=f2(10);
7446 EXPLAIN SELECT * FROM t1 WHERE c1=f2(c1);
7447 EXPLAIN SELECT * FROM t1 WHERE c1=f2(rand());
7450 DROP VIEW v1;
7451 DROP FUNCTION f1;
7452 DROP FUNCTION f2;
7453 DROP TABLE t1;
7456 # Bug#29408 Cannot find view in columns table if the selection contains a function
7458 delimiter |;
7460 create function f1()
7461     returns int(11)
7462 not deterministic
7463 contains sql
7464 sql security definer
7465 comment ''
7466 begin
7467   declare x int(11);
7468   set x=-1;
7469    return x;
7470 end|
7471                           
7472 delimiter ;|
7473                   
7474 create view v1 as select 1 as one, f1() as days;
7475                           
7476 connect (bug29408, localhost, root,,*NO-ONE*);                     
7477 connection bug29408;
7479 show create view test.v1;      
7480 select column_name from information_schema.columns
7481 where table_name='v1' and table_schema='test';
7483 connection default;
7484 disconnect bug29408;
7485 drop view v1;
7486 drop function f1;
7489 # Bug#13675: DATETIME/DATE type in store proc param seems to be converted as
7490 # varbinary
7493 --echo
7494 --echo # Bug#13675.
7495 --echo
7497 --disable_warnings
7498 DROP PROCEDURE IF EXISTS p1;
7499 DROP PROCEDURE IF EXISTS p2;
7501 DROP TABLE IF EXISTS t1;
7502 --enable_warnings
7504 --echo
7506 CREATE PROCEDURE p1(v DATETIME) CREATE TABLE t1 SELECT v;
7508 CREATE PROCEDURE p2(v INT) CREATE TABLE t1 SELECT v;
7510 --echo
7511 CALL p1(NOW());
7512 SHOW CREATE TABLE t1;
7514 --echo
7515 DROP TABLE t1;
7517 --echo
7518 CALL p1('text');
7519 SHOW CREATE TABLE t1;
7521 --echo
7522 DROP TABLE t1;
7524 --echo
7525 CALL p2(10);
7526 SHOW CREATE TABLE t1;
7528 --echo
7529 DROP TABLE t1;
7531 --echo
7532 CALL p2('text');
7533 SHOW CREATE TABLE t1;
7535 --echo
7536 DROP TABLE t1;
7538 --echo
7539 DROP PROCEDURE p1;
7540 DROP PROCEDURE p2;
7542 ###########################################################################
7545 # Bug#31035: select from function, group by result crasher.
7548 ###########################################################################
7550 --echo
7552 --echo #
7553 --echo # Bug#31035.
7554 --echo #
7556 --echo
7558 --echo #
7559 --echo # - Prepare.
7560 --echo #
7562 --echo
7564 --disable_warnings
7565 DROP TABLE IF EXISTS t1;
7566 DROP FUNCTION IF EXISTS f1;
7567 DROP FUNCTION IF EXISTS f2;
7568 DROP FUNCTION IF EXISTS f3;
7569 DROP FUNCTION IF EXISTS f4;
7570 --enable_warnings
7572 --echo
7574 --echo #
7575 --echo # - Create required objects.
7576 --echo #
7578 --echo
7580 CREATE TABLE t1(c1 INT);
7582 --echo
7584 INSERT INTO t1 VALUES (1), (2), (3);
7586 --echo
7588 CREATE FUNCTION f1()
7589   RETURNS INT
7590   NOT DETERMINISTIC
7591     RETURN 1;
7593 --echo
7595 CREATE FUNCTION f2(p INT)
7596   RETURNS INT
7597   NOT DETERMINISTIC
7598     RETURN 1;
7600 --echo
7602 CREATE FUNCTION f3()
7603   RETURNS INT
7604   DETERMINISTIC
7605     RETURN 1;
7607 --echo
7609 CREATE FUNCTION f4(p INT)
7610   RETURNS INT
7611   DETERMINISTIC
7612     RETURN 1;
7614 --echo
7616 --echo #
7617 --echo # - Check.
7618 --echo #
7620 --echo
7622 # Not deterministic function, no arguments.
7624 SELECT f1() AS a FROM t1 GROUP BY a;
7626 --echo
7628 # Not deterministic function, non-constant argument.
7630 SELECT f2(@a) AS a FROM t1 GROUP BY a;
7632 --echo
7634 # Deterministic function, no arguments.
7636 SELECT f3() AS a FROM t1 GROUP BY a;
7638 --echo
7640 # Deterministic function, constant argument.
7642 SELECT f4(0) AS a FROM t1 GROUP BY a;
7644 --echo
7646 # Deterministic function, non-constant argument.
7648 SELECT f4(@a) AS a FROM t1 GROUP BY a;
7650 --echo
7652 --echo #
7653 --echo # - Cleanup.
7654 --echo #
7656 --echo
7658 DROP TABLE t1;
7659 DROP FUNCTION f1;
7660 DROP FUNCTION f2;
7661 DROP FUNCTION f3;
7662 DROP FUNCTION f4;
7664 --echo
7666 ###########################################################################
7669 # Bug#31191: JOIN in combination with stored function crashes the server.
7672 ###########################################################################
7674 --echo #
7675 --echo # Bug#31191.
7676 --echo #
7678 --echo
7680 --echo #
7681 --echo # - Prepare.
7682 --echo #
7684 --echo
7686 --disable_warnings
7687 DROP TABLE IF EXISTS t1;
7688 DROP TABLE IF EXISTS t2;
7689 DROP FUNCTION IF EXISTS f1;
7690 --enable_warnings
7692 --echo
7694 --echo #
7695 --echo # - Create required objects.
7696 --echo #
7698 --echo
7700 CREATE TABLE t1 (
7701    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
7702    barcode INT(8) UNSIGNED ZEROFILL nOT NULL,
7703    PRIMARY KEY  (id),
7704    UNIQUE KEY barcode (barcode)
7707 --echo
7709 INSERT INTO t1 (id, barcode) VALUES (1, 12345678);
7710 INSERT INTO t1 (id, barcode) VALUES (2, 12345679);
7712 --echo
7714 CREATE TABLE test.t2 (
7715    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
7716    barcode BIGINT(11) UNSIGNED ZEROFILL NOT NULL,
7717    PRIMARY KEY  (id)
7720 --echo
7722 INSERT INTO test.t2 (id, barcode) VALUES (1, 12345106708);
7723 INSERT INTO test.t2 (id, barcode) VALUES (2, 12345106709);
7725 --echo
7727 CREATE FUNCTION f1(p INT(8))
7728   RETURNS BIGINT(11) UNSIGNED
7729   READS SQL DATA
7730     RETURN FLOOR(p/1000)*1000000 + 100000 + FLOOR((p MOD 1000)/10)*100 + (p MOD 10);
7732 --echo
7734 --echo #
7735 --echo # - Check.
7736 --echo #
7738 --echo
7740 SELECT DISTINCT t1.barcode, f1(t1.barcode)
7741 FROM t1
7742 INNER JOIN t2
7743 ON f1(t1.barcode) = t2.barcode
7744 WHERE t1.barcode=12345678;
7746 --echo
7748 --echo #
7749 --echo # - Cleanup.
7750 --echo #
7752 --echo
7754 DROP TABLE t1;
7755 DROP TABLE t2;
7756 DROP FUNCTION f1;
7758 --echo
7760 ###########################################################################
7763 # Bug#31226: Group by function crashes mysql.
7766 ###########################################################################
7768 --echo #
7769 --echo # Bug#31226.
7770 --echo #
7772 --echo
7774 --echo #
7775 --echo # - Prepare.
7776 --echo #
7778 --echo
7780 --disable_warnings
7781 DROP TABLE IF EXISTS t1;
7782 DROP FUNCTION IF EXISTS f1;
7783 --enable_warnings
7785 --echo
7787 --echo #
7788 --echo # - Create required objects.
7789 --echo #
7791 --echo
7793 CREATE TABLE t1(id INT);
7795 --echo
7797 INSERT INTO t1 VALUES (1), (2), (3);
7799 --echo
7801 CREATE FUNCTION f1()
7802   RETURNS DATETIME
7803   NOT DETERMINISTIC NO SQL
7804     RETURN NOW();
7806 --echo
7808 --echo #
7809 --echo # - Check.
7810 --echo #
7812 --echo
7814 --replace_column 1 <timestamp>
7815 SELECT f1() FROM t1 GROUP BY 1;
7817 --echo
7819 --echo #
7820 --echo # - Cleanup.
7821 --echo #
7823 --echo
7825 DROP TABLE t1;
7826 DROP FUNCTION f1;
7828 --echo
7830 ###########################################################################
7833 # Bug#28318 (CREATE FUNCTION (UDF) requires a schema)
7836 --disable_warnings
7837 DROP PROCEDURE IF EXISTS db28318_a.t1;
7838 DROP PROCEDURE IF EXISTS db28318_b.t2;
7839 DROP DATABASE IF EXISTS db28318_a;
7840 DROP DATABASE IF EXISTS db28318_b;
7841 --enable_warnings
7843 CREATE DATABASE db28318_a;
7844 CREATE DATABASE db28318_b;
7846 CREATE PROCEDURE db28318_a.t1() SELECT "db28318_a.t1";
7847 CREATE PROCEDURE db28318_b.t2() CALL t1();
7849 use db28318_a;
7851 # In db28318_b.t2, t1 refers to db28318_b.t1
7852 --error ER_SP_DOES_NOT_EXIST
7853 CALL db28318_b.t2();
7855 DROP PROCEDURE db28318_a.t1;
7856 DROP PROCEDURE db28318_b.t2;
7857 DROP DATABASE db28318_a;
7858 DROP DATABASE db28318_b;
7859 use test;
7861 ###########################################################################
7864 # Bug#29770 Two handlers are allowed to catch an error in an stored procedure.
7867 --disable_warnings
7868 DROP TABLE IF EXISTS t1;
7869 DROP PROCEDURE IF EXISTS bug29770;
7870 --enable_warnings
7872 CREATE TABLE t1(a int);
7873 delimiter |;
7874 CREATE PROCEDURE bug29770()
7875 BEGIN
7876   DECLARE CONTINUE HANDLER FOR SQLSTATE '42S22' SET @state:= 'run';
7877   DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @exception:= 'run';
7878   SELECT x FROM t1;
7879 END|
7880 delimiter ;|
7881 CALL bug29770();
7882 SELECT @state, @exception;
7883 DROP TABLE t1;
7884 DROP PROCEDURE bug29770;
7887 # Bug#33618 Crash in sp_rcontext
7890 use test;
7892 --disable_warnings
7893 drop table if exists t_33618;
7894 drop procedure if exists proc_33618;
7895 --enable_warnings
7897 create table t_33618 (`a` int, unique(`a`), `b` varchar(30)) engine=myisam;
7898 insert into t_33618 (`a`,`b`) values (1,'1'),(2,'2');
7900 delimiter //;
7902 create procedure proc_33618(num int)
7903 begin
7904   declare count1 int default '0';
7905   declare vb varchar(30);
7906   declare last_row int;
7908   while(num>=1) do
7909     set num=num-1;
7910     begin
7911       declare cur1 cursor for select `a` from t_33618;
7912       declare continue handler for not found set last_row = 1;
7913       set last_row:=0;
7914       open cur1;
7915       rep1:
7916       repeat
7917         begin
7918           declare exit handler for 1062 begin end;
7919           fetch cur1 into vb;
7920           if (last_row = 1) then
7921             leave rep1;
7922           end if;
7923         end;
7924         until last_row=1
7925       end repeat;
7926       close cur1;
7927     end;
7928   end while;
7929 end//
7931 delimiter ;//
7933 call proc_33618(20);
7935 drop table t_33618;
7936 drop procedure proc_33618;
7938 --echo #
7939 --echo # Bug#30787: Stored function ignores user defined alias.
7940 --echo #
7941 use test;
7942 --disable_warnings
7943 drop function if exists func30787;
7944 --enable_warnings
7945 create table t1(f1 int);
7946 insert into t1 values(1),(2);
7947 delimiter |;
7948 create function func30787(p1 int) returns int
7949 begin
7950   return p1;
7951 end |
7952 delimiter ;|
7953 select (select func30787(f1)) as ttt from t1;
7954 drop function func30787;
7955 drop table t1;
7958 # Bug #33811: Call to stored procedure with SELECT * / RIGHT JOIN fails
7959 # after the first time
7961 CREATE TABLE t1 (id INT);
7962 INSERT INTO t1 VALUES (1),(2),(3),(4);
7964 CREATE PROCEDURE test_sp()
7965   SELECT t1.* FROM t1 RIGHT JOIN t1 t2 ON t1.id=t2.id;
7967 CALL test_sp();
7968 CALL test_sp();
7970 DROP PROCEDURE test_sp;
7971 DROP TABLE t1;
7974 ###########################################################################
7976 # Bug#38291 memory corruption and server crash with view/sp/function
7979 create table t1(c1 INT);
7980 create function f1(p1 int) returns varchar(32)
7981   return 'aaa';
7982 create view v1 as select f1(c1) as parent_control_name from t1;
7984 delimiter //;
7985 create procedure p1()
7986 begin
7987     select parent_control_name as c1 from v1;
7988 end //
7989 delimiter ;//
7991 call p1();
7992 call p1();
7994 drop procedure p1;
7995 drop function f1;
7996 drop view v1;
7997 drop table t1;
8000 # Bug#38469 invalid memory read and/or crash with utf8 text field, stored procedure, uservar 
8002 delimiter $;
8003 --disable_warnings
8004 drop procedure if exists `p2` $
8005 --enable_warnings
8006 create procedure `p2`(in `a` text charset utf8)
8007 begin
8008         declare `pos` int default 1;
8009         declare `str` text charset utf8;
8010         set `str` := `a`;
8011         select substr(`str`, `pos`+ 1 ) into `str`;
8012 end $
8013 delimiter ;$
8014 call `p2`('s s s s s s');
8015 drop procedure `p2`;
8018 # Bug#38823: Invalid memory access when a SP statement does wildcard expansion
8021 --disable_warnings
8022 drop table if exists t1;
8023 drop procedure if exists p1;
8024 --enable_warnings
8026 delimiter $;
8027 create procedure p1() begin select * from t1; end$
8028 --error ER_NO_SUCH_TABLE
8029 call p1$
8030 create table t1 (a integer)$
8031 call p1$
8032 alter table t1 add b integer;
8033 call p1$
8034 delimiter ;$
8036 drop table t1;
8037 drop procedure p1;
8039 --echo # ------------------------------------------------------------------
8040 --echo # -- End of 5.0 tests
8041 --echo # ------------------------------------------------------------------
8043 ###########################################################################
8046 # Bug#20550: Stored function: wrong RETURN type metadata when used in a VIEW.
8049 ###########################################################################
8051 --echo
8053 --echo #
8054 --echo # Bug#20550.
8055 --echo #
8057 --echo
8059 --echo #
8060 --echo # - Prepare.
8061 --echo #
8063 --echo
8065 --disable_warnings
8066 DROP VIEW IF EXISTS v1;
8067 DROP VIEW IF EXISTS v2;
8068 DROP FUNCTION IF EXISTS f1;
8069 DROP FUNCTION IF EXISTS f2;
8070 --enable_warnings
8072 --echo
8074 --echo #
8075 --echo # - Create required objects.
8076 --echo #
8078 --echo
8080 CREATE FUNCTION f1() RETURNS VARCHAR(65525) RETURN 'Hello';
8082 --echo
8084 CREATE FUNCTION f2() RETURNS TINYINT RETURN 1;
8086 --echo
8088 CREATE VIEW v1 AS SELECT f1();
8090 --echo
8092 CREATE VIEW v2 AS SELECT f2();
8094 --echo
8096 --echo #
8097 --echo # - Check.
8098 --echo #
8100 --echo
8102 SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'v1';
8104 --echo
8106 SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'v2';
8108 --echo
8110 --echo #
8111 --echo # - Cleanup.
8112 --echo #
8114 --echo
8116 DROP FUNCTION f1;
8117 DROP FUNCTION f2;
8118 DROP VIEW v1;
8119 DROP VIEW v2;
8121 --echo
8123 ###########################################################################
8126 # Bug#24923: Functions with ENUM issues.
8129 ###########################################################################
8131 --echo #
8132 --echo # - Bug#24923: prepare.
8133 --echo #
8135 --echo
8137 --disable_warnings
8138 DROP FUNCTION IF EXISTS f1;
8139 --enable_warnings
8141 --echo
8143 --echo #
8144 --echo # - Bug#24923: create required objects.
8145 --echo #
8147 --echo
8149 delimiter |;
8151 CREATE FUNCTION f1(p INT)
8152   RETURNS ENUM ('Very_long_enum_element_identifier',
8153                 'Another_very_long_enum_element_identifier')
8154   BEGIN
8155     CASE p
8156     WHEN 1 THEN
8157       RETURN 'Very_long_enum_element_identifier';
8158     ELSE
8159       RETURN 'Another_very_long_enum_element_identifier';
8160     END CASE;
8161   END|
8163 delimiter ;|
8165 --echo
8167 --echo #
8168 --echo # - Bug#24923: check.
8169 --echo #
8171 --echo
8173 SELECT f1(1);
8175 --echo
8177 SELECT f1(2);
8179 --echo
8181 SHOW CREATE FUNCTION f1;
8183 --echo #
8184 --echo # - Bug#24923: cleanup.
8185 --echo #
8187 --echo
8189 DROP FUNCTION f1;
8191 --echo
8193 ###########################################################################
8196 # Bug#32633 Can not create any routine if SQL_MODE=no_engine_substitution
8198 # Ensure that when new SQL modes are introduced, they are also added to
8199 # the mysql.proc table.
8202 --disable_warnings
8203 drop procedure if exists p;
8204 --enable_warnings
8205 set @old_mode= @@sql_mode;
8206 set @@sql_mode= pow(2,32)-1;
8207 select @@sql_mode into @full_mode;
8208 create procedure p() begin end;
8209 call p();
8210 select @@sql_mode;
8211 set @@sql_mode= @old_mode;
8212 # Rename SQL modes that differ in name between the server and the table definition.
8213 select replace(@full_mode, '?', 'NOT_USED') into @full_mode;
8214 select replace(@full_mode, 'ALLOW_INVALID_DATES', 'INVALID_DATES') into @full_mode;
8215 select name from mysql.proc where name = 'p' and sql_mode = @full_mode;
8216 drop procedure p;
8219 # Bug#43962 "Packets out of order" calling a SHOW TABLE STATUS
8221 DELIMITER //;
8222 CREATE DEFINER = 'root'@'localhost' PROCEDURE p1()
8223 NOT DETERMINISTIC
8224 CONTAINS SQL
8225 SQL SECURITY DEFINER
8226 COMMENT ''
8227 BEGIN
8228  SHOW TABLE STATUS like 't1';
8229 END;//
8230 DELIMITER ;//
8233 CREATE TABLE t1 (f1 INT);
8234 --disable_result_log
8235 let $tab_count= 4;
8236 while ($tab_count)
8238  EVAL CALL p1();
8239  dec $tab_count ;
8241 --enable_result_log
8242 DROP PROCEDURE p1;
8243 DROP TABLE t1;
8246 # Bug#47649 crash during CALL procedure
8248 CREATE TABLE t1 ( f1 integer, primary key (f1));
8249 CREATE TABLE t2 LIKE t1;
8250 CREATE TEMPORARY TABLE t3 LIKE t1;
8251 delimiter |;
8252 CREATE PROCEDURE p1 () BEGIN SELECT f1 FROM t3 AS A WHERE A.f1 IN ( SELECT f1 FROM t3 ) ;
8253 END|
8254 delimiter ;|
8255 --error ER_CANT_REOPEN_TABLE
8256 CALL p1;
8257 CREATE VIEW t3 AS SELECT f1 FROM t2 A WHERE A.f1 IN ( SELECT f1 FROM t2 );
8258 DROP TABLE t3;
8259 CALL p1;
8260 CALL p1;
8261 DROP PROCEDURE p1;
8262 DROP TABLE t1, t2;
8263 DROP VIEW t3;
8265 --echo #
8266 --echo # Bug #46629: Item_in_subselect::val_int(): Assertion `0' 
8267 --echo # on subquery inside a SP
8268 --echo #
8269 CREATE TABLE t1(a INT);
8270 CREATE TABLE t2(a INT, b INT PRIMARY KEY);
8272 DELIMITER |;
8273 CREATE PROCEDURE p1 () 
8274 BEGIN 
8275   SELECT a FROM t1 A WHERE A.b IN (SELECT b FROM t2 AS B);
8276 END|
8277 DELIMITER ;|
8278 --error ER_BAD_FIELD_ERROR
8279 CALL p1;
8280 --error ER_BAD_FIELD_ERROR
8281 CALL p1;
8282 DROP PROCEDURE p1;
8283 DROP TABLE t1, t2;
8285 --echo #
8286 --echo # Bug#47627: SET @@{global.session}.local_variable in stored routine causes crash
8287 --echo # Bug#48626: Crash or lost connection using SET for declared variables with @@
8288 --echo #
8290 --disable_warnings
8291 DROP PROCEDURE IF EXISTS p1;
8292 DROP PROCEDURE IF EXISTS p2;
8293 DROP PROCEDURE IF EXISTS p3;
8294 --enable_warnings
8296 delimiter //;
8298 --error ER_UNKNOWN_SYSTEM_VARIABLE
8299 CREATE PROCEDURE p1()
8300 BEGIN
8301   DECLARE v INT DEFAULT 0;
8302   SET @@SESSION.v= 10;
8303 END//
8305 CREATE PROCEDURE p2()
8306 BEGIN
8307   DECLARE v INT DEFAULT 0;
8308   SET v= 10;
8309 END//
8310 call p2()//
8312 --error ER_UNKNOWN_SYSTEM_VARIABLE
8313 CREATE PROCEDURE p3()
8314 BEGIN
8315   DECLARE v INT DEFAULT 0;
8316   SELECT @@SESSION.v;
8317 END//
8319 --error ER_UNKNOWN_SYSTEM_VARIABLE
8320 CREATE PROCEDURE p4()
8321 BEGIN
8322   DECLARE v INT DEFAULT 0;
8323   SET @@GLOBAL.v= 10;
8324 END//
8326 CREATE PROCEDURE p5()
8327 BEGIN
8328   DECLARE init_connect INT DEFAULT 0;
8329   SET init_connect= 10;
8330   SET @@GLOBAL.init_connect= 'SELECT 1';
8331   SET @@SESSION.IDENTITY= 1;
8332   SELECT @@SESSION.IDENTITY;
8333   SELECT @@GLOBAL.init_connect;
8334   SELECT init_connect;
8335 END//
8337 --error ER_UNKNOWN_SYSTEM_VARIABLE
8338 CREATE PROCEDURE p6()
8339 BEGIN
8340   DECLARE v INT DEFAULT 0;
8341   SET @@v= 0;
8342 END//
8344 delimiter ;//
8346 SET @old_init_connect= @@GLOBAL.init_connect;
8347 CALL p5();
8348 SET @@GLOBAL.init_connect= @old_init_connect;
8350 DROP PROCEDURE p2;
8351 DROP PROCEDURE p5;
8354 --echo #
8355 --echo # Bug#11840395 (formerly known as bug#60347):
8356 --echo # The string "versiondata" seems
8357 --echo # to be 'leaking' into the schema name space
8358 --echo #
8359 --disable_warnings
8360 DROP DATABASE IF EXISTS mixedCaseDbName;
8361 --enable_warnings
8362 CREATE DATABASE mixedCaseDbName;
8363 DELIMITER |;
8364 CREATE PROCEDURE mixedCaseDbName.tryMyProc() begin end|
8365 CREATE FUNCTION mixedCaseDbName.tryMyFunc() returns text begin return 'IT WORKS'; end
8367 DELIMITER ;|
8368 call mixedCaseDbName.tryMyProc();
8369 select mixedCaseDbName.tryMyFunc();
8370 DROP DATABASE mixedCaseDbName;
8373 --echo #
8374 --echo # Bug#11766594  59736: SELECT DISTINCT.. INCORRECT RESULT WITH DETERMINISTIC FUNCTION IN WHERE C
8375 --echo #
8377 CREATE TABLE t1 (a INT, b INT, KEY(b));
8378 CREATE TABLE t2 (c INT, d INT, KEY(c));
8379 INSERT INTO t1 VALUES (1,1),(1,1),(1,2);
8380 INSERT INTO t2 VALUES (1,1),(1,2);
8382 DELIMITER $;
8384 CREATE FUNCTION f1() RETURNS INT DETERMINISTIC
8385 BEGIN
8386   DECLARE a int;
8387   -- SQL statement inside
8388   SELECT 1 INTO a;
8389   RETURN a;
8390 END $
8392 DELIMITER ;$
8394 SELECT COUNT(DISTINCT d) FROM t1, t2  WHERE a = c AND b = f1();
8396 DROP FUNCTION f1;
8397 DROP TABLE t1, t2;
8400 --echo #
8401 --echo # Bug#12663165 SP DEAD CODE REMOVAL DOESN'T UNDERSTAND CONTINUE HANDLERS
8402 --echo #
8404 --disable_warnings
8405 DROP FUNCTION IF EXISTS f1;
8406 --enable_warnings
8408 delimiter $;
8409 CREATE FUNCTION f1() RETURNS INT
8410 BEGIN
8411   DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
8412   BEGIN
8413     DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RETURN f1();
8414     BEGIN
8415      DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RETURN f1();
8416      RETURN f1();
8417     END;
8418   END;
8419 RETURN 1;
8420 END $ 
8421 delimiter ;$
8423 # This used to cause an assertion.
8424 SELECT f1();
8426 DROP FUNCTION f1;
8429 --echo # ------------------------------------------------------------------
8430 --echo # -- End of 5.1 tests
8431 --echo # ------------------------------------------------------------------
8433 --echo
8434 --echo # Bug#13805127: Stored program cache produces wrong result in same THD
8435 --echo
8437 delimiter |;
8439 CREATE PROCEDURE p1(x INT UNSIGNED)
8440 BEGIN
8441   SELECT c1, t2.c2, count(c3)
8442   FROM
8443     (
8444     SELECT 3 as c2 FROM dual WHERE x = 1
8445     UNION
8446     SELECT 2       FROM dual WHERE x = 1 OR x = 2
8447     ) AS t1,
8448     (
8449     SELECT '2012-03-01 01:00:00' AS c1, 3 as c2, 1 as c3 FROM dual
8450     UNION
8451     SELECT '2012-03-01 02:00:00',       3,       2       FROM dual
8452     UNION
8453     SELECT '2012-03-01 01:00:00',       2,       1       FROM dual
8454     ) AS t2
8455   WHERE t2.c2 = t1.c2
8456   GROUP BY c1, c2
8457   ;
8458 END|
8460 delimiter ;|
8462 --echo
8463 CALL p1(1);
8464 CALL p1(2);
8465 CALL p1(1);
8467 DROP PROCEDURE p1;