[MDEV-14415] Add Oracle-style FOR loop to sql_mode=DEFAULT Created: 2017-11-16 Updated: 2023-10-04 Resolved: 2017-11-16 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Parser |
| Fix Version/s: | 10.3.3 |
| Type: | Task | Priority: | Major |
| Reporter: | Alexander Barkov | Assignee: | Alexander Barkov |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | Compatibility | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||
| Description |
|
This MDEV is a part of 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:
The proposed syntax is a compilation of the SQL standard syntax and Oracle's syntax for FOR 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:
This script uses an extra FETCH command inside the loop body:
It will return:
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.
It will return without errors, with the following output:
Notice:
|
| Comments |
| Comment by Robert Dyas [ 2018-07-03 ] | ||||||||||||||||||
|
I can't find this in the MariaDB documentation... is it there and I just missed it? Especially useful to note in the docs that 10.3.3+ has ANSI SQL cursor FOR loop with simple cursor example. So much easier than the exiting way for 99% of my uses cases. | ||||||||||||||||||
| Comment by Sergei Golubchik [ 2018-07-03 ] | ||||||||||||||||||
|
MariaDB 10.3 does not support ANSI SQL cursor FOR. It's kind-of-like-in-Oracle cursor FOR. Functionality is pretty much the same, syntax is a bit different. Still, strictly speaking, not standard. | ||||||||||||||||||
| Comment by Alexander Barkov [ 2018-07-04 ] | ||||||||||||||||||
|
The documentation is currently in progress: In the meanwhile please have a look into this https://github.com/MariaDB/server/blob/10.3/mysql-test/main/sp-for-loop.test | ||||||||||||||||||
| Comment by Robert Dyas [ 2018-07-04 ] | ||||||||||||||||||
|
In the documentation if you could clarify what happens with rec.col references if col is ambiguous from a join... is it simply rec.table.col as you would guess? Example:
| ||||||||||||||||||
| Comment by Alexander Barkov [ 2018-07-05 ] | ||||||||||||||||||
|
I tested this script:
and it correctly returns this error:
The correct syntax is to give unique names to columns using AS, e.g. like this:
|