Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-33830

Support for cursors on prepared statements

    XMLWordPrintable

Details

    Description

      Currently prepared statements can only be executed and only a single row may be fetched. This is a severe limitation, in particular when it comes to stored procedures. This limits the support for Dynamic SQL we have and makes migration from, say, Oracle, real difficult. Oracle has support for cursors on Dynamic SQL.

      This is a suggested syntax:

      CREATE OR REPLACE PROCEDURE do_select1(tab VARCHAR(64), id INTEGER)
      BEGIN
         DECLARE no_data BOOL DEFAULT FALSE;
         DECLARE v_c1 VARCHAR(100);
         DECLARE c1 CURSOR;
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_data = TRUE;
       
         PREPARE s1 FROM CONCAT('SELECT c1 FROM ', tab, ' WHERE id = ?');
         OPEN c1 FOR s1 USING id;
         LOOP
             FETCH c1 INTO v_c1;
             INSERT INTO othertable VALUES(v_c1);
         END LOOP;
         CLOSE c1;
      END;

      Cursor FOR-loops should also be supported:

      CREATE OR REPLACE PROCEDURE do_select2(tab VARCHAR(64), id INTEGER)
      BEGIN
         DECLARE v_c1 VARCHAR(100);
         DECLARE c1 CURSOR;
       
         PREPARE s1 FROM CONCAT('SELECT c1 FROM ', tab, ' WHERE id = ?');
       
         FOR v_c1 IN c1(id) FOR s1
         LOOP
             INSERT INTO othertable VALUES(v_c1);
         END LOOP;
      END;

      Attachments

        Issue Links

          Activity

            People

              bar Alexander Barkov
              karlsson Anders Karlsson
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.