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

Native Dynamic SQL

Native Dynamic SQL (NDS) is also generally able to take advantage of soft-closed cursors and cursor reuse, but it does best when bind variables are used. Consider the following two procedures; they do the same thing except that one uses bind variables while the other uses concatenation.

    CREATE OR REPLACE PROCEDURE bind ( p_on NUMBER ) AS
      v_od DATE;
    BEGIN
      EXECUTE IMMEDIATE 'SELECT order_date ' ||
                         ' FROM orders '     ||
                        ' WHERE order_number = :v_on'
              INTO v_od
              USING p_on;
    END;

    CREATE OR REPLACE PROCEDURE concatenate ( p_on NUMBER ) AS
      v_od DATE;
    BEGIN
      EXECUTE IMMEDIATE 'SELECT order_date ' ||
                         ' FROM orders '     ||
                        ' WHERE order_number = ' || p_on
              INTO v_od;
    END;

First, I’ll execute the bind version three times:

    SQL> BEGIN
      2    FOR counter IN 1..3 LOOP
      3      bind(counter);
      4    END LOOP;
      5  END;
      6  /

    PL/SQL procedure successfully completed.

The open cursors list shows one very familiar cursor:

    SELECT order_date  FROM orders
      WHERE order_number = :v_on

The parse and execution counts are, as expected, 1 and 3:

    SQL_TEXT                       PARSE_CALLS EXECUTIONS
    ------------------------------ ----------- ----------
    SELECT order_date  FROM orders           1          3
      WHERE order_number = :v_on

Now I’ll run the concatenation version three times.

    SQL> BEGIN
      2    FOR counter IN 1..3 LOOP
      3      concatenate(counter);
      4    END LOOP;
      5  END;
      6  /

    PL/SQL procedure successfully completed.

The open cursor list looks like this because only the very last one is kept around, hoping for re-execution.

 SQL_TEXT ------------------------------ SELECT order_date ...
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