Details

    Description

      Syntax:

      CURSOR <left paren> subquery <right paren>
      

      This example passes a cursor expression with a set of INTs to the function f1() which calculates the sum of all INTs in the set:

      DROP TABLE t1;
      CREATE TABLE t1 (a INT);
      INSERT INTO t1 VALUES (1);
      INSERT INTO t1 VALUES (2);
      INSERT INTO t1 VALUES (3);
      CREATE OR REPLACE FUNCTION f1(c SYS_REFCURSOR) RETURN INT
      AS
        v INT;
        t INT := 0;
      BEGIN
        LOOP
          FETCH c INTO v;
          EXIT WHEN c%NOTFOUND;
          t:= t + v;
        END LOOP;
        RETURN t;
      END;
      /
      CREATE OR REPLACE PROCEDURE p1
      AS
        t INT;
      BEGIN
        FOR i IN 1..3 LOOP
          SELECT f1(cursor(SELECT a FROM t1 WHERE a<=i)) INTO t FROM DUAL;
          DBMS_OUTPUT.PUT_LINE(' sum(t1.a<=' || i || ')=' || t);
        END LOOP;
      END;
      /
      CALL p1;
      

       sum(t1.a<=1)=1
       sum(t1.a<=2)=3
       sum(t1.a<=3)=6
      

      TODO:
      Add details (rules, restrictions, when the cursor gets closed, etc)

      Attachments

        Issue Links

          Activity

            bar Alexander Barkov created issue -
            bar Alexander Barkov made changes -
            Field Original Value New Value
            bar Alexander Barkov made changes -
            Description Syntax:
            {code:sql}
            CURSOR <left paren> subquery <right paren>
            {code}

            This example passes a cursor expression with a set of INTs to the function f1() which calculates the sum of all INTs in the set:

            {code:sql}
            DROP TABLE t1;
            CREATE TABLE t1 (a INT);
            INSERT INTO t1 VALUES (1);
            INSERT INTO t1 VALUES (2);
            INSERT INTO t1 VALUES (3);
            CREATE OR REPLACE FUNCTION f1(c SYS_REFCURSOR) RETURN INT
            AS
              v INT;
              t INT := 0;
            BEGIN
              LOOP
                FETCH c INTO v;
                EXIT WHEN c%NOTFOUND;
                t:= t + v;
              END LOOP;
              RETURN t;
            END;
            /
            CREATE OR REPLACE PROCEDURE p1
            AS
              t INT;
            BEGIN
              FOR i IN 1..3 LOOP
                SELECT f1(cursor(SELECT a FROM t1 WHERE a<=i)) INTO t FROM DUAL;
                DBMS_OUTPUT.PUT_LINE(' sum(1..' || i || ')=' || t);
              END LOOP;
            END;
            /
            CALL p1;
            {code}
            {noformat}
            sum(1..1)=1
            sum(1..2)=3
            sum(1..3)=6
            {noformat}

            TODO:
            Add details (rules, restrictions, when the cursor gets closed, etc)
            Syntax:
            {code:sql}
            CURSOR <left paren> subquery <right paren>
            {code}

            This example passes a cursor expression with a set of INTs to the function f1() which calculates the sum of all INTs in the set:

            {code:sql}
            DROP TABLE t1;
            CREATE TABLE t1 (a INT);
            INSERT INTO t1 VALUES (1);
            INSERT INTO t1 VALUES (2);
            INSERT INTO t1 VALUES (3);
            CREATE OR REPLACE FUNCTION f1(c SYS_REFCURSOR) RETURN INT
            AS
              v INT;
              t INT := 0;
            BEGIN
              LOOP
                FETCH c INTO v;
                EXIT WHEN c%NOTFOUND;
                t:= t + v;
              END LOOP;
              RETURN t;
            END;
            /
            CREATE OR REPLACE PROCEDURE p1
            AS
              t INT;
            BEGIN
              FOR i IN 1..3 LOOP
                SELECT f1(cursor(SELECT a FROM t1 WHERE a<=i)) INTO t FROM DUAL;
                DBMS_OUTPUT.PUT_LINE(' sum(t1.a<=' || i || ')=' || t);
              END LOOP;
            END;
            /
            CALL p1;
            {code}
            {noformat}
             sum(t1.a<=1)=1
             sum(t1.a<=2)=3
             sum(t1.a<=3)=6
            {noformat}

            TODO:
            Add details (rules, restrictions, when the cursor gets closed, etc)
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            bar Alexander Barkov made changes -
            Labels Compatibility Oracle
            ralf.gebhardt Ralf Gebhardt made changes -
            Parent MDEV-35973 [ 132608 ]
            Issue Type New Feature [ 2 ] Technical task [ 7 ]
            ralf.gebhardt Ralf Gebhardt made changes -
            ralf.gebhardt Ralf Gebhardt made changes -
            julien.fritsch Julien Fritsch made changes -
            Parent MDEV-35973 [ 132608 ]
            Issue Type Technical task [ 7 ] New Feature [ 2 ]
            julien.fritsch Julien Fritsch made changes -
            Priority Major [ 3 ] Minor [ 4 ]
            julien.fritsch Julien Fritsch made changes -
            Priority Minor [ 4 ] Major [ 3 ]

            People

              Unassigned Unassigned
              bar Alexander Barkov
              Votes:
              0 Vote for this issue
              Watchers:
              3 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.