summaryrefslogtreecommitdiff
path: root/mysql-test/suite/compat/oracle/r/sp.result
blob: f0a7d2808f1d47e0b199b338da841554e2bb33b1 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
SET sql_mode=ORACLE;
# Testing routines with no parameters
CREATE FUNCTION f1 RETURN INT
AS
BEGIN
RETURN 10;
END;
/
SHOW CREATE FUNCTION f1;
Function	f1
sql_mode	PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,SIMULTANEOUS_ASSIGNMENT
Create Function	CREATE DEFINER="root"@"localhost" FUNCTION "f1"() RETURN int(11)
AS
BEGIN
RETURN 10;
END
character_set_client	latin1
collation_connection	latin1_swedish_ci
Database Collation	latin1_swedish_ci
SELECT f1();
f1()
10
DROP FUNCTION f1;
CREATE PROCEDURE p1
AS
BEGIN
SET @a=10;
END;
/
SHOW CREATE PROCEDURE p1;
Procedure	p1
sql_mode	PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ORACLE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,SIMULTANEOUS_ASSIGNMENT
Create Procedure	CREATE DEFINER="root"@"localhost" PROCEDURE "p1"()
AS
BEGIN
SET @a=10;
END
character_set_client	latin1
collation_connection	latin1_swedish_ci
Database Collation	latin1_swedish_ci
SET @a=0;
CALL p1();
SELECT @a;
@a
10
DROP PROCEDURE p1;
# Testing ":=" to set the default value of a variable
CREATE FUNCTION f1 () RETURN NUMBER(10) AS
a NUMBER(10) := 10;
BEGIN
DECLARE
b NUMBER(10) DEFAULT 3;
BEGIN
RETURN a+b;
END;
END;
/
SELECT f1();
f1()
13
DROP FUNCTION f1;
# Testing labels
CREATE FUNCTION f1 (a INT) RETURN CLOB AS
BEGIN
<<label1>>
BEGIN
IF a = 1 THEN
LEAVE label1;
END IF;
RETURN 'IS NOT 1';
END label1;
RETURN 'IS 1';
END;
/
SELECT f1(1);
f1(1)
IS 1
SELECT f1(2);
f1(2)
IS NOT 1
DROP FUNCTION f1;
CREATE FUNCTION f1 (a INT) RETURN INT IS
BEGIN
<<label1>>
LOOP
IF a = 2 THEN
LEAVE label1;
END IF;
SET a= a-1;
END LOOP;
RETURN a;
END;
/
SELECT f1(4);
f1(4)
2
DROP FUNCTION f1;
CREATE FUNCTION f1 (a INT) RETURN INT AS
BEGIN
<<label1>>
WHILE a>0 LOOP
IF a = 2 THEN
LEAVE label1;
END IF;
SET a= a-1;
END LOOP label1;
RETURN a;
END;
/
SELECT f1(4);
f1(4)
2
DROP FUNCTION f1;
CREATE FUNCTION f1 (a INT) RETURN INT AS
BEGIN
<<label1>>
REPEAT
IF a = 2 THEN
LEAVE label1;
END IF;
SET a= a-1;
UNTIL a=0 END REPEAT;
RETURN a;
END;
/
SELECT f1(4);
f1(4)
2
DROP FUNCTION f1;
# Testing IN/OUT/INOUT
CREATE PROCEDURE p1 (p1 IN VARCHAR2(10), p2 OUT VARCHAR2(10)) AS
BEGIN
SET p1='p1new';
SET p2='p2new';
END;
/
SET @p1='p1', @p2='p2';
CALL p1(@p1, @p2);
SELECT @p1, @p2;
@p1	@p2
p1	p2new
DROP PROCEDURE p1;
# Testing Oracle-style assigment
CREATE PROCEDURE p1 (p1 OUT VARCHAR2(10)) AS
BEGIN
p1:= 'p1new';
END;
/
SET @p1='p1';
CALL p1(@p1);
SELECT @p1;
@p1
p1new
DROP PROCEDURE p1;
# Testing that NULL is a valid statement
CREATE PROCEDURE p1(a INT) AS
BEGIN
NULL;
END;
/
DROP PROCEDURE p1;
CREATE PROCEDURE p1(a INT) AS
a INT:=10;
BEGIN
IF a=10 THEN NULL; ELSE NULL; END IF;
END;
/
DROP PROCEDURE p1;
# Testing that (some) keyword_sp are allowed in Oracle-style assignments
CREATE PROCEDURE p1 (action OUT INT) AS BEGIN action:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (clob OUT INT) AS BEGIN clob:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (enum OUT INT) AS BEGIN enum:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (via OUT INT) AS BEGIN via:=10; END;/
DROP PROCEDURE p1/
# Testing keyword_directly_assignable
CREATE PROCEDURE p1 (ascii OUT INT) AS BEGIN ascii:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (backup OUT INT) AS BEGIN backup:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (binlog OUT INT) AS BEGIN binlog:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (byte OUT INT) AS BEGIN byte:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (cache OUT INT) AS BEGIN cache:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (checksum OUT INT) AS BEGIN checksum:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (checkpoint OUT INT) AS BEGIN checkpoint:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (column_add OUT INT) AS BEGIN column_add:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (column_check OUT INT) AS BEGIN column_check:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (column_create OUT INT) AS BEGIN column_create:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (column_delete OUT INT) AS BEGIN column_delete:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (column_get OUT INT) AS BEGIN column_get:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (deallocate OUT INT) AS BEGIN deallocate:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (examined OUT INT) AS BEGIN examined:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (execute OUT INT) AS BEGIN execute:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (flush OUT INT) AS BEGIN flush:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (format OUT INT) AS BEGIN format:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (get OUT INT) AS BEGIN get:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (help OUT INT) AS BEGIN help:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (host OUT INT) AS BEGIN host:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (install OUT INT) AS BEGIN install:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (option OUT INT) AS BEGIN option:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (options OUT INT) AS BEGIN options:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (owner OUT INT) AS BEGIN owner:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (parser OUT INT) AS BEGIN parser:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (port OUT INT) AS BEGIN port:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (prepare OUT INT) AS BEGIN prepare:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (remove OUT INT) AS BEGIN remove:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (reset OUT INT) AS BEGIN reset:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (restore OUT INT) AS BEGIN restore:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (security OUT INT) AS BEGIN security:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (server OUT INT) AS BEGIN server:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (signed OUT INT) AS BEGIN signed:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (socket OUT INT) AS BEGIN socket:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (slave OUT INT) AS BEGIN slave:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (slaves OUT INT) AS BEGIN slaves:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (soname OUT INT) AS BEGIN soname:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (start OUT INT) AS BEGIN start:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (stop OUT INT) AS BEGIN stop:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (stored OUT INT) AS BEGIN stored:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (unicode OUT INT) AS BEGIN unicode:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (uninstall OUT INT) AS BEGIN uninstall:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (upgrade OUT INT) AS BEGIN upgrade:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (wrapper OUT INT) AS BEGIN wrapper:=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (xa OUT INT) AS BEGIN xa:=10; END;/
DROP PROCEDURE p1/
# Testing that keyword_directly_not_assignable does not work in :=
CREATE PROCEDURE p1 (commit OUT INT) AS BEGIN commit:=10; END;/
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':=10; END' at line 1
CREATE PROCEDURE p1 (rollback OUT INT) AS BEGIN rollback:=10; END;/
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':=10; END' at line 1
CREATE PROCEDURE p1 (shutdown OUT INT) AS BEGIN shutdown:=10; END;/
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':=10; END' at line 1
CREATE PROCEDURE p1 (exception OUT INT) AS BEGIN exception:=10; END;/
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':=10; END' at line 1
# Testing that keyword_directly_not_assignable works in SET statements.
CREATE PROCEDURE p1 (contains OUT INT) AS BEGIN SET contains=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (language OUT INT) AS BEGIN SET language=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (no OUT INT) AS BEGIN SET no=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (charset OUT INT) AS BEGIN SET charset=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (do OUT INT) AS BEGIN SET do=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (repair OUT INT) AS BEGIN SET repair=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (handler OUT INT) AS BEGIN SET handler=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (open OUT INT) AS BEGIN SET open=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (close OUT INT) AS BEGIN SET close=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (savepoint OUT INT) AS BEGIN SET savepoint=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (truncate OUT INT) AS BEGIN SET truncate=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (begin OUT INT) AS BEGIN SET begin=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (end OUT INT) AS BEGIN SET end=10; END;/
DROP PROCEDURE p1/
CREATE PROCEDURE p1 (exception OUT INT) AS BEGIN SET exception=10; END;/
DROP PROCEDURE p1/
# Testing that keyword_directly_not_assignable works in table/column names
CREATE TABLE contains (contains INT);
DROP TABLE contains;
CREATE TABLE language (language INT);
DROP TABLE language;
CREATE TABLE no (no INT);
DROP TABLE no;
CREATE TABLE charset (charset INT);
DROP TABLE charset;
CREATE TABLE do (do INT);
DROP TABLE do;
CREATE TABLE repair (repair INT);
DROP TABLE repair;
CREATE TABLE handler (handler INT);
DROP TABLE handler;
CREATE TABLE open (open INT);
DROP TABLE open;
CREATE TABLE close (close INT);
DROP TABLE close;
CREATE TABLE savepoint (savepoint INT);
DROP TABLE savepoint;
CREATE TABLE truncate (truncate INT);
DROP TABLE truncate;
CREATE TABLE begin (begin INT);
DROP TABLE begin;
CREATE TABLE end (end INT);
DROP TABLE end;
CREATE TABLE exception (exception INT);
DROP TABLE exception;
# Testing ELSIF
CREATE FUNCTION f1(a INT) RETURN CLOB
AS
BEGIN
IF a=1 THEN RETURN 'a is 1';
ELSIF a=2 THEN RETURN 'a is 2';
ELSE RETURN 'a is unknown';
END IF;
END;
/
SELECT f1(2) FROM DUAL;
f1(2)
a is 2
DROP FUNCTION f1;
# Testing top-level declarations
CREATE PROCEDURE p1 (p1 OUT VARCHAR2(10))
AS
p2 VARCHAR(10);
BEGIN
p2:='p1new';
p1:=p2;
END;
/
SET @p1='p1';
CALL p1(@p1);
SELECT @p1;
@p1
p1new
DROP PROCEDURE p1;
CREATE FUNCTION f1 (p1 VARCHAR2(10)) RETURN VARCHAR(20)
AS
p2 VARCHAR(10);
BEGIN
p2:='new';
RETURN CONCAT(p1, p2);
END;
/
SET @p1='p1';
SELECT f1(@p1);
f1(@p1)
p1new
DROP FUNCTION f1;
# Testing non-top declarations
CREATE PROCEDURE p1 (p1 OUT VARCHAR2(10))
AS
BEGIN
DECLARE
p2 VARCHAR(10);
BEGIN
p2:='p1new';
p1:=p2;
END;
DECLARE
t1 VARCHAR(10);
t2 VARCHAR(10);
BEGIN
END;
END;
/
SET @p1='p1';
CALL p1(@p1);
SELECT @p1;
@p1
p1new
DROP PROCEDURE p1;
CREATE FUNCTION f1 (p1 VARCHAR2(10)) RETURN VARCHAR(20)
AS
BEGIN
DECLARE
p2 VARCHAR(10);
BEGIN
p2:='new';
RETURN CONCAT(p1, p2);
END;
DECLARE
t1 VARCHAR(10);
t2 VARCHAR(10);
BEGIN
END;
END;
/
SET @p1='p1';
SELECT f1(@p1);
f1(@p1)
p1new
DROP FUNCTION f1;
# Testing exceptions
CREATE TABLE t1 (c1 INT);
CREATE PROCEDURE sp1 (p1 IN VARCHAR2(20), p2 OUT VARCHAR2(30))
IS
v1 INT;
BEGIN
SELECT c1 INTO v1 FROM t1;
p2 := p1;
EXCEPTION
WHEN NOT FOUND THEN
BEGIN
p2 := 'def';
END;
END;
/
CALL sp1('abc', @a);
SELECT @a;
@a
def
DROP PROCEDURE sp1;
DROP TABLE t1;
CREATE PROCEDURE sp1 (v IN OUT INT, error IN INT)
IS
BEGIN
SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=error, MESSAGE_TEXT='User defined error!';
v:= 223;
EXCEPTION
WHEN 30001 THEN
BEGIN
v:= 113;
END;
END;
/
SET @v=10;
CALL sp1(@v, 30001);
CALL sp1(@v, 30002);
ERROR 45000: User defined error!
SELECT @v;
@v
113
DROP PROCEDURE sp1;
CREATE PROCEDURE sp1 (v IN OUT INT, error IN INT)
IS
BEGIN
BEGIN
BEGIN
SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=error, MESSAGE_TEXT='User defined error!';
v:= 223;
EXCEPTION
WHEN 30001 THEN
BEGIN
v:= 113;
END;
END;
END;
END;
/
SET @v=10;
CALL sp1(@v, 30001);
SELECT @v;
@v
113
SET @v=10;
CALL sp1(@v, 30002);
ERROR 45000: User defined error!
SELECT @v;
@v
10
DROP PROCEDURE sp1;
#
# Testing EXIT statement
#
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
EXIT;
END;
/
ERROR 42000: EXIT with no matching label: 
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
<<lable1>>
BEGIN
<<label2>>
LOOP
EXIT label1;
END LOOP;
END;
END;
/
ERROR 42000: EXIT with no matching label: label1
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
LOOP
LOOP
i:= i + 1;
IF i >= 5 THEN
EXIT;
END IF;
END LOOP;
i:= i + 100;
EXIT;
END LOOP;
RETURN i;
END;
/
SELECT f1() FROM DUAL;
f1()
105
DROP FUNCTION f1;
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
<<label1>>
LOOP
<<label2>>
LOOP
i:= i + 1;
IF i >= 5 THEN
EXIT label2;
END IF;
END LOOP;
i:= i + 100;
EXIT;
END LOOP;
RETURN i;
END;
/
SELECT f1() FROM DUAL;
f1()
105
DROP FUNCTION f1;
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
<<label1>>
LOOP
<<label2>>
LOOP
i:= i + 1;
IF i >= 5 THEN
EXIT label1;
END IF;
END LOOP;
i:= i + 100;
EXIT;
END LOOP;
RETURN i;
END;
/
SELECT f1() FROM DUAL;
f1()
5
DROP FUNCTION f1;
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
LOOP
i:= i + 1;
EXIT WHEN i >=5;
END LOOP;
RETURN i;
END;
/
SELECT f1() FROM DUAL;
f1()
5
DROP FUNCTION f1;
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
<<label1>>
LOOP
<<label2>>
LOOP
i:= i + 1;
EXIT label2 WHEN i >= 5;
END LOOP;
i:= i + 100;
EXIT;
END LOOP;
RETURN i;
END;
/
SELECT f1() FROM DUAL;
f1()
105
DROP FUNCTION f1;
CREATE FUNCTION f1 RETURN INT
IS
i INT := 0;
BEGIN
<<label1>>
LOOP
<<label2>>
LOOP
i:= i + 1;
EXIT label1 WHEN i >= 5;
END LOOP;
i:= i + 100;
EXIT;
END LOOP;
RETURN i;
END;
/
SELECT f1() FROM DUAL;
f1()
5
DROP FUNCTION f1;
# Testing CURSOR declaration
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1);
CREATE FUNCTION f1 RETURN INT
AS
v_a INT:=10;
CURSOR c IS SELECT a FROM t1;
BEGIN
OPEN c;
FETCH c INTO v_a;
CLOSE c;
RETURN v_a;
EXCEPTION
WHEN OTHERS THEN RETURN -1;
END;
/
SELECT f1() FROM DUAL;
f1()
1
DROP FUNCTION f1;
DROP TABLE t1;
# Testing RETURN in procedures
CREATE PROCEDURE p1 (a IN OUT INT)
AS
BEGIN
RETURN 10;
END;
/
ERROR 42000: RETURN is only allowed in a FUNCTION
CREATE FUNCTION f1 (a INT) RETURN INT
AS
BEGIN
RETURN;
END;
/
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ';
END' at line 4
CREATE PROCEDURE p1 (a IN OUT INT)
AS
BEGIN
IF a < 10 THEN
BEGIN
a:= a - 1;
RETURN;
END;
END IF;
a:= a + 1;
EXCEPTION
WHEN OTHERS THEN RETURN;
END;
/
SET @v=10;
CALL p1(@v);
SELECT @v;
@v
11
SET @v=9;
CALL p1(@v);
SELECT @v;
@v
8
DROP PROCEDURE p1;
CREATE PROCEDURE p1 (a IN OUT INT)
AS
BEGIN
DROP TABLE t1_non_existent;
EXCEPTION
WHEN OTHERS THEN
BEGIN
a:= 100;
RETURN;
END;
END;
/
SET @v=10;
CALL p1(@v);
SELECT @v;
@v
100
DROP PROCEDURE p1;
# Testing WHILE loop
CREATE PROCEDURE p1 (a IN OUT INT)
AS
i INT:= 1;
j INT:= 3;
BEGIN
WHILE i<=j
LOOP
a:= a + i;
i:= i + 1;
END LOOP;
END;
/
SET @v=0;
CALL p1(@v);
SELECT @v;
@v
6
DROP PROCEDURE p1;
CREATE PROCEDURE p1 (a IN OUT INT)
AS
i INT:= 1;
j INT:= 3;
BEGIN
<<label>>
WHILE i<=j
LOOP
a:= a + i;
i:= i + 1;
END LOOP label;
END;
/
SET @v=0;
CALL p1(@v);
SELECT @v;
@v
6
DROP PROCEDURE p1;
# Testing the FOR loop statement
CREATE TABLE t1 (a INT);
FOR i IN 1..3
LOOP
INSERT INTO t1 VALUES (i);
END LOOP;
/
SELECT * FROM t1;
a
1
2
3
DROP TABLE t1;
CREATE FUNCTION f1 (lower_bound INT, upper_bound INT, lim INT) RETURN INT
AS
total INT := 0;
BEGIN
FOR i IN lower_bound . . upper_bound
LOOP
NULL
END LOOP;
RETURN total;
END;
/
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '. upper_bound
LOOP
NULL
END LOOP;
RETURN total;
END' at line 5
CREATE FUNCTION f1 (lower_bound INT, upper_bound INT, lim INT) RETURN INT
AS
total INT := 0;
BEGIN
FOR i IN lower_bound .. upper_bound
LOOP
total:= total + i;
IF i = lim THEN
EXIT;
END IF;
-- Bounds are calculated only once.
-- The below assignments have no effect on the loop condition
lower_bound:= 900;
upper_bound:= 1000;
END LOOP;
RETURN total;
END;
/
SELECT f1(1, 3, 100) FROM DUAL;
f1(1, 3, 100)
6
SELECT f1(1, 3, 2) FROM DUAL;
f1(1, 3, 2)
3
DROP FUNCTION f1;
CREATE FUNCTION f1 RETURN INT
AS
total INT := 0;
BEGIN
FOR i IN 1 .. 5
LOOP
total:= total + 1000;
FOR j IN 1 .. 5
LOOP
total:= total + 1;
IF j = 3 THEN
EXIT; -- End the internal loop
END IF;
END LOOP;
END LOOP;
RETURN total;
END;
/
SELECT f1() FROM DUAL;
f1()
5015
DROP FUNCTION f1;
CREATE FUNCTION f1 (a INT, b INT) RETURN INT
AS
total INT := 0;
BEGIN
FOR i IN REVERSE a..1
LOOP
total:= total + i;
IF i = b THEN
EXIT;
END IF;
END LOOP;
RETURN total;
END
/
SELECT f1(3, 100) FROM DUAL;
f1(3, 100)
6
SELECT f1(3, 2) FROM DUAL;
f1(3, 2)
5
DROP FUNCTION f1;
# Testing labeled FOR LOOP statement
CREATE FUNCTION f1 (a INT, limita INT, b INT, limitb INT) RETURN INT
AS
total INT := 0;
BEGIN
<<la>>
FOR ia IN 1 .. a
LOOP
total:= total + 1000;
<<lb>>
FOR ib IN 1 .. b
LOOP
total:= total + 1;
EXIT lb WHEN ib = limitb;
EXIT la WHEN ia = limita;
END LOOP lb;
END LOOP la;
RETURN total;
END;
/
SELECT f1(1, 1, 1, 1) FROM DUAL;
f1(1, 1, 1, 1)
1001
SELECT f1(1, 2, 1, 2) FROM DUAL;
f1(1, 2, 1, 2)
1001
SELECT f1(2, 1, 2, 1) FROM DUAL;
f1(2, 1, 2, 1)
2002
SELECT f1(2, 1, 2, 2) FROM DUAL;
f1(2, 1, 2, 2)
1001
SELECT f1(2, 2, 2, 2) FROM DUAL;
f1(2, 2, 2, 2)
2003
SELECT f1(2, 3, 2, 3) FROM DUAL;
f1(2, 3, 2, 3)
2004
DROP FUNCTION f1;
# Testing labeled ITERATE in a labeled FOR LOOP statement
CREATE FUNCTION f1 (a INT, b INT, blim INT) RETURN INT
AS
total INT := 0;
BEGIN
<<la>>
FOR ia IN 1 .. a
LOOP
total:= total + 1000;
DECLARE
ib INT:= 1;
BEGIN
WHILE ib <= b
LOOP
IF ib > blim THEN
ITERATE la;
END IF;
ib:= ib + 1;
total:= total + 1;
END LOOP;
END;
END LOOP la;
RETURN total;
END;
/
SELECT f1(3,3,0), f1(3,3,1), f1(3,3,2), f1(3,3,3), f1(3,3,4) FROM DUAL;
f1(3,3,0)	f1(3,3,1)	f1(3,3,2)	f1(3,3,3)	f1(3,3,4)
3000	3003	3006	3009	3009
DROP FUNCTION f1;
# Testing CONTINUE statement
CREATE FUNCTION f1(a INT) RETURN INT
AS
total INT:= 0;
BEGIN
FOR i IN 1 .. a
LOOP
IF i=5 THEN
CONTINUE;
END IF;
total:= total + 1;
END LOOP;
RETURN total;
END;
/
SELECT f1(3), f1(4), f1(5), f1(6) FROM DUAL;
f1(3)	f1(4)	f1(5)	f1(6)
3	4	4	5
DROP FUNCTION f1;
CREATE FUNCTION f1(a INT) RETURN INT
AS
total INT:= 0;
BEGIN
<<lj>>
FOR j IN 1 .. 2
LOOP
FOR i IN 1 .. a
LOOP
IF i=5 THEN
CONTINUE lj;
END IF;
total:= total + 1;
END LOOP;
END LOOP;
RETURN total;
END;
/
SELECT f1(3), f1(4), f1(5) FROM DUAL;
f1(3)	f1(4)	f1(5)
6	8	8
DROP FUNCTION f1;
CREATE FUNCTION f1(a INT) RETURN INT
AS
total INT:= 0;
BEGIN
<<lj>>
FOR j IN 1 .. 2
LOOP
FOR i IN 1 .. a
LOOP
CONTINUE lj WHEN i=5;
total:= total + 1;
END LOOP;
END LOOP;
RETURN total;
END;
/
SELECT f1(3), f1(4), f1(5) FROM DUAL;
f1(3)	f1(4)	f1(5)
6	8	8
DROP FUNCTION f1;
CREATE FUNCTION f1(a INT) RETURN INT
AS
total INT:= 0;
i INT:= 1;
BEGIN
WHILE i <= a
LOOP
i:= i + 1;
IF i=6 THEN
CONTINUE;
END IF;
total:= total + 1;
END LOOP;
RETURN total;
END;
/
SELECT f1(3), f1(4), f1(5), f1(6) FROM DUAL;
f1(3)	f1(4)	f1(5)	f1(6)
3	4	4	5
DROP FUNCTION f1;
#
# Testing behaviour of unknown identifiers in EXIT and CONTINUE statements
#
CREATE PROCEDURE p1
AS
BEGIN
LOOP
EXIT WHEN unknown_ident IS NULL;
END LOOP;
END$$
CALL p1;
ERROR 42S22: Unknown column 'unknown_ident' in 'field list'
DROP PROCEDURE p1;
CREATE PROCEDURE p1
AS
BEGIN
<<label>>
LOOP
EXIT label WHEN unknown_ident IS NULL;
END LOOP;
END$$
CALL p1;
ERROR 42S22: Unknown column 'unknown_ident' in 'field list'
DROP PROCEDURE p1;
CREATE PROCEDURE p1
AS
BEGIN
LOOP
CONTINUE WHEN unknown_ident IS NULL;
END LOOP;
END$$
CALL p1;
ERROR 42S22: Unknown column 'unknown_ident' in 'field list'
DROP PROCEDURE p1;
CREATE PROCEDURE p1
AS
BEGIN
<<label>>
LOOP
CONTINUE label WHEN unknown_ident IS NULL;
END LOOP;
END$$
CALL p1;
ERROR 42S22: Unknown column 'unknown_ident' in 'field list'
DROP PROCEDURE p1;
#
# MDEV-10583 sql_mode=ORACLE: SQL%ROWCOUNT
#
EXPLAIN EXTENDED SELECT sql%rowcount;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
Warnings:
Note	1003	select SQL%ROWCOUNT AS "sql%rowcount"
CREATE TABLE t1 AS SELECT SQL%ROWCOUNT;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE "t1" (
  "SQL%ROWCOUNT" bigint(21) NOT NULL
)
DROP TABLE t1;
#
# UPDATE
#
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1
AS
BEGIN
UPDATE t1 SET a=30;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
0
DROP PROCEDURE p1;
DROP TABLE t1;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (10);
INSERT INTO t1 VALUES (20);
CREATE PROCEDURE p1
AS
BEGIN
UPDATE t1 SET a=30;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
2
DROP PROCEDURE p1;
DROP TABLE t1;
#
# DELETE
#
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1
AS
BEGIN
DELETE FROM t1;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
0
DROP PROCEDURE p1;
DROP TABLE t1;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (10);
INSERT INTO t1 VALUES (20);
CREATE PROCEDURE p1
AS
BEGIN
DELETE FROM t1;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
2
DROP PROCEDURE p1;
DROP TABLE t1;
#
# SELECT ... INTO var FROM ... - one row found
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (10);
INSERT INTO t1 VALUES (20);
CREATE PROCEDURE p1
AS
va INT;
BEGIN
SELECT a INTO va FROM t1 LIMIT 1;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
1
DROP PROCEDURE p1;
DROP TABLE t1;
#
# SELECT ... INTO var FROM ... - no rows found
#
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1
AS
va INT;
BEGIN
SELECT a INTO va FROM t1;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
0
Warnings:
Warning	1329	No data - zero rows fetched, selected, or processed
DROP PROCEDURE p1;
DROP TABLE t1;
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1
AS
va INT;
BEGIN
SELECT a INTO va FROM t1;
SELECT SQL%ROWCOUNT;
EXCEPTION
WHEN NO_DATA_FOUND THEN SELECT SQL%ROWCOUNT||' (EXCEPTION)';
END;
$$
CALL p1();
SQL%ROWCOUNT||' (EXCEPTION)'
0 (EXCEPTION)
DROP PROCEDURE p1;
DROP TABLE t1;
#
# SELECT ... INTO var FROM ... - multiple rows found
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (10);
INSERT INTO t1 VALUES (20);
CREATE PROCEDURE p1
AS
va INT:=1;
BEGIN
SELECT a INTO va FROM t1;
SELECT SQL%ROWCOUNT;
EXCEPTION
WHEN TOO_MANY_ROWS THEN SELECT SQL%ROWCOUNT||' (EXCEPTION) va='||va;
END;
$$
CALL p1();
SQL%ROWCOUNT||' (EXCEPTION) va='||va
1 (EXCEPTION) va=10
DROP PROCEDURE p1;
DROP TABLE t1;
#
# INSERT INTO t2 SELECT ...
#
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
INSERT INTO t1 VALUES (10);
INSERT INTO t1 VALUES (20);
CREATE PROCEDURE p1
AS
BEGIN
INSERT INTO t2 SELECT * FROM t1;
SELECT SQL%ROWCOUNT;
END;
$$
CALL p1();
SQL%ROWCOUNT
2
DROP PROCEDURE p1;
DROP TABLE t1, t2;
#
# End of MDEV-10583 sql_mode=ORACLE: SQL%ROWCOUNT
#
#
# MDEV-10577 sql_mode=ORACLE: %TYPE in variable declarations
#
#
# Missing table
#
CREATE PROCEDURE p1
AS
a t1.a%TYPE;
BEGIN
NULL;
END;
$$
CALL p1();
ERROR 42S02: Table 'test.t1' doesn't exist
DROP PROCEDURE p1;
#
# Missing column
#
CREATE TABLE t1 (b INT);
CREATE PROCEDURE p1
AS
a t1.a%TYPE;
BEGIN
NULL;
END;
$$
CALL p1();
ERROR 42S22: Unknown column 'a' in 't1'
DROP PROCEDURE p1;
DROP TABLE t1;
#
# One %TYPE variable
#
CREATE TABLE t1 (a INT);
CREATE PROCEDURE p1
AS
a t1.a%TYPE;
BEGIN
a:= 123;
SELECT a;
END;
$$
CALL p1();
a
123
DROP PROCEDURE p1;
DROP TABLE t1;
#
# Two %TYPE variables, with a truncation warning on assignment
#
CREATE TABLE t1 (a TINYINT, b INT);
CREATE PROCEDURE p1
AS
a t1.a%TYPE;
b t1.b%TYPE;
BEGIN
a:= 200;
b:= 200;
SELECT a, b;
END;
$$
CALL p1();
a	b
127	200
Warnings:
Warning	1264	Out of range value for column 'a' at row 1
DROP PROCEDURE p1;
DROP TABLE t1;
#
# %TYPE variables for fields with various attributes
#
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
a TINYINT NOT NULL,
b INT NOT NULL,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE(a)
);
CREATE PROCEDURE p1
AS
id t1.id%TYPE;
a t1.a%TYPE;
b t1.b%TYPE;
ts t1.ts%TYPE;
BEGIN
SELECT id, a, b, ts;
CREATE TABLE t2 AS SELECT id, a, b, ts;
SHOW CREATE TABLE t2;
DROP TABLE t2;
END;
$$
CALL p1();
id	a	b	ts
NULL	NULL	NULL	NULL
Table	Create Table
t2	CREATE TABLE "t2" (
  "id" int(11) DEFAULT NULL,
  "a" tinyint(4) DEFAULT NULL,
  "b" int(11) DEFAULT NULL,
  "ts" timestamp NULL DEFAULT NULL
)
DROP PROCEDURE p1;
DROP TABLE t1;
#
# %TYPE + virtual columns
#
CREATE TABLE t1 (
a INT NOT NULL,
b VARCHAR(32),
c INT AS (a + 10) VIRTUAL,
d VARCHAR(5) AS (left(b,5)) PERSISTENT
);
CREATE PROCEDURE p1
AS
c t1.c%TYPE;
d t1.d%TYPE;
BEGIN
SELECT c, d;
CREATE TABLE t2 AS SELECT c, d;
SHOW CREATE TABLE t2;
DROP TABLE t2;
END;
$$
CALL p1();
c	d
NULL	NULL
Table	Create Table
t2	CREATE TABLE "t2" (
  "c" int(11) DEFAULT NULL,
  "d" varchar(5) DEFAULT NULL
)
DROP PROCEDURE p1;
DROP TABLE t1;
#
# %TYPE + the ZEROFILL attribute
#
CREATE TABLE t1 (
dz DECIMAL(10,3) ZEROFILL
);
CREATE PROCEDURE p1
AS
dzr t1.dz%TYPE := 10;
dzt DECIMAL(10,3) ZEROFILL := 10;
BEGIN
SELECT dzr, dzt;
CREATE TABLE t2 AS SELECT dzr,dzt;
SHOW CREATE TABLE t2;
DROP TABLE t2;
END;
$$
CALL p1();
dzr	dzt
0000010.000	0000010.000
Table	Create Table
t2	CREATE TABLE "t2" (
  "dzr" decimal(10,3) unsigned DEFAULT NULL,
  "dzt" decimal(10,3) unsigned DEFAULT NULL
)
DROP PROCEDURE p1;
DROP TABLE t1;
#
# Temporary tables shadow real tables for %TYPE purposes
#
CREATE TABLE t1 (a VARCHAR(10));
INSERT INTO t1 VALUES ('t1');
CREATE TEMPORARY TABLE t1 (a INT);
INSERT INTO t1 VALUES (10);
SELECT * FROM t1;
a
10
CREATE PROCEDURE p1
AS
a t1.a%TYPE:=11;
BEGIN
CREATE TABLE t2 AS SELECT a;
END;
$$
#
# Should use INT(11) as %TYPE, as in the temporary table
#
CALL p1();
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" int(11) DEFAULT NULL
)
SELECT * FROM t2;
a
11
DROP TABLE t2;
SELECT * FROM t1;
a
10
DROP TEMPORARY TABLE t1;
SELECT * FROM t1;
a
t1
#
# Should use VARCHAR(10) as %TYPE, as in the real table
#
CALL p1();
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" varchar(10) DEFAULT NULL
)
SELECT * FROM t2;
a
11
DROP TABLE t2;
DROP PROCEDURE p1;
DROP TABLE t1;
#
# t1.a%TYPE searches for "t1" in the current database
#
CREATE TABLE t1 (a VARCHAR(10));
CREATE DATABASE test1;
CREATE TABLE test1.t1 (a INT);
CREATE PROCEDURE p1
AS
a t1.a%TYPE:=11;
BEGIN
CREATE TABLE test.t2 AS SELECT a;
END;
$$
#
# This interprets t1.a%TYPE as VARCHAR(10), as in test.t1.a
#
USE test;
CALL test.p1();
SHOW CREATE TABLE test.t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" varchar(10) DEFAULT NULL
)
DROP TABLE test.t2;
#
# This interprets t1.a%TYPE as INT, as in test1.t1.a
#
USE test1;
CALL test.p1();
SHOW CREATE TABLE test.t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" int(11) DEFAULT NULL
)
DROP TABLE test.t2;
#
# Error if there is no an active database
#
DROP DATABASE test1;
CALL test.p1();
ERROR 3D000: No database selected
USE test;
DROP PROCEDURE p1;
DROP TABLE t1;
#
# A reference to a table in a non-existing database
#
CREATE PROCEDURE p1
AS
a test1.t1.a%TYPE;
BEGIN
CREATE TABLE t1 AS SELECT a;
END;
$$
CALL p1;
ERROR 42S02: Table 'test1.t1' doesn't exist
DROP PROCEDURE p1;
#
# A reference to a table in a different database
#
CREATE TABLE t1(a INT);
CREATE DATABASE test1;
CREATE TABLE test1.t1 (a VARCHAR(10));
CREATE PROCEDURE p1
AS
a t1.a%TYPE;
b test1.t1.a%TYPE;
BEGIN
CREATE TABLE t2 AS SELECT a,b;
END;
$$
CALL p1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" int(11) DEFAULT NULL,
  "b" varchar(10) DEFAULT NULL
)
DROP PROCEDURE p1;
DROP TABLE t2;
DROP DATABASE test1;
DROP TABLE t1;
#
# Using a table before it appears in a %TYPE declaration + multiple %TYPE declarations
#
CREATE TABLE t1 (a INT, b VARCHAR(10));
INSERT INTO t1 (a,b) VALUES (10,'b10');
CREATE PROCEDURE p1
AS
BEGIN
INSERT INTO t1 (a,b) VALUES (11, 'b11');
SELECT * FROM t1;
DECLARE
va t1.a%TYPE:= 30;
vb t1.b%TYPE:= 'b30';
BEGIN
INSERT INTO t1 (a,b) VALUES (12,'b12');
SELECT * FROM t1;
INSERT INTO t1 (a,b) VALUES (va, vb);
SELECT * FROM t1;
END;
DECLARE
va t1.a%TYPE:= 40;
vb t1.b%TYPE:= 'b40';
BEGIN
INSERT INTO t1 (a,b) VALUES (va,vb);
SELECT * FROM t1;
END;
END;
$$
CALL p1;
a	b
10	b10
11	b11
a	b
10	b10
11	b11
12	b12
a	b
10	b10
11	b11
12	b12
30	b30
a	b
10	b10
11	b11
12	b12
30	b30
40	b40
DROP TABLE t1;
DROP PROCEDURE p1;
#
# %TYPE variables + TABLE vs VIEW
#
CREATE TABLE t1 (
bit6 BIT(6),
bit7 BIT(7),
bit8 BIT(8),
i1 TINYINT,
i2 SMALLINT,
i3 MEDIUMINT,
i4 INT,
i8 BIGINT,
ff FLOAT,
fd DOUBLE,
cc CHAR(10),
cv VARCHAR(10),
cvu VARCHAR(10) CHARACTER SET utf8,
t1 TINYTEXT,
t2 TEXT,
t3 MEDIUMTEXT,
t4 LONGTEXT,
enum1 ENUM('a','b','c'),
set1  SET('a','b','c'),
blob1 TINYBLOB,
blob2 BLOB,
blob3 MEDIUMBLOB,
blob4 LONGBLOB,
yy  YEAR,
dd  DATE,
tm0 TIME,
tm3 TIME(3),
tm6 TIME(6),
dt0 DATETIME,
dt3 DATETIME(3),
dt6 DATETIME(6),
ts0 TIMESTAMP,
ts3 TIMESTAMP(3),
ts6 TIMESTAMP(6),
dc100 DECIMAL(10,0),
dc103 DECIMAL(10,3),
dc209 DECIMAL(20,9)
);
CREATE PROCEDURE p1(command enum('create','select'))
AS
bit6  t1.bit6%TYPE := 0x30;
bit7  t1.bit7%TYPE := 0x41;
bit8  t1.bit8%TYPE := 0x7E;
i1  t1.i1%TYPE := 11;
i2  t1.i2%TYPE := 12;
i3  t1.i3%TYPE := 13;
i4  t1.i4%TYPE := 14;
i8  t1.i8%TYPE := 18;
ff  t1.ff%TYPE := 21;
fd  t1.fd%TYPE := 22;
cc  t1.cc%TYPE := 'char';
cv  t1.cv%TYPE := 'varchar';
cvu t1.cvu%TYPE := 'varcharu8';
t1  t1.t1%TYPE := 'text1';
t2  t1.t2%TYPE := 'text2';
t3  t1.t3%TYPE := 'text3';
t4  t1.t4%TYPE := 'text4';
enum1 t1.enum1%TYPE := 'b';
set1  t1.set1%TYPE  := 'a,c';
blob1 t1.blob1%TYPE := 'blob1';
blob2 t1.blob2%TYPE := 'blob2';
blob3 t1.blob3%TYPE := 'blob3';
blob4 t1.blob4%TYPE := 'blob4';
yy  t1.yy%TYPE := 2001;
dd  t1.dd%TYPE := '2001-01-01';
tm0 t1.tm0%TYPE := '00:00:01';
tm3 t1.tm3%TYPE := '00:00:03.333';
tm6 t1.tm6%TYPE := '00:00:06.666666';
dt0 t1.dt0%TYPE := '2001-01-01 00:00:01';
dt3 t1.dt3%TYPE := '2001-01-03 00:00:01.333';
dt6 t1.dt6%TYPE := '2001-01-06 00:00:01.666666';
ts0 t1.ts0%TYPE := '2002-01-01 00:00:01';
ts3 t1.ts3%TYPE := '2002-01-03 00:00:01.333';
ts6 t1.ts6%TYPE := '2002-01-06 00:00:01.666666';
dc100 t1.dc100%TYPE := 10;
dc103 t1.dc103%TYPE := 10.123;
dc209 t1.dc209%TYPE := 10.123456789;
BEGIN
CASE
WHEN command='create' THEN
CREATE TABLE t2 AS SELECT
bit6, bit7, bit8,
i1,i2,i3,i4,i8,
ff,fd, dc100, dc103, dc209,
cc,cv,cvu,
t1,t2,t3,t4,
enum1, set1,
blob1, blob2, blob3, blob4,
dd, yy,
tm0, tm3, tm6,
dt0, dt3, dt6,
ts0, ts3, ts6;
WHEN command='select' THEN
SELECT
bit6, bit7, bit8,
i1,i2,i3,i4,i8,
ff,fd, dc100, dc103, dc209,
cc,cv,cvu,
t1,t2,t3,t4,
enum1, set1,
blob1, blob2, blob3, blob4,
dd, yy,
tm0, tm3, tm6,
dt0, dt3, dt6,
ts0, ts3, ts6;
END CASE;
END;
$$
#
# TABLE
#
CALL p1('create');
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "bit6" bit(6) DEFAULT NULL,
  "bit7" bit(7) DEFAULT NULL,
  "bit8" bit(8) DEFAULT NULL,
  "i1" tinyint(4) DEFAULT NULL,
  "i2" smallint(6) DEFAULT NULL,
  "i3" mediumint(9) DEFAULT NULL,
  "i4" int(11) DEFAULT NULL,
  "i8" bigint(20) DEFAULT NULL,
  "ff" float DEFAULT NULL,
  "fd" double DEFAULT NULL,
  "dc100" decimal(10,0) DEFAULT NULL,
  "dc103" decimal(10,3) DEFAULT NULL,
  "dc209" decimal(20,9) DEFAULT NULL,
  "cc" varchar(10) DEFAULT NULL,
  "cv" varchar(10) DEFAULT NULL,
  "cvu" varchar(10) CHARACTER SET utf8 DEFAULT NULL,
  "t1" tinytext DEFAULT NULL,
  "t2" text DEFAULT NULL,
  "t3" mediumtext DEFAULT NULL,
  "t4" longtext DEFAULT NULL,
  "enum1" varchar(1) DEFAULT NULL,
  "set1" varchar(5) DEFAULT NULL,
  "blob1" tinyblob DEFAULT NULL,
  "blob2" longblob DEFAULT NULL,
  "blob3" mediumblob DEFAULT NULL,
  "blob4" longblob DEFAULT NULL,
  "dd" datetime DEFAULT NULL,
  "yy" year(4) DEFAULT NULL,
  "tm0" time DEFAULT NULL,
  "tm3" time(3) DEFAULT NULL,
  "tm6" time(6) DEFAULT NULL,
  "dt0" datetime DEFAULT NULL,
  "dt3" datetime(3) DEFAULT NULL,
  "dt6" datetime(6) DEFAULT NULL,
  "ts0" timestamp NULL DEFAULT NULL,
  "ts3" timestamp(3) NULL DEFAULT NULL,
  "ts6" timestamp(6) NULL DEFAULT NULL
)
SELECT * FROM t2;
bit6	0
bit7	A
bit8	~
i1	11
i2	12
i3	13
i4	14
i8	18
ff	21
fd	22
dc100	10
dc103	10.123
dc209	10.123456789
cc	char
cv	varchar
cvu	varcharu8
t1	text1
t2	text2
t3	text3
t4	text4
enum1	b
set1	a,c
blob1	blob1
blob2	blob2
blob3	blob3
blob4	blob4
dd	2001-01-01 00:00:00
yy	2001
tm0	00:00:01
tm3	00:00:03.333
tm6	00:00:06.666666
dt0	2001-01-01 00:00:01
dt3	2001-01-03 00:00:01.333
dt6	2001-01-06 00:00:01.666666
ts0	2002-01-01 00:00:01
ts3	2002-01-03 00:00:01.333
ts6	2002-01-06 00:00:01.666666
DROP TABLE t2;
CALL p1('select');
Catalog	Database	Table	Table_alias	Column	Column_alias	Type	Length	Max length	Is_null	Flags	Decimals	Charsetnr
def				bit6	bit6	16	6	1	Y	32	0	63
def				bit7	bit7	16	7	1	Y	32	0	63
def				bit8	bit8	16	8	1	Y	32	0	63
def				i1	i1	1	4	2	Y	32768	0	63
def				i2	i2	2	6	2	Y	32768	0	63
def				i3	i3	9	9	2	Y	32768	0	63
def				i4	i4	3	11	2	Y	32768	0	63
def				i8	i8	8	20	2	Y	32768	0	63
def				ff	ff	4	12	2	Y	32768	31	63
def				fd	fd	5	22	2	Y	32768	31	63
def				dc100	dc100	246	11	2	Y	32768	0	63
def				dc103	dc103	246	12	6	Y	32768	3	63
def				dc209	dc209	246	22	12	Y	32768	9	63
def				cc	cc	254	10	4	Y	0	0	8
def				cv	cv	253	10	7	Y	0	0	8
def				cvu	cvu	253	10	9	Y	0	0	8
def				t1	t1	252	255	5	Y	16	0	8
def				t2	t2	252	65535	5	Y	16	0	8
def				t3	t3	252	16777215	5	Y	16	0	8
def				t4	t4	252	4294967295	5	Y	16	0	8
def				enum1	enum1	254	1	1	Y	256	0	8
def				set1	set1	254	5	3	Y	2048	0	8
def				blob1	blob1	252	255	5	Y	144	0	63
def				blob2	blob2	252	4294967295	5	Y	144	0	63
def				blob3	blob3	252	16777215	5	Y	144	0	63
def				blob4	blob4	252	4294967295	5	Y	144	0	63
def				dd	dd	12	19	19	Y	128	0	63
def				yy	yy	13	4	4	Y	32864	0	63
def				tm0	tm0	11	10	8	Y	128	0	63
def				tm3	tm3	11	14	12	Y	128	3	63
def				tm6	tm6	11	17	15	Y	128	6	63
def				dt0	dt0	12	19	19	Y	128	0	63
def				dt3	dt3	12	23	23	Y	128	3	63
def				dt6	dt6	12	26	26	Y	128	6	63
def				ts0	ts0	7	19	19	Y	9376	0	63
def				ts3	ts3	7	23	23	Y	160	3	63
def				ts6	ts6	7	26	26	Y	160	6	63
bit6	0
bit7	A
bit8	~
i1	11
i2	12
i3	13
i4	14
i8	18
ff	21
fd	22
dc100	10
dc103	10.123
dc209	10.123456789
cc	char
cv	varchar
cvu	varcharu8
t1	text1
t2	text2
t3	text3
t4	text4
enum1	b
set1	a,c
blob1	blob1
blob2	blob2
blob3	blob3
blob4	blob4
dd	2001-01-01 00:00:00
yy	2001
tm0	00:00:01
tm3	00:00:03.333
tm6	00:00:06.666666
dt0	2001-01-01 00:00:01
dt3	2001-01-03 00:00:01.333
dt6	2001-01-06 00:00:01.666666
ts0	2002-01-01 00:00:01
ts3	2002-01-03 00:00:01.333
ts6	2002-01-06 00:00:01.666666
#
# VIEW
#
ALTER TABLE t1 RENAME t0;
CREATE VIEW t1 AS SELECT * FROM t0;
CALL p1('create');
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "bit6" bit(6) DEFAULT NULL,
  "bit7" bit(7) DEFAULT NULL,
  "bit8" bit(8) DEFAULT NULL,
  "i1" tinyint(4) DEFAULT NULL,
  "i2" smallint(6) DEFAULT NULL,
  "i3" mediumint(9) DEFAULT NULL,
  "i4" int(11) DEFAULT NULL,
  "i8" bigint(20) DEFAULT NULL,
  "ff" float DEFAULT NULL,
  "fd" double DEFAULT NULL,
  "dc100" decimal(10,0) DEFAULT NULL,
  "dc103" decimal(10,3) DEFAULT NULL,
  "dc209" decimal(20,9) DEFAULT NULL,
  "cc" varchar(10) DEFAULT NULL,
  "cv" varchar(10) DEFAULT NULL,
  "cvu" varchar(10) CHARACTER SET utf8 DEFAULT NULL,
  "t1" tinytext DEFAULT NULL,
  "t2" text DEFAULT NULL,
  "t3" mediumtext DEFAULT NULL,
  "t4" longtext DEFAULT NULL,
  "enum1" varchar(1) DEFAULT NULL,
  "set1" varchar(5) DEFAULT NULL,
  "blob1" tinyblob DEFAULT NULL,
  "blob2" longblob DEFAULT NULL,
  "blob3" mediumblob DEFAULT NULL,
  "blob4" longblob DEFAULT NULL,
  "dd" datetime DEFAULT NULL,
  "yy" year(4) DEFAULT NULL,
  "tm0" time DEFAULT NULL,
  "tm3" time(3) DEFAULT NULL,
  "tm6" time(6) DEFAULT NULL,
  "dt0" datetime DEFAULT NULL,
  "dt3" datetime(3) DEFAULT NULL,
  "dt6" datetime(6) DEFAULT NULL,
  "ts0" timestamp NULL DEFAULT NULL,
  "ts3" timestamp(3) NULL DEFAULT NULL,
  "ts6" timestamp(6) NULL DEFAULT NULL
)
SELECT * FROM t2;
bit6	0
bit7	A
bit8	~
i1	11
i2	12
i3	13
i4	14
i8	18
ff	21
fd	22
dc100	10
dc103	10.123
dc209	10.123456789
cc	char
cv	varchar
cvu	varcharu8
t1	text1
t2	text2
t3	text3
t4	text4
enum1	b
set1	a,c
blob1	blob1
blob2	blob2
blob3	blob3
blob4	blob4
dd	2001-01-01 00:00:00
yy	2001
tm0	00:00:01
tm3	00:00:03.333
tm6	00:00:06.666666
dt0	2001-01-01 00:00:01
dt3	2001-01-03 00:00:01.333
dt6	2001-01-06 00:00:01.666666
ts0	2002-01-01 00:00:01
ts3	2002-01-03 00:00:01.333
ts6	2002-01-06 00:00:01.666666
DROP TABLE t2;
CALL p1('select');
Catalog	Database	Table	Table_alias	Column	Column_alias	Type	Length	Max length	Is_null	Flags	Decimals	Charsetnr
def				bit6	bit6	16	6	1	Y	32	0	63
def				bit7	bit7	16	7	1	Y	32	0	63
def				bit8	bit8	16	8	1	Y	32	0	63
def				i1	i1	1	4	2	Y	32768	0	63
def				i2	i2	2	6	2	Y	32768	0	63
def				i3	i3	9	9	2	Y	32768	0	63
def				i4	i4	3	11	2	Y	32768	0	63
def				i8	i8	8	20	2	Y	32768	0	63
def				ff	ff	4	12	2	Y	32768	31	63
def				fd	fd	5	22	2	Y	32768	31	63
def				dc100	dc100	246	11	2	Y	32768	0	63
def				dc103	dc103	246	12	6	Y	32768	3	63
def				dc209	dc209	246	22	12	Y	32768	9	63
def				cc	cc	254	10	4	Y	0	0	8
def				cv	cv	253	10	7	Y	0	0	8
def				cvu	cvu	253	10	9	Y	0	0	8
def				t1	t1	252	255	5	Y	16	0	8
def				t2	t2	252	65535	5	Y	16	0	8
def				t3	t3	252	16777215	5	Y	16	0	8
def				t4	t4	252	4294967295	5	Y	16	0	8
def				enum1	enum1	254	1	1	Y	256	0	8
def				set1	set1	254	5	3	Y	2048	0	8
def				blob1	blob1	252	255	5	Y	144	0	63
def				blob2	blob2	252	4294967295	5	Y	144	0	63
def				blob3	blob3	252	16777215	5	Y	144	0	63
def				blob4	blob4	252	4294967295	5	Y	144	0	63
def				dd	dd	12	19	19	Y	128	0	63
def				yy	yy	13	4	4	Y	32864	0	63
def				tm0	tm0	11	10	8	Y	128	0	63
def				tm3	tm3	11	14	12	Y	128	3	63
def				tm6	tm6	11	17	15	Y	128	6	63
def				dt0	dt0	12	19	19	Y	128	0	63
def				dt3	dt3	12	23	23	Y	128	3	63
def				dt6	dt6	12	26	26	Y	128	6	63
def				ts0	ts0	7	19	19	Y	160	0	63
def				ts3	ts3	7	23	23	Y	160	3	63
def				ts6	ts6	7	26	26	Y	160	6	63
bit6	0
bit7	A
bit8	~
i1	11
i2	12
i3	13
i4	14
i8	18
ff	21
fd	22
dc100	10
dc103	10.123
dc209	10.123456789
cc	char
cv	varchar
cvu	varcharu8
t1	text1
t2	text2
t3	text3
t4	text4
enum1	b
set1	a,c
blob1	blob1
blob2	blob2
blob3	blob3
blob4	blob4
dd	2001-01-01 00:00:00
yy	2001
tm0	00:00:01
tm3	00:00:03.333
tm6	00:00:06.666666
dt0	2001-01-01 00:00:01
dt3	2001-01-03 00:00:01.333
dt6	2001-01-06 00:00:01.666666
ts0	2002-01-01 00:00:01
ts3	2002-01-03 00:00:01.333
ts6	2002-01-06 00:00:01.666666
DROP VIEW t1;
DROP TABLE t0;
DROP PROCEDURE p1;
#
# VIEW with subqueries
#
CREATE TABLE t1 (a INT,b INT);
INSERT INTO t1 VALUES (10,1),(20,2),(30,3),(40,4);
SELECT AVG(a) FROM t1;
AVG(a)
25.0000
CREATE VIEW v1 AS SELECT a,1 as b FROM t1 WHERE a>(SELECT AVG(a) FROM t1) AND b>(SELECT 1);
SELECT * FROM v1;
a	b
30	1
40	1
CREATE PROCEDURE p1
AS
a v1.a%TYPE := 10;
b v1.b%TYPE := 1;
BEGIN
SELECT a,b;
END;
$$
CALL p1;
a	b
10	1
DROP PROCEDURE p1;
CREATE FUNCTION f1 RETURN INT
AS
a v1.a%TYPE := 10;
b v1.b%TYPE := 1;
BEGIN
RETURN a+b;
END;
$$
SELECT f1();
f1()
11
DROP FUNCTION f1;
DROP VIEW v1;
DROP TABLE t1;
#
# %TYPE variables + INFORMATION_SCHEMA
#
CREATE PROCEDURE p1
AS
tables_table_name INFORMATION_SCHEMA.TABLES.TABLE_NAME%TYPE;
tables_table_rows INFORMATION_SCHEMA.TABLES.TABLE_ROWS%TYPE;
processlist_info INFORMATION_SCHEMA.PROCESSLIST.INFO%TYPE;
processlist_info_binary INFORMATION_SCHEMA.PROCESSLIST.INFO_BINARY%TYPE;
BEGIN
CREATE TABLE t1 AS SELECT
tables_table_name,
tables_table_rows,
processlist_info,
processlist_info_binary;
END;
$$
CALL p1();
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE "t1" (
  "tables_table_name" varchar(64) CHARACTER SET utf8 DEFAULT NULL,
  "tables_table_rows" bigint(21) unsigned DEFAULT NULL,
  "processlist_info" longtext CHARACTER SET utf8 DEFAULT NULL,
  "processlist_info_binary" blob DEFAULT NULL
)
DROP TABLE t1;
DROP PROCEDURE p1;
#
# %TYPE + Table structure change
# Data type for both a0 and a1 is chosen in the very beginning
#
CREATE PROCEDURE p1
AS
a0 t1.a%TYPE;
BEGIN
ALTER TABLE t1 MODIFY a VARCHAR(10); -- This does not affect a1
DECLARE
a1 t1.a%TYPE;
BEGIN
CREATE TABLE t2 AS SELECT a0, a1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
END;
END
$$
CREATE TABLE t1 (a INT);
CALL p1;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a0" int(11) DEFAULT NULL,
  "a1" int(11) DEFAULT NULL
)
DROP TABLE t1;
DROP PROCEDURE p1;
#
# %TYPE in parameters
#
CREATE TABLE t1 (a VARCHAR(10));
CREATE DATABASE test1;
CREATE TABLE test1.t1 (b SMALLINT);
CREATE PROCEDURE p1(a t1.a%TYPE, b test1.t1.b%TYPE)
AS
BEGIN
CREATE TABLE t2 AS SELECT a, b;
END;
$$
CALL p1('test', 123);
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" varchar(10) DEFAULT NULL,
  "b" smallint(6) DEFAULT NULL
)
SELECT * FROM t2;
a	b
test	123
DROP TABLE t2;
DROP PROCEDURE p1;
DROP TABLE test1.t1;
DROP DATABASE test1;
DROP TABLE t1;
#
# %TYPE in a stored function variables and arguments
#
CREATE TABLE t1 (a INT);
SET sql_mode=ORACLE;
CREATE FUNCTION f1 (prm t1.a%TYPE) RETURN INT
AS
a t1.a%TYPE:= prm;
BEGIN
RETURN a;
END;
$$
SELECT f1(20);
f1(20)
20
DROP FUNCTION f1;
DROP TABLE t1;
#
# %TYPE in function RETURN clause is not supported yet
#
CREATE FUNCTION f1 RETURN t1.a%TYPE
AS
BEGIN
RETURN 0;
END;
$$
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1.a%TYPE
AS
BEGIN
RETURN 0;
END' at line 1
#
# End of MDEV-10577 sql_mode=ORACLE: %TYPE in variable declarations
#
#
# MDEV-12089 sql_mode=ORACLE: Understand optional routine name after the END keyword
#
CREATE FUNCTION f1 RETURN INT AS
BEGIN
RETURN 10;
END f1;
$$
DROP FUNCTION f1;
CREATE FUNCTION test.f1 RETURN INT AS
BEGIN
RETURN 10;
END test.f1;
$$
DROP FUNCTION f1;
CREATE FUNCTION test.f1 RETURN INT AS
BEGIN
RETURN 10;
END test2.f1;
$$
ERROR HY000: END identifier 'test2.f1' does not match 'test.f1'
CREATE FUNCTION test.f1 RETURN INT AS
BEGIN
RETURN 10;
END test.f2;
$$
ERROR HY000: END identifier 'test.f2' does not match 'test.f1'
CREATE FUNCTION f1 RETURN INT AS
BEGIN
RETURN 10;
END test.f2;
$$
ERROR HY000: END identifier 'test.f2' does not match 'test.f1'
CREATE FUNCTION f1 RETURN INT AS
BEGIN
RETURN 10;
END test2.f1;
$$
ERROR HY000: END identifier 'test2.f1' does not match 'test.f1'
CREATE PROCEDURE p1 AS
BEGIN
NULL;
END p1;
$$
DROP PROCEDURE p1;
CREATE PROCEDURE test.p1 AS
BEGIN
NULL;
END test.p1;
$$
DROP PROCEDURE p1;
CREATE PROCEDURE test.p1 AS
BEGIN
NULL;
END test2.p1;
$$
ERROR HY000: END identifier 'test2.p1' does not match 'test.p1'
CREATE PROCEDURE test.p1 AS
BEGIN
NULL;
END test.p2;
$$
ERROR HY000: END identifier 'test.p2' does not match 'test.p1'
CREATE PROCEDURE p1 AS
BEGIN
NULL;
END test.p2;
$$
ERROR HY000: END identifier 'test.p2' does not match 'test.p1'
CREATE PROCEDURE p1 AS
BEGIN
NULL;
END test2.p1;
$$
ERROR HY000: END identifier 'test2.p1' does not match 'test.p1'
#
# MDEV-12107 sql_mode=ORACLE: Inside routines the CALL keywoard is optional
#
CREATE OR REPLACE PROCEDURE p1(a INT) AS
BEGIN
SELECT 'This is p1' AS "comment";
END;
/
CREATE OR REPLACE PROCEDURE p2 AS
BEGIN
SELECT 'This is p2' AS "comment";
END;
/
BEGIN
p1(10);
p2;
test.p1(10);
test.p2;
END;
/
comment
This is p1
comment
This is p2
comment
This is p1
comment
This is p2
CREATE PROCEDURE p3 AS
BEGIN
p1(10);
p2;
test.p1(10);
test.p2;
END
/
CALL p3;
comment
This is p1
comment
This is p2
comment
This is p1
comment
This is p2
DROP PROCEDURE p3;
DROP PROCEDURE p2;
DROP PROCEDURE p1;
#
# MDEV-12854 Synchronize CREATE..SELECT data type and result set metadata data type for INT functions
#
SELECT SQL%ROWCOUNT;
Catalog	Database	Table	Table_alias	Column	Column_alias	Type	Length	Max length	Is_null	Flags	Decimals	Charsetnr
def					SQL%ROWCOUNT	8	21	1	N	32897	0	63
SQL%ROWCOUNT
0
#
# MDEV-13686 EXCEPTION reserved keyword in SQL_MODE=oracle but not in Oracle itself
#
CREATE TABLE t1 (c1 int);
CREATE VIEW v1 AS SELECT c1 exception FROM t1;
SELECT exception FROM v1;
exception
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-14139 Anchored data types for variables
#
BEGIN NOT ATOMIC
DECLARE a a%TYPE;
END;
$$
ERROR 42000: Undeclared variable: a
DECLARE
int11 INT;
dec103 DECIMAL(10,3);
flt0 FLOAT;
dbl0 DOUBLE;
enum0 ENUM('a','b');
bit3 BIT(3);
varchar10 VARCHAR(10);
text1 TEXT;
tinytext1 TINYTEXT;
mediumtext1 MEDIUMTEXT;
longtext1 LONGTEXT;
time3 TIME(3);
datetime4 DATETIME(4);
timestamp5 TIMESTAMP(5);
date0 DATE;
a_int11 int11%TYPE;
a_dec103 dec103%TYPE;
a_flt0 flt0%TYPE;
a_dbl0 dbl0%TYPE;
a_bit3 bit3%TYPE;
a_enum0 enum0%TYPE;
a_varchar10 varchar10%TYPE;
a_text1 text1%TYPE;
a_tinytext1 tinytext1%TYPE;
a_mediumtext1 mediumtext1%TYPE;
a_longtext1 longtext1%TYPE;
a_time3 time3%TYPE;
a_datetime4 datetime4%TYPE;
a_timestamp5 timestamp5%TYPE;
a_date0 date0%TYPE;
aa_int11 a_int11%TYPE;
aa_dec103 a_dec103%TYPE;
aa_flt0 a_flt0%TYPE;
aa_dbl0 a_dbl0%TYPE;
aa_bit3 a_bit3%TYPE;
aa_enum0 a_enum0%TYPE;
aa_varchar10 a_varchar10%TYPE;
aa_text1 a_text1%TYPE;
aa_tinytext1 a_tinytext1%TYPE;
aa_mediumtext1 a_mediumtext1%TYPE;
aa_longtext1 a_longtext1%TYPE;
aa_time3 a_time3%TYPE;
aa_datetime4 a_datetime4%TYPE;
aa_timestamp5 a_timestamp5%TYPE;
aa_date0 a_date0%TYPE;
BEGIN
CREATE TABLE t1 AS
SELECT a_int11,a_dec103,a_flt0,a_dbl0,a_bit3,
a_enum0,a_varchar10,
a_text1,a_tinytext1,a_mediumtext1,a_longtext1,
a_time3,a_datetime4,a_timestamp5,a_date0;
SHOW CREATE TABLE t1;
DROP TABLE t1;
CREATE TABLE t1 AS
SELECT aa_int11,aa_dec103,aa_flt0,aa_dbl0,aa_bit3,
aa_enum0,aa_varchar10,
aa_text1,aa_tinytext1,aa_mediumtext1,aa_longtext1,
aa_time3,aa_datetime4,aa_timestamp5,aa_date0;
SHOW CREATE TABLE t1;
DROP TABLE t1;
END;
$$
Table	Create Table
t1	CREATE TABLE "t1" (
  "a_int11" int(11) DEFAULT NULL,
  "a_dec103" decimal(10,3) DEFAULT NULL,
  "a_flt0" float DEFAULT NULL,
  "a_dbl0" double DEFAULT NULL,
  "a_bit3" bit(3) DEFAULT NULL,
  "a_enum0" varchar(1) DEFAULT NULL,
  "a_varchar10" varchar(10) DEFAULT NULL,
  "a_text1" text DEFAULT NULL,
  "a_tinytext1" tinytext DEFAULT NULL,
  "a_mediumtext1" mediumtext DEFAULT NULL,
  "a_longtext1" longtext DEFAULT NULL,
  "a_time3" time(3) DEFAULT NULL,
  "a_datetime4" datetime(4) DEFAULT NULL,
  "a_timestamp5" timestamp(5) NULL DEFAULT NULL,
  "a_date0" datetime DEFAULT NULL
)
Table	Create Table
t1	CREATE TABLE "t1" (
  "aa_int11" int(11) DEFAULT NULL,
  "aa_dec103" decimal(10,3) DEFAULT NULL,
  "aa_flt0" float DEFAULT NULL,
  "aa_dbl0" double DEFAULT NULL,
  "aa_bit3" bit(3) DEFAULT NULL,
  "aa_enum0" varchar(1) DEFAULT NULL,
  "aa_varchar10" varchar(10) DEFAULT NULL,
  "aa_text1" text DEFAULT NULL,
  "aa_tinytext1" tinytext DEFAULT NULL,
  "aa_mediumtext1" mediumtext DEFAULT NULL,
  "aa_longtext1" longtext DEFAULT NULL,
  "aa_time3" time(3) DEFAULT NULL,
  "aa_datetime4" datetime(4) DEFAULT NULL,
  "aa_timestamp5" timestamp(5) NULL DEFAULT NULL,
  "aa_date0" datetime DEFAULT NULL
)
#
#  MDEV-11160 "Incorrect column name" when "CREATE TABLE t1 AS SELECT spvar"
#
CREATE TABLE t1 (x INT);
INSERT INTO t1 VALUES (10);
CREATE VIEW v1 AS SELECT x+1 AS a,x+1 AS b FROM t1;
CREATE PROCEDURE p1
AS
a INT := 1;
b INT := 2;
BEGIN
CREATE TABLE t2 AS SELECT a,b FROM v1;
SHOW CREATE TABLE t2;
SELECT * FROM t2;
DROP TABLE t2;
END;
$$
CALL p1();
Table	Create Table
t2	CREATE TABLE "t2" (
  "a" int(11) DEFAULT NULL,
  "b" int(11) DEFAULT NULL
)
a	b
1	2
DROP PROCEDURE p1;
DROP VIEW v1;
DROP TABLE t1;
#
# MDEV-14228 MariaDB crashes with function
#
CREATE TABLE t1 (c VARCHAR(16), KEY(c));
INSERT INTO t1 VALUES ('foo');
CREATE FUNCTION f1() RETURN VARCHAR(16)
IS
v VARCHAR2(16);
BEGIN
FOR v IN (SELECT DISTINCT c FROM t1)
LOOP
IF (v = 'bar') THEN
SELECT 1 INTO @a;
END IF;
END LOOP;
RETURN 'qux';
END $$
SELECT f1();
ERROR HY000: Illegal parameter data types row and varchar for operation '='
DROP FUNCTION f1;
CREATE FUNCTION f1() RETURN VARCHAR(16)
IS
v t1%ROWTYPE;
BEGIN
IF v = 'bar' THEN
NULL;
END IF;
RETURN 'qux';
END $$
SELECT f1();
ERROR HY000: Illegal parameter data types row and varchar for operation '='
DROP FUNCTION f1;
CREATE FUNCTION f1() RETURN VARCHAR(16)
IS
v ROW(a INT);
BEGIN
IF v = 'bar' THEN
NULL;
END IF;
RETURN 'qux';
END $$
SELECT f1();
ERROR HY000: Illegal parameter data types row and varchar for operation '='
DROP FUNCTION f1;
DROP TABLE t1;
DECLARE
v ROW(a INT);
BEGIN
SELECT v IN ('a','b');
END $$
ERROR HY000: Illegal parameter data types row and varchar for operation 'in'
DECLARE
v ROW(a INT);
BEGIN
SELECT 'a' IN (v,'b');
END $$
ERROR HY000: Illegal parameter data types varchar and row for operation 'in'
DECLARE
v ROW(a INT);
BEGIN
SELECT 'a' IN ('b',v);
END $$
ERROR HY000: Illegal parameter data types varchar and row for operation 'in'