Skip to Content
Oracle PL/SQL for DBAs
book

Oracle PL/SQL for DBAs

by Arup Nanda, Steven Feuerstein
October 2005
Intermediate to advanced
454 pages
14h 44m
English
O'Reilly Media, Inc.
Content preview from Oracle PL/SQL for DBAs

Cursors

We looked carefully at cursors in Chapter 2, and here they are again! How do cursors and table functions interact? Let’s start by looking at a non-pipelined version of a table function.

    /* File on web: date_parser.sql */
    CREATE OR REPLACE FUNCTION date_parse ( p_curs SYS_REFCURSOR )
                      RETURN order_date_t AS

      v_order_rec orders%ROWTYPE;
      v_ret_val   order_date_t := order_date_t(  );

    BEGIN

      -- for every order in the cursor...
      LOOP
        FETCH p_curs INTO v_order_rec;
        EXIT WHEN p_curs%NOTFOUND;

        -- extend the array by 3 and populate with cmoponents of the
        -- orders creation, assignment and close date
        v_ret_val.EXTEND(3);
        v_ret_val(v_ret_val.LAST - 2) := order_date_o(v_order_rec.order_number,
                                                      'O',
                                        TO_CHAR(v_order_rec.create_date,'YYYY'),
                                        TO_CHAR(v_order_rec.create_date,'Q'),
                                        TO_CHAR(v_order_rec.create_date,'MM'));
        v_ret_val(v_ret_val.LAST - 1) := order_date_o(v_order_rec.order_number,
                                                      'A',
                                        TO_CHAR(v_order_rec.assign_date,'YYYY'),
                                        TO_CHAR(v_order_rec.assign_date,'Q'),
                                        TO_CHAR(v_order_rec.assign_date,'MM'));
        v_ret_val(v_ret_val.LAST) := order_date_o(v_order_rec.order_number,
                                                  'C',
                                        TO_CHAR(v_order_rec.close_date,'YYYY'),
                                        TO_CHAR(v_order_rec.close_date,'Q'),
                                        TO_CHAR(v_order_rec.close_date,'MM'));
      END LOOP;  -- every order in ths cursor

      RETURN(v_ret_val);

    END;

And here are the results when three orders are queried.

 ORDER_NUMBER D YEAR QUARTER MONTH ------------ - ---------- ---------- ---------- 1 O 2005 3 8 1 A 2005 3 8 1 C 2005 3 8 2 O 2005 4 10 2 A 2005 4 10 2 C 2005 4 10 3 O 2005 4 12 3 A 2005 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Oracle Database 12c PL/SQL Programming

Oracle Database 12c PL/SQL Programming

Michael McLaughlin
Expert PL/SQL Practices for Oracle Developers and DBAs

Expert PL/SQL Practices for Oracle Developers and DBAs

John Beresniewicz, Adrian Billington, Martin Büchi, Melanie Caffrey, Ron Crisco, Lewis Cunningham, Dominic Delmolino, Sue Harper, Torben Holm, Connor McDonald, Arup Nanda, Stephan Petit, Michael Rosenblum, Robyn Sands, Riyaj Shamsudeen

Publisher Resources

ISBN: 0596005873Supplemental ContentErrata Page