summaryrefslogtreecommitdiff
path: root/mysql-test/main/sp-for-loop.test
blob: 6350e9fb9d33b31abb9bdadc4914964cde0d21cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
--echo #
--echo # MDEV-14415 Add Oracle-style FOR loop to sql_mode=DEFAULT
--echo #


CREATE TABLE t1 (a INT);
DELIMITER /;
FOR i IN 1..3
DO
  INSERT INTO t1 VALUES (i);
END FOR;
/
DELIMITER ;/
SELECT * FROM t1;
DROP TABLE t1;


# Dots must have no delimiters in between

DELIMITER /;
--error ER_PARSE_ERROR
CREATE FUNCTION f1 (lower_bound INT, upper_bound INT, lim INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  FOR i IN lower_bound . . upper_bound
  DO
    NULL
  END FOR;
  RETURN total;
END;
/
DELIMITER ;/


DELIMITER /;
CREATE FUNCTION f1 (lower_bound INT, upper_bound INT, lim INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  lab:
  FOR i IN lower_bound .. upper_bound
  DO
    SET total= total + i;
    IF i = lim THEN
      LEAVE lab;
    END IF;
     -- Bounds are calculated only once.
     -- The below assignments have no effect on the loop condition
    SET lower_bound= 900;
    SET upper_bound= 1000;
  END FOR;
  RETURN total;
END;
/
DELIMITER ;/
SELECT f1(1, 3, 100) FROM DUAL;
SELECT f1(1, 3, 2) FROM DUAL;
DROP FUNCTION f1;


DELIMITER /;
CREATE FUNCTION f1() RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  FOR i IN 1 .. 5
  DO
    SET total= total + 1000;
    forj:
    FOR j IN 1 .. 5
    DO
      SET total= total + 1;
      IF j = 3 THEN
        LEAVE forj; -- End the internal loop
      END IF;
    END FOR;
  END FOR;
  RETURN total;
END;
/
DELIMITER ;/
SELECT f1() FROM DUAL;
DROP FUNCTION f1;


DELIMITER /;
CREATE FUNCTION f1 (a INT, b INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  fori:
  FOR i IN REVERSE a..1
  DO
    SET total= total + i;
    IF i = b THEN
      LEAVE fori;
    END IF;
  END FOR;
  RETURN total;
END
/
DELIMITER ;/
SELECT f1(3, 100) FROM DUAL;
SELECT f1(3, 2) FROM DUAL;
DROP FUNCTION f1;


--echo # Testing labeled FOR LOOP statement

DELIMITER /;
CREATE FUNCTION f1 (a INT, limita INT, b INT, limitb INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  la:
  FOR ia IN 1 .. a
  DO
    SET total= total + 1000;
    lb:
    FOR ib IN 1 .. b
    DO
      SET total= total + 1;
      IF ib = limitb THEN
        LEAVE lb;
      END IF;
      IF ia = limita THEN
        LEAVE la;
      END IF;
    END FOR lb;
  END FOR la;
  RETURN total;
END;
/
DELIMITER ;/
SELECT f1(1, 1, 1, 1) FROM DUAL;
SELECT f1(1, 2, 1, 2) FROM DUAL;
SELECT f1(2, 1, 2, 1) FROM DUAL;
SELECT f1(2, 1, 2, 2) FROM DUAL;
SELECT f1(2, 2, 2, 2) FROM DUAL;
SELECT f1(2, 3, 2, 3) FROM DUAL;
DROP FUNCTION f1;


--echo # Testing labeled ITERATE in a labeled FOR LOOP statement

DELIMITER /;
CREATE FUNCTION f1 (a INT, b INT, blim INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  la:
  FOR ia IN 1 .. a
  DO
    SET total= total + 1000;
    BEGIN
      DECLARE ib INT DEFAULT 1;
      WHILE ib <= b
      DO
        IF ib > blim THEN
          ITERATE la;
        END IF;
        SET ib= ib + 1;
        SET total= total + 1;
      END WHILE;
    END;
  END FOR la;
  RETURN total;
END;
/
DELIMITER ;/
SELECT f1(3,3,0), f1(3,3,1), f1(3,3,2), f1(3,3,3), f1(3,3,4) FROM DUAL;
DROP FUNCTION f1;


--echo # Testing INTERATE statement

DELIMITER /;
CREATE FUNCTION f1(a INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  fori:
  FOR i IN 1 .. a
  DO
    IF i=5 THEN
      ITERATE fori;
    END IF;
    SET total= total + 1;
  END FOR;
  RETURN total;
END;
/
DELIMITER ;/
SELECT f1(3), f1(4), f1(5), f1(6) FROM DUAL;
DROP FUNCTION f1;


DELIMITER /;
CREATE FUNCTION f1(a INT) RETURNS INT
BEGIN
  DECLARE total INT DEFAULT 0;
  lj:
  FOR j IN 1 .. 2
  DO
    FOR i IN 1 .. a
    DO
      IF i=5 THEN
        ITERATE lj;
      END IF;
      SET total= total + 1;
    END FOR;
  END FOR;
  RETURN total;
END;
/
DELIMITER ;/
SELECT f1(3), f1(4), f1(5) FROM DUAL;
DROP FUNCTION f1;