§ Oracle Compatibility - Stored Procedure - CURSOR
§ 1. Syntax
1. [ FOR rows IN
{ cursor [ ( cursor_parameter_dec
[ [,] cursor_parameter_dec ]... )]
| ( select_statement )
}
LOOP statement... END LOOP [label] ;
2. FETCH CURSOR INTO var
3. CURSOR cursor_name (column_name datatype) IS ...
4. cursor_name CURSOR%ROWTYPE
5. cursor_name TABLE%ROWTYPE
2
3
4
5
6
7
8
9
10
11
12
13
14
§ 2. Definition and Usage
GreatSQL supports the following cursor usages in ORACLE mode:
- Usage 1: Use the
FOR ... IN CURSOR() LOOP ... END LOOPsyntax to read data into a cursor in a loop.
- Usage 1: Use the
[ FOR rows IN
{ cursor [ ( cursor_parameter_dec
[ [,] cursor_parameter_dec ]... )]
| ( select_statement )
}
LOOP statement... END LOOP [label] ;
2
3
4
5
6
- Usage 2: Read multiple columns from a cursor and assign them to a variable, implementing the
%ROWTYPEfeature.
- Usage 2: Read multiple columns from a cursor and assign them to a variable, implementing the
FETCH CURSOR INTO var
- Usage 3: Cursors can be defined with parameters, and opened with
OPEN CURSOR(var_list).
- Usage 3: Cursors can be defined with parameters, and opened with
CURSOR cursor_name (column_name datatype) IS ...
- Usage 4: Parameters can be defined as
CURSOR%ROWTYPEtype, used to store multiple columns.
- Usage 4: Parameters can be defined as
cursor_name CURSOR%ROWTYPE
- Usage 5: Parameters can be defined as
TABLE%ROWTYPEtype, used to store multiple columns.
- Usage 5: Parameters can be defined as
cursor_name TABLE%ROWTYPE
§ 3. Oracle Compatibility Notes
The way GreatSQL reads data with cursors in stored procedures in ORACLE mode is basically consistent with Oracle; only %ROWCOUNT is not yet supported.
In Oracle, the FOR var_name IN cursor_name LOOP ... END LOOP and %ROWTYPE syntax are supported for fetching values from a cursor, but in GreatSQL the usage is LOOP ... END LOOP.
Other details about cursor usage are described below:
Both
DECLARE CURSOR c IS ...andDECLARE CURSOR(a INT) c IS ...are supported for declaring cursors.FOR var_name IN CURSOR LOOPcan be used to read data; parentheses after the cursor name are optional.CURSOR.column_nameis supported to obtain the data of a specified column.CURSOR%FOUND,CURSOR%NOTFOUNDandCURSOR%ISOPENare supported to check the cursor status.CURSOR%TYPEis supported to inherit the%ROWTYPEtype of a cursor.TABLE%ROWTYPEis supported to obtain the structure of an existing table.A table containing
UDTtypes can be defined as a cursor.SELECT INTOcan also be used forUDTfields.In the statement block
FOR var IN CURSOR() LOOP ... END LOOP, the variablevaris only valid within theFOR LOOP ... END LOOPstatement block.
§ 4. Examples
Set the sql_generate_invisible_primary_key option because the tables created in the following examples have no explicit primary key; disabling this option avoids automatically creating the implicit primary key my_row_id, which might affect the following examples.
greatsql> SET SESSION sql_generate_invisible_primary_key = 0;
Create test tables and initialize data
greatsql> DROP TABLE IF EXISTS t;
greatsql> CREATE TABLE IF NOT EXISTS t(id INT NOT NULL, c1 VARCHAR(100) NOT NULL);
greatsql> INSERT INTO t VALUES(1, 'row1'), (2, 'row2'), (3, 'row3');
2
3
- Example 1:
FOR LOOPreads data in a loop
- Example 1:
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE sp1_cur_loop() AS
CURSOR cur1 IS SELECT * FROM t;
BEGIN
FOR rec IN cur1() LOOP
SELECT rec.id, rec.c1;
END LOOP;
END; //
greatsql> CALL sp1_cur_loop() //
+--------+--------+
| rec.id | rec.c1 |
+--------+--------+
| 1 | row1 |
+--------+--------+
1 row in set (0.00 sec)
+--------+--------+
| rec.id | rec.c1 |
+--------+--------+
| 2 | row2 |
+--------+--------+
1 row in set (0.00 sec)
+--------+--------+
| rec.id | rec.c1 |
+--------+--------+
| 3 | row3 |
+--------+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
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
- Example 2:
CURSOR%ROWTYPEinherits a data type
- Example 2:
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE sp2_cur_rowtype() AS
CURSOR cur1 IS SELECT * FROM t;
rec1 cur1%ROWTYPE; -- Inherit the data type with CURSOR%ROWTYPE
rec2 t.id%TYPE; -- Inherit the data type with TABLE%TYPE
BEGIN
OPEN cur1; -- Open the cursor
LOOP -- Loop over the cursor until all data is read
FETCH cur1 INTO rec1;
EXIT WHEN cur1%NOTFOUND;
rec2 := 1; -- Assign a value to the rec2 variable separately
SELECT rec1.id, rec1.c1, rec2;
END LOOP;
CLOSE cur1;
END; //
greatsql> CALL sp2_cur_rowtype() //
+---------+---------+------+
| rec1.id | rec1.c1 | rec2 |
+---------+---------+------+
| 1 | row1 | 1 |
+---------+---------+------+
1 row in set (0.00 sec)
+---------+---------+------+
| rec1.id | rec1.c1 | rec2 |
+---------+---------+------+
| 2 | row2 | 1 |
+---------+---------+------+
1 row in set (0.00 sec)
+---------+---------+------+
| rec1.id | rec1.c1 | rec2 |
+---------+---------+------+
| 3 | row3 | 1 |
+---------+---------+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
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
- Example 3: define a cursor with parameters
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE PROCEDURE sp3_cur_var() AS
vid INT;
-- Define a cursor with parameters
CURSOR cur1 (vid INT) IS SELECT * FROM t WHERE id = vid;
rec1 cur1%ROWTYPE;
BEGIN
OPEN cur1(2); -- Pass parameters when opening the cursor
LOOP
FETCH cur1 INTO rec1;
EXIT WHEN cur1%NOTFOUND;
SELECT rec1.id, rec1.c1;
END LOOP;
CLOSE cur1;
END; //
greatsql> CALL sp3_cur_var() //
+---------+---------+
| rec1.id | rec1.c1 |
+---------+---------+
| 2 | row2 |
+---------+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
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
- Example 4: a table containing
UDTtypes is supported
- Example 4: a table containing
greatsql> SET sql_mode = ORACLE;
greatsql> DELIMITER //
greatsql> CREATE OR REPLACE TYPE udt1 AS OBJECT(id INT, c1 VARCHAR(20));
greatsql> CREATE TABLE udt_t1(id INT,c2 udt1);
greatsql> INSERT INTO udt_t1 VALUES(1, udt1(10, 'c1_row10'));
greatsql> INSERT INTO udt_t1 VALUES(2, udt1(20, 'c1_row20'));
greatsql> SET udt_format_result = 'DBA';
greatsql> CREATE OR REPLACE PROCEDURE sp4_cur_udt() AS
CURSOR cur1 (vid INT) IS SELECT * FROM udt_t1 WHERE id = vid;
rec1 udt_t1.c2%TYPE;
BEGIN
FOR rec IN cur1(10) LOOP
SELECT rec.id, rec.c2;
SELECT rec.c2 INTO rec1;
SELECT rec1.id, rec1.c1;
END LOOP;
SELECT * FROM udt_t1;
END; //
greatsql> CALL sp4_cur_udt(); //
+------+---------------------+
| id | c2 |
+------+---------------------+
| 1 | id:10 | c1:c1_row10 |
| 2 | id:20 | c1:c1_row20 |
+------+---------------------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
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
Scan the QR code to follow our WeChat official account
