Returning cursors in Oracle

17. October 2006 10:07 by Mrojas in General  //  Tags:   //   Comments (0)

I recenlty had a hard time trying to return some cursos and used them in Excel.

The problem is that Excel does not understand ref cursors. There are a couple of links in Microsoft. In general you have to use a special ODBC query syntax and create a package ????

This is an example, I just copied from Microsoft


      DROP TABLE DATA1;

      CREATE TABLE DATA1
       (ssn     NUMBER(9) PRIMARY KEY,
        fname   VARCHAR2(15),
        lname   VARCHAR2(20));

      INSERT INTO DATA1 VALUES(555662222,'Sam','Goodwin');

      INSERT INTO DATA1 VALUES(555882222,'Kent','Clark');

      INSERT INTO DATA1 VALUES(666223333,'Sally','Burnett');

      COMMIT;
      /

      CREATE OR REPLACE PACKAGE packData1
      AS
          TYPE tssn is TABLE of  NUMBER(10)
          INDEX BY BINARY_INTEGER;
          TYPE tfname is TABLE of VARCHAR2(15)
          INDEX BY BINARY_INTEGER;
          TYPE tlname is TABLE of VARCHAR2(20)
          INDEX BY BINARY_INTEGER;

        PROCEDURE GetData
              (param1 IN      Date,
               ssn    OUT     tssn,
               fname  OUT     tfname,
               lname  OUT     tlname);
      END packData1;
      /


      CREATE OR REPLACE PACKAGE BODY packData1
      AS

      PROCEDURE GetData
            (param1  IN    Date,
                   ssn     OUT   tssn,
                   fname   OUT   tfname,
                   lname   OUT   tlname)
      IS
        CURSOR data1_cur IS
                   SELECT ssn, fname, lname
                   FROM Data1
                   WHERE param1 < current_date;
           percount NUMBER DEFAULT 1;
       BEGIN
           FOR singledata IN data1_cur
           LOOP
                   ssn(percount) := singledata.ssn;
                   fname(percount) := singledata.fname;
                   lname(percount) := singledata.lname;
                   percount := percount + 1;
           END LOOP;
       END;
       END;
       / 
      
To call it you write in Excel something like {call packdata1.GetData('01-JAN-2005',{resultset 70000, ssn, fname, lname})}      

{call packdata1.GetData(?,{resultset 70000, ssn, fname, lname})}    to use parameters.

The microsoft links are:

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q174679

http://www.support.microsoft.com/kb/174981