1

MDEV-15941

 3 years ago
source link: https://jira.mariadb.org/browse/MDEV-15941
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Explicit cursor FOR loop does not close the cursor

Details

Description

I run this script:

SET sql_mode=ORACLE;
DELIMITER $$
DECLARE
  CURSOR cur IS SELECT 1 AS a FROM DUAL;
  v INT;
BEGIN
  FOR rec IN cur
  LOOP
    NULL;
  END LOOP;
  FETCH cur INTO v;
  CLOSE cur;
END;
$$
DELIMITER ;

It ends successfully. This is wrong. The FOR loop for an explicit cursor should close the cursor. In fact it does not seem to, so FETCH and CLOSE are still possible with this cursor after the loop.

In Oracle, the cursor is in closed state after the loop. So attempts to use it with FETCH (or CLOSE) after the loop return an error:

DECLARE
  CURSOR cur IS SELECT 1 AS a FROM DUAL;
  v INT;
BEGIN
  FOR rec IN cur
  LOOP
    NULL;
  END LOOP;
  FETCH cur INTO v;
END;
/
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at line 9

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK