Type:
Task
Priority:
Major
Resolution:
Fixed
This MDEV is a part of MDEV-12518 .
Under terms of this task we'll add Oracle-style FOR loop (both for integer ranges and cursors) into sql_mode=DEFAULT .
FOR loops will be a non-standard MariaDB extension.
We won't implement the SQL-standard cursor FOR loop yet, because it would need to open the cursor at parse time to know column names of the cursor.
Proposed syntax:
Integer range FOR loop:
[begin_label:]
FOR var_name IN [ REVERSE ] lower_bound .. upper_bound
DO statement_list
END FOR [ end_label ]
Explicit cursor FOR loop
[begin_label:]
FOR record_name IN cursor_name [ ( cursor_actual_parameter_list)]
DO statement_list
END FOR [ end_label ]
Implicit cursor FOR loop
[begin_label:]
FOR record_name IN ( select_statement )
DO statement_list
END FOR [ end_label ]
The proposed syntax is a compilation of the SQL standard syntax and Oracle's syntax for FOR loop:
It generally uses Oracle style syntax
however, like in the SQL standard, it uses FOR ..DO ..END FOR (instead or Oracle's FOR ..LOOP ..END LOOP )
Note, for cursor FOR loops, there is no a need to do OPEN , FETCH and CLOSE . These commands are done automatically. However, it will still be possible to fetch extra records inside the explicit cursor FOR loop body using explicit FETCH commands.
In sql_mode=DEFAULT , the automatic implicit FETCH which happens on FOR iterations and an explicit FETCH inside the loop body will work differently:
The automatic FETCH will never generate errors. On NOT FOUND , it will automatically close the cursor and leave the loop.
An explicit FETCH command inside the FOR loop will work like normal FETCH commands: i.e. it will generate an error on NOT FOUND , even when used with the FOR cursor.
This script uses an extra FETCH command inside the loop body:
DELIMITER $$
BEGIN NOT ATOMIC
DECLARE x INT ;
DECLARE cur CURSOR FOR SELECT 1 AS x;
FOR rec IN cur
DO
FETCH cur INTO x; -- Notice it uses "cur" (the loop cursor)
END FOR ;
END ;
$$
DELIMITER ;
It will return:
ERROR 02000: No data - zero rows fetched, selected, or processed
because the explicit FETCH command returns no rows: the first record is fetched automatically in the beginning of the FOR iteration, and there are no more records.
FETCH commands inside the FOR body will be normally handled by "NOT FOUND " handlers.
The following script uses a FETCH command with the FOR loop cursor, in combination with a CONTINUE HANDLER FOR NOT FOUND .
DELIMITER $$
BEGIN NOT ATOMIC
DECLARE done INT DEFAULT 0;
DECLARE cur CURSOR FOR SELECT 1 AS x, 'y1' AS y UNION
SELECT 2, 'y2' UNION
SELECT 3, 'y3' ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE ;
forrec:
FOR rec IN cur
DO
SELECT CONCAT(rec.x, ' ' , rec.y) AS 'Implicit FETCH' ;
FETCH cur INTO rec;
IF done THEN
SELECT 'NO DATA' AS `Explicit FETCH `;
LEAVE forrec;
ELSE
SELECT CONCAT(rec.x, ' ' , rec.y) AS 'Explicit FETCH' ;
END IF ;
END FOR ;
END ;
$$
DELIMITER ;
It will return without errors, with the following output:
Implicit FETCH
1 y1
Explicit FETCH
2 y2
Implicit FETCH
3 y3
Explicit FETCH
NO DATA
Notice:
some records are fetched using the automatic FETCH on FOR iterations
some records are fetched using the explicit FETCH command inside the loop body
the last explicit FETCH fails on NOT FOUND , which is caught by the CONTINUE handler, and the error is suppressed.
Transition
Time In Source Status
Execution Times
Open
Closed
7h 59m
1
{"report":{"fcp":1050.2999999523163,"ttfb":283.10000014305115,"pageVisibility":"visible","entityId":64326,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"1e93e4f4-8c8a-490e-a00d-9bd9b4998094","navigationType":0,"readyForUser":1185.1000001430511,"redirectCount":0,"resourceLoadedEnd":1247.1000001430511,"resourceLoadedStart":289,"resourceTiming":[{"duration":186,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":289,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":289,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":475,"responseStart":0,"secureConnectionStart":0},{"duration":186,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":289.2999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":289.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":475.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":197.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":289.5,"connectEnd":289.5,"connectStart":289.5,"domainLookupEnd":289.5,"domainLookupStart":289.5,"fetchStart":289.5,"redirectEnd":0,"redirectStart":0,"requestStart":289.5,"responseEnd":487,"responseStart":487,"secureConnectionStart":289.5},{"duration":279.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":289.7000000476837,"connectEnd":289.7000000476837,"connectStart":289.7000000476837,"domainLookupEnd":289.7000000476837,"domainLookupStart":289.7000000476837,"fetchStart":289.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":289.7000000476837,"responseEnd":568.7999999523163,"responseStart":568.7999999523163,"secureConnectionStart":289.7000000476837},{"duration":283.2000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":289.90000009536743,"connectEnd":289.90000009536743,"connectStart":289.90000009536743,"domainLookupEnd":289.90000009536743,"domainLookupStart":289.90000009536743,"fetchStart":289.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":289.90000009536743,"responseEnd":573.1000001430511,"responseStart":573,"secureConnectionStart":289.90000009536743},{"duration":283.39999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":290.10000014305115,"connectEnd":290.10000014305115,"connectStart":290.10000014305115,"domainLookupEnd":290.10000014305115,"domainLookupStart":290.10000014305115,"fetchStart":290.10000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":290.10000014305115,"responseEnd":573.5,"responseStart":573.5,"secureConnectionStart":290.10000014305115},{"duration":283.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":290.2999999523163,"connectEnd":290.2999999523163,"connectStart":290.2999999523163,"domainLookupEnd":290.2999999523163,"domainLookupStart":290.2999999523163,"fetchStart":290.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":290.2999999523163,"responseEnd":573.7999999523163,"responseStart":573.7999999523163,"secureConnectionStart":290.2999999523163},{"duration":345.2000000476837,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":290.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":290.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":635.6000001430511,"responseStart":0,"secureConnectionStart":0},{"duration":283.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":290.60000014305115,"connectEnd":290.60000014305115,"connectStart":290.60000014305115,"domainLookupEnd":290.60000014305115,"domainLookupStart":290.60000014305115,"fetchStart":290.60000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":290.60000014305115,"responseEnd":574.2999999523163,"responseStart":574.2999999523163,"secureConnectionStart":290.60000014305115},{"duration":345.10000014305115,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":290.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":290.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":635.9000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":284.10000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":290.7999999523163,"connectEnd":290.7999999523163,"connectStart":290.7999999523163,"domainLookupEnd":290.7999999523163,"domainLookupStart":290.7999999523163,"fetchStart":290.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":290.7999999523163,"responseEnd":574.9000000953674,"responseStart":574.9000000953674,"secureConnectionStart":290.7999999523163},{"duration":584.0999999046326,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":296.40000009536743,"connectEnd":296.40000009536743,"connectStart":296.40000009536743,"domainLookupEnd":296.40000009536743,"domainLookupStart":296.40000009536743,"fetchStart":296.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":296.40000009536743,"responseEnd":880.5,"responseStart":880.5,"secureConnectionStart":296.40000009536743},{"duration":698,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":296.40000009536743,"connectEnd":296.40000009536743,"connectStart":296.40000009536743,"domainLookupEnd":296.40000009536743,"domainLookupStart":296.40000009536743,"fetchStart":296.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":296.40000009536743,"responseEnd":994.4000000953674,"responseStart":994.4000000953674,"secureConnectionStart":296.40000009536743},{"duration":225.20000004768372,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":655.7000000476837,"connectEnd":655.7000000476837,"connectStart":655.7000000476837,"domainLookupEnd":655.7000000476837,"domainLookupStart":655.7000000476837,"fetchStart":655.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":655.7000000476837,"responseEnd":880.9000000953674,"responseStart":880.9000000953674,"secureConnectionStart":655.7000000476837},{"duration":109.20000004768372,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/css/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":934.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":934.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1044,"responseStart":0,"secureConnectionStart":0},{"duration":108.79999995231628,"initiatorType":"link","name":"https://jira.mariadb.org/s/50bc9be5bfead1a25e72c1a9338c94f6-CDN/lu2cib/820016/12ta74/e108c7645258ccb43280ed3404e3e949/_/download/contextbatch/css/com.atlassian.jira.plugins.jira-development-integration-plugin:0,-_super,-jira.view.issue,-jira.global,-jira.general,-jira.browse.project,-project.issue.navigator,-atl.general/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":935.4000000953674,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":935.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1044.2000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":306.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-project.issue.navigator,-jira.view.issue,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":936.4000000953674,"connectEnd":936.4000000953674,"connectStart":936.4000000953674,"domainLookupEnd":936.4000000953674,"domainLookupStart":936.4000000953674,"fetchStart":936.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":936.4000000953674,"responseEnd":1242.5,"responseStart":1242.5,"secureConnectionStart":936.4000000953674},{"duration":308.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/097ae97cb8fbec7d6ea4bbb1f26955b9-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/js/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":936.9000000953674,"connectEnd":936.9000000953674,"connectStart":936.9000000953674,"domainLookupEnd":936.9000000953674,"domainLookupStart":936.9000000953674,"fetchStart":936.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":936.9000000953674,"responseEnd":1245.5,"responseStart":1245.5,"secureConnectionStart":936.9000000953674},{"duration":309.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/e0bf5781d46ea69fb123572974cf39de-CDN/lu2cib/820016/12ta74/e108c7645258ccb43280ed3404e3e949/_/download/contextbatch/js/com.atlassian.jira.plugins.jira-development-integration-plugin:0,-_super,-jira.view.issue,-jira.global,-jira.general,-jira.browse.project,-project.issue.navigator,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":937.2000000476837,"connectEnd":937.2000000476837,"connectStart":937.2000000476837,"domainLookupEnd":937.2000000476837,"domainLookupStart":937.2000000476837,"fetchStart":937.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":937.2000000476837,"responseEnd":1247.1000001430511,"responseStart":1247.1000001430511,"secureConnectionStart":937.2000000476837}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":81,"responseStart":283,"responseEnd":288,"domLoading":287,"domInteractive":1276,"domContentLoadedEventStart":1276,"domContentLoadedEventEnd":1340,"domComplete":2072,"loadEventStart":2072,"loadEventEnd":2072,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1244.2999999523163},{"name":"bigPipe.sidebar-id.end","time":1245.2999999523163},{"name":"bigPipe.activity-panel-pipe-id.start","time":1245.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.end","time":1246.2999999523163},{"name":"activityTabFullyLoaded","time":1359.1000001430511}],"measures":[],"correlationId":"23826b144cdf78","effectiveType":"4g","downlink":9.2,"rtt":0,"serverDuration":126,"dbReadsTimeInMs":32,"dbConnsTimeInMs":42,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}