Skip to Main Content
Oracle PL/SQL for DBAs
book

Oracle PL/SQL for DBAs

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

Establishing Time Limits

One very useful application of table functions is to use them to establish time limits for returning records from queries. This is great if you want to test an application using a subset of queried records without having to wait for the whole list. The following function pipes records back from a query for the number of seconds passed in. Once the number of seconds is reached, a value of negative 1 is piped out and the function is exited.

    /* File on web: time_limit.sql */
    CREATE OR REPLACE FUNCTION get_a_table
              ( p_limit NUMBER )
              RETURN rowset_t
              PIPELINED AS
      CURSOR curs_get_a IS
      SELECT *
        FROM a_table;
      v_start DATE;
    BEGIN
      v_start := SYSDATE;
      FOR v_a_rec IN curs_get_a LOOP
        PIPE ROW(rowset_o(v_a_rec.col1));
        IF SYSDATE - v_start >= ( p_limit * 0.000011574 ) THEN
          PIPE ROW(rowset_o(-1));
          EXIT;
        END IF;
      END LOOP;
    END;

Here’s an example of selecting from a table with 1,000 records in it.

    SQL> SELECT *
      2    FROM TABLE(get_a_table(1));

          COL1
    ----------
           661
           662
           663
           664
            -1

    5 rows selected.

If the query takes more than p_limit seconds to return, the function will exceed its time limit.

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 PL/SQL Best Practices

Oracle PL/SQL Best Practices

Steven Feuerstein
Expert Oracle PL/SQL

Expert Oracle PL/SQL

Ron Hardman, Michael McLaughlin
Oracle PL/SQL For Dummies

Oracle PL/SQL For Dummies

Michael Rosenblum, Paul Dorsey

Publisher Resources

ISBN: 0596005873Supplemental ContentErrata Page