Articles 11g Plsql New Features and Enhancements

Embed Size (px)

Citation preview

  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    1/22

    Home Articles Scripts Forums Blog Certificatio n Misc Se arch About Printer Friendly

    Search

    Oracle 8i | Oracle 9i | Oracle 10g | Orac le 11g | Oracle 1 2c | Misce llaneous | PL/SQL | SQL | Oracle RAC | Oracle Apps | Linux

    Home Articles 11g Here

    PL/SQL New Features and Enhancements in Oracle Database 11g

    Release 1

    Oracle 11g includes a substantial number o f PL/SQL new fe atures and enhance me nts. In orde r to preve nt this

    article becoming to o large som e o f these features have be en split of f into separate articles, but the fo llowingse ctions re prese nt an explanation o f the all the new fe atures liste d in the What's New in PL/SQL? sec tion of the

    PL/SQL Languag e Refere nce manual.

    Topics covered in this article:

    Enhancements to Regular Expression Built-in SQL Functions

    SIMPLE_INTEGER DatatypeCONTINUE Statement

    Sequences in PL/SQL Expressions

    Dynamic SQL Enhancem ents

    Generalized InvocationName d and Mixed Notation in PL/SQL Subprogram Invocations

    Autom atic Subprog ram Inlining

    PL/ScopePL/SQLNative Co mpiler Generates Native Code Directly

    Ne w PL/SQL Compiler Warning

    PLS-00436 Restriction in FORALL Statements Removed

    Topics c ove red in separate articles o n this site:

    Cross -Sess ion PL/SQL Function Res ult Cac heTrigger Enhancements in Oracle Database 11g Release 1

    PL/SQL Hierarchical Profiler

    Native Orac le XML DB Web Se rvice sDatabase Resident Co nnection Poo l

    Enhancements to Regular Expression Built-in SQL Functions

    Ads by Google Oracle Oracle 1

    PDFmyURL.com

    http://www.oracle-base.com/articles/11g/native-oracle-xml-db-web-services-11gr1.phphttp://www.oracle-base.com/articles/11g/cross-session-plsql-function-result-cache-11gr1.phphttp://www.oracle-base.com/articles/11g/cross-session-plsql-function-result-cache-11gr1.phphttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#automatic_subprogram_inlininghttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#generalized_invocationhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#continue_statementhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#simple_integerhttp://www.oracle-base.com/articleshttp://www.oracle-base.com/articles/11ghttp://www.oracle-base.com/articles/8i/articles-8i.phphttp://www.oracle-base.com/articles/9i/articles-9i.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/forums/http://www.oracle-base.com/blog/http://www.oracle-base.com/misc/ocp-certification.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/index.phphttp://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions135.htm#SQLRF06303http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions133.htm#SQLRF06300http://www.oracle-base.com/articles/11g/database-resident-connection-pool-11gr1.phphttp://www.oracle-base.com/articles/11g/native-oracle-xml-db-web-services-11gr1.phphttp://www.oracle-base.com/articles/11g/plsql-hierarchical-profiler-11gr1.phphttp://www.oracle-base.com/articles/11g/trigger-enhancements-11gr1.phphttp://www.oracle-base.com/articles/11g/cross-session-plsql-function-result-cache-11gr1.phphttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#forall_restriction_removedhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#new_plsql_compiler_warninghttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#native_compilationhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#plscopehttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#automatic_subprogram_inlininghttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#named_and_mixed_notation_in_sqlhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#generalized_invocationhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#dynamic_sql_enhancementshttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#sequences_in_plsql_expressionshttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#continue_statementhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#simple_integerhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#ehancements_to_regular_expressionshttp://docs.oracle.com/cd/B28359_01/appdev.111/b28370/whatsnew.htm#CJAEGHHHhttp://www.oracle-base.com/articles/11ghttp://www.oracle-base.com/articleshttp://www.oracle-base.com/http://www.oracle-base.com/articles/linux/articles-linux.phphttp://www.oracle-base.com/articles/apps/articles-apps.phphttp://www.oracle-base.com/articles/rac/articles-rac.phphttp://www.oracle-base.com/articles/sql/articles-sql.phphttp://www.oracle-base.com/articles/plsql/articles-plsql.phphttp://www.oracle-base.com/articles/misc/articles-misc.phphttp://www.oracle-base.com/articles/12c/articles-12c.phphttp://www.oracle-base.com/articles/11g/articles-11g.phphttp://www.oracle-base.com/articles/10g/articles-10g.phphttp://www.oracle-base.com/articles/9i/articles-9i.phphttp://www.oracle-base.com/articles/8i/articles-8i.phphttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php?display_type=printablehttp://www.oracle-base.com/misc/site-info.phphttp://www.oracle-base.com/searchhttp://www.oracle-base.com/misc/miscellaneous.phphttp://www.oracle-base.com/misc/ocp-certification.phphttp://www.oracle-base.com/blog/http://www.oracle-base.com/forums/http://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/index.php
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    2/22

    The REGEXP_INSTRand REGEXP_SUBSTRfunctions include a new SUBEXPR parameter that limits the pattern match to a specific subexpression in the search pattern.

    SQL> SELECT REGEXP_INSTR('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 1) FROM dual;

    REGEXP_INSTR('1234567890','(123)(4(56)(78))',1,1,0,'I',1)---------------------------------------------------------

    1

    1 row selected.

    SQL> SELECT REGEXP_INSTR('1234567890', '(123)(4(56)(78))', 1, 1, 0, 'i', 3) FROM dual;

    REGEXP_INSTR('1234567890','(123)(4(56)(78))',1,1,0,'I',3)---------------------------------------------------------

    5

    1 row selected.

    SQL> SELECT REGEXP_SUBSTR('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 0) FROM dual;

    REGEXP_S--------

    12345678

    1 row selected.

    SQL> SELECT REGEXP_SUBSTR('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 1) FROM dual;

    REG---123

    1 row selected.

    SQL> SELECT REGEXP_SUBSTR('1234567890', '(123)(4(56)(78))', 1, 1, 'i', 3) FROM dual;

    RE--56

    1 row selected.

    SQL>

    The new REGEXP_COUNT function returns the number of times the search pattern appears in source string.

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions132.htm#SQLRF20014
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    3/22

    SQL> SELECT REGEXP_COUNT('123 123 123 123', '123', 1, 'i') FROM dual;

    REGEXP_COUNT('123123123123','123',1,'I')----------------------------------------

    4

    1 row selected.

    SQL> SELECT REGEXP_COUNT('123 123 123 123', '123', 9, 'i') FROM dual;

    REGEXP_COUNT('123123123123','123',9,'I')----------------------------------------

    2

    1 row selected.

    SQL>

    SIMPLE_INTEGER Datatype

    The SIMPLE_INTEGER datatype is a subtype o f the PLS_INTEGER datatype and can dramatically increase the speed of integer arithmetic in natively compiled code, but

    only shows marginal performance improveme nts in interpreted c ode . The fo llowing proce dure c ompares the performance o f the SIMPLE_INTEGER and PLS_INTEGERdatatypes.

    CREATE OR REPLACE PROCEDURE simple_integer_test_proc ASl_start NUMBER;l_loops NUMBER := 10000000;l_pls_integer PLS_INTEGER := 0;l_pls_integer_incr PLS_INTEGER := 1;l_simple_integer SIMPLE_INTEGER := 0;l_simple_integer_incr SIMPLE_INTEGER := 1;

    BEGIN

    l_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPl_pls_integer := l_pls_integer + l_pls_integer_incr;

    END LOOP;

    DBMS_OUTPUT.put_line('PLS_INTEGER: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');

    l_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOP

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    4/22

    l_simple_integer := l_simple_integer + l_simple_integer_incr;END LOOP;

    DBMS_OUTPUT.put_line('SIMPLE_INTEGER: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');

    END simple_integer_test_proc;/

    When run in the default interpreted mode the performance improvement of the SIMPLE_INTEGER datatype is not spectacular.

    SQL> SET SERVEROUTPUT ONSQL> EXEC simple_integer_test_proc;PLS_INTEGER: 47 hsecsSIMPLE_INTEGER: 44 hsecs

    PL/SQL procedure successfully completed.

    SQL>

    We natively compile the procedure by altering the PLSQL_CODE_TYPE value fo r the se ssion and recom piling the proce dure.

    ALTER SESSION SET PLSQL_CODE_TYPE=NATIVE;ALTER PROCEDURE simple_integer_test_proc COMPILE;

    Native ly co mpiling the pro ce dure pro duce s dram atic s pe ed improve me nts fo r bo th datatype s, but mo re s o f o r the SIMPLE_INTEGER datatype.

    SQL> SET SERVEROUTPUT ONSQL> EXEC simple_integer_test_proc;PLS_INTEGER: 10 hsecsSIMPLE_INTEGER: 2 hsecs

    PL/SQL procedure successfully completed.

    SQL>

    The spe ed improvem ents are a result of two f undamental differences between the two datatypes. First, SIMPLE_INTEGER and PLS_INTEGER have the same range (-

    2,147,483,648 through 2,147,483,647), but SIMPLE_INTEGER wraps ro und when it exc ee ds its bo unds, rather than throwing an error like PLS_INTEGER.

    SET SERVEROUTPUT ONDECLARE

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    5/22

    l_simple_integer SIMPLE_INTEGER := 2147483645;BEGIN

    FOR i IN 1 .. 4 LOOPl_simple_integer := l_simple_integer + 1;DBMS_OUTPUT.PUT_LINE(TO_CHAR(l_simple_integer, 'S9999999999'));

    END LOOP;

    FOR i IN 1 .. 4 LOOPl_simple_integer := l_simple_integer - 1;DBMS_OUTPUT.PUT_LINE(TO_CHAR(l_simple_integer, 'S9999999999'));

    END LOOP;END;/+2147483646+2147483647-2147483648-2147483647-2147483648+2147483647+2147483646+2147483645

    PL/SQL procedure successfully completed.

    SQL>

    Second, SIMPLE_INTEGER can never have a NULL value, either when it is de clared, o r by assig nment.

    DECLAREl_simple_integer SIMPLE_INTEGER;

    BEGINNULL;

    END;/

    *ERROR at line 2:ORA-06550: line 2, column 20:PLS-00218: a variable declared NOT NULL must have an initialization assignment

    SQL>

    DECLAREl_simple_integer SIMPLE_INTEGER := 0;

    BEGINl_simple_integer := NULL;

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    6/22

    END;/

    *ERROR at line 4:ORA-06550: line 4, column 23:PLS-00382: expression is of wrong typeORA-06550: line 4, column 3:PL/SQL: Statement ignored

    SQL>

    The removal of overflow and NULL checking result in a significant reduction in overhead compared to PLS_INTEGER.

    CONTINUE Statement

    The CONTINUE statement jumps out of the current loop interation and starts the next one. It can be used on its own, or as part of a CONTINUE WHEN stateme nt, as shown

    be low.

    SET SERVEROUTPUT ONDECLARE

    l_number NUMBER := 0;BEGIN

    FOR i IN 1 .. 100 LOOPCONTINUE WHEN MOD(i,2) = 0;

    -- Do something here!l_number := l_number + 1;

    END LOOP;

    DBMS_OUTPUT.put_line('CONTINUE WHEN : ' || l_number);

    l_number := 0;

    FOR i IN 1 .. 100 LOOP

    IF MOD(i,2) = 0 THENCONTINUE;END IF;

    -- Do something here!l_number := l_number + 1;

    END LOOP;

    DBMS_OUTPUT.put_line('IF .. CONTINUE: ' || l_number);END;/

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    7/22

    CONTINUE WHEN : 50IF .. CONTINUE: 50

    PL/SQL procedure successfully completed.

    SQL>

    This type of proce ssing has always bee n possible using IF statements either on their own or with exceptions or GOTO stateme nts, but the CONTINUE statement is neater and

    brings PL/SQL in line with o ther langaug es . The f o llowing e xamp les s how the ty pe o f c ode ne ce ss ary to pe rfo rm the s ame task before the CONTINUE stateme nt was added

    to PL/SQL.

    SET SERVEROUTPUT ONDECLARE

    ex_continue EXCEPTION;l_number NUMBER := 0;

    BEGINFOR i IN 1 .. 100 LOOP

    BEGINIF MOD(i,2) != 0 THEN

    RAISE ex_continue;END IF;

    -- Do something here!l_number := l_number + 1;

    EXCEPTIONWHEN ex_continue THEN

    NULL;END;

    END LOOP;

    DBMS_OUTPUT.put_line('EXCEPTION: ' || l_number);

    l_number := 0;

    FOR i IN 1 .. 100 LOOPIF MOD(i,2) != 0 THEN

    -- Do something here!l_number := l_number + 1;

    END IF;END LOOP;

    DBMS_OUTPUT.put_line('IF : ' || l_number);

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    8/22

    l_number := 0;

    FOR i IN 1 .. 100 LOOPIF MOD(i,2) = 0 THEN

    GOTO label_continue;END IF;

    -- Do something here!l_number := l_number + 1;

    >NULL;

    END LOOP;

    DBMS_OUTPUT.put_line('GOTO : ' || l_number);END;/EXCEPTION: 50IF : 50GOTO : 50

    PL/SQL procedure successfully completed.

    SQL>

    Sequences in PL/SQL Expressions

    The NEXTVAL and CURRVAL sequence pseudoc olumns c an now be acce sse d in PL/SQL expressions as well as queries. T his m akes the co de loo k simpler, and the

    doc umentation sugg ests it improves performance. The fo llowing e xample co mpares the spee d of the original and new methods o f acc ess ing these sequence values.

    CREATE SEQUENCE test1_seq START WITH 1000000;

    SET SERVEROUTPUT ONDECLARE

    l_start NUMBER;l_loops NUMBER := 100000;l_value NUMBER;

    BEGIN

    l_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPSELECT test1_seq.NEXTVALINTO l_valueFROM dual;

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    9/22

    END LOOP;

    DBMS_OUTPUT.put_line('NEXTVAL SELECT=' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');

    l_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPl_value := test1_seq.NEXTVAL;

    END LOOP;

    DBMS_OUTPUT.put_line('NEXTVAL Expression=' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');

    l_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPSELECT test1_seq.CURRVALINTO l_valueFROM dual;

    END LOOP;

    DBMS_OUTPUT.put_line('CURRVAL SELECT=' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');

    l_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPl_value := test1_seq.CURRVAL;

    END LOOP;

    DBMS_OUTPUT.put_line('CURRVAL Expression=' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');

    END;/NEXTVAL SELECT=2196 hsecsNEXTVAL Expression=2203 hsecsCURRVAL SELECT=1007 hsecsCURRVAL Expression=1003 hsecs

    PL/SQL procedure successfully completed.

    SQL>

    You can se e that as far as e lapsed time is co ncerned, there is little difference betwee n the two methods .

    Dynamic SQL Enhancements

    Native dynamic SQL and the DBMS_S QL packag e no w suppo rt dynamic S QL state me nts large r than 32 KB. The EXECUTE IMMEDIATE statement, OPEN-FORstatement

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_sql.htm#i997676http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/langelems.htm#i35231http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/langelems.htm#i33888
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    10/22

    and DBMS_SQL.PARSE procedure all accept SQL statements in the form of CLOBs.

    The DBMS_SQL.TO_REFCURSORfunction co nverts a DBMS_SQ L cursor ID into a REF CURSOR.

    SET SERVEROUTPUT ONDECLARE

    l_cursor NUMBER;l_return NUMBER;

    l_ref_cursor SYS_REFCURSOR;TYPE t_emp_tab IS TABLE OF emp%ROWTYPE;l_emp_tab t_emp_tab;

    BEGINl_cursor := DBMS_SQL.open_cursor;

    DBMS_SQL.parse(l_cursor, 'SELECT * FROM emp', DBMS_SQL.NATIVE);

    l_return := DBMS_SQL.EXECUTE(l_cursor);

    -- Connvert from DBMS_SQL to a REF CURSOR.l_ref_cursor := DBMS_SQL.to_refcursor(l_cursor);FETCH l_ref_cursor BULK COLLECT INTO l_emp_tab;

    DBMS_OUTPUT.put_line('Employee Count: ' || l_emp_tab.count);

    CLOSE l_ref_cursor;END;/Employee Count: 14

    PL/SQL procedure successfully completed.

    SQL>

    The DBMS_SQL.TO_CURSOR_NUMBERfunction co nverts a REF CURSOR into a DBMS_SQ L cursor ID.

    SET SERVEROUTPUT ONDECLARE

    l_ref_cursor SYS_REFCURSOR;

    l_cursor NUMBER;l_count NUMBER := 0;

    BEGINOPEN l_ref_cursor FOR 'SELECT * FROM emp';

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/dynamic.htm#BHCBJGEHhttp://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_sql.htm#CHDFDCDE
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    11/22

    l_cursor := DBMS_SQL.to_cursor_number(l_ref_cursor);

    WHILE DBMS_SQL.fetch_rows(l_cursor) > 0 LOOPl_count := l_count + 1;

    END LOOP;

    DBMS_OUTPUT.put_line('Employee Count: ' || l_count);

    DBMS_SQL.close_cursor(l_cursor);END;

    /Employee Count: 14

    PL/SQL procedure successfully completed.

    SQL>

    In addition, the DBMS_SQL package now supports all datatypes supported by native dynamic SQL.

    Generalized Invocation

    Generalized invocation allows a subtype to invoke a method of a parent type (supertype) using the following syntax.

    (SELF AS supertype_name).method_name

    The following example shows this in action.

    First, we create a type with some attributes and a member function.

    CREATE OR REPLACE TYPE my_type AS OBJECT (id NUMBER,description VARCHAR2(50),MEMBER FUNCTION show_attributes RETURN VARCHAR2)NOT FINAL;

    /

    CREATE OR REPLACE TYPE BODY my_type AS

    MEMBER FUNCTION show_attributes RETURN VARCHAR2 ISBEGIN

    RETURN 'id=' || id || ' description=' || description;END;

    END;

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    12/22

    /

    Ne xt, we c reate a s ubtype o f this o bjec t, which adds a new attribute and m ethod as well as o verriding the show_attributes membe r function.

    CREATE OR REPLACE TYPE my_subtype UNDER my_type (short_desc VARCHAR2(10),OVERRIDING MEMBER FUNCTION show_attributes RETURN VARCHAR2,MEMBER FUNCTION show_parent_attributes RETURN VARCHAR2);

    /

    CREATE OR REPLACE TYPE BODY my_subtype AS

    OVERRIDING MEMBER FUNCTION show_attributes RETURN VARCHAR2 ISBEGIN

    RETURN (self AS my_type).show_attributes || ' short_desc=' || short_desc;END;

    MEMBER FUNCTION show_parent_attributes RETURN VARCHAR2 ISBEGIN

    RETURN (self AS my_type).show_attributes;END;

    END;/

    No tice the m ethod o f the parent ty pe is acce ss ed using the g ene ralize d invoc ation syntax. The c ode be low c reate s a subtype and invo kes its me mbe r functions .

    SET SERVEROUTPUT ONDECLARE

    l_subtype my_subtype := my_subtype(1, 'Long description for 1', 'S Desc 1');BEGIN

    DBMS_OUTPUT.put_line('show_attributes=' || l_subtype.show_attributes);DBMS_OUTPUT.put_line('show_parent_attributes=' || l_subtype.show_parent_attributes);

    END;/show_attributes=id=1 description=Long description for 1 short_desc=S Desc 1show_parent_attributes=id=1 description=Long description for 1

    PL/SQL procedure successfully completed.

    SQL>

    A type can invoke the member functions of any parent type in this way, regardless of the depth of the inheritance.

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    13/22

    Named and Mixed Notation in PL/SQL Subprogram Invocations

    Prior to 11g, PL/SQL invoked from SQL had to have its parameters passed using positional notation, making it difficult to determine the meaning of parameters. Oracle 11g

    allows po sitional, named and mixed notation to be use d when calling PL/SQL from S QL, as sho wn below.

    -- Build a test function with multiple parameters.CREATE OR REPLACE FUNCTION test_func(p_value_1 IN NUMBER DEFAULT 0,

    p_value_2 IN NUMBER DEFAULT 0)RETURN NUMBER AS

    BEGINRETURN p_value_1 + p_value_2;

    END test_func;/

    Function created.

    SQL> -- Positional Notation.SQL> SELECT test_func(10, 20) FROM dual;

    TEST_FUNC(10,20)----------------

    30

    1 row selected.

    SQL> -- Mixed Notation.SQL> SELECT test_func(10, p_value_2 => 20) FROM dual;

    TEST_FUNC(10,P_VALUE_2=>20)---------------------------

    30

    1 row selected.

    SQL> -- Named Notation.

    SQL> SELECT test_func(p_value_1 => 10, p_value_2 => 20) FROM dual;

    TEST_FUNC(P_VALUE_1=>10,P_VALUE_2=>20)--------------------------------------

    30

    1 row selected.

    SQL>

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    14/22

    Automatic Subprogram Inlining

    Every call to a procedure or function causes a slight, but measurable, performance overhead, which is especially noticeable when the subprogram is called within a loop.

    Avoiding procedures and functions is not an option, as it goes against the concept of modular programming, making programs bulky and difficult to manage. Automaticsubprogram inlining can reduce the overheads associated with calling subprograms, whilst leaving your original source code in its normal modular state. This is done by

    replacing the subprog ram calls with a co py o f the c ode in the subprog ram at co mpile time.

    The process of subprogram inlining is controlled by the PLSQL_OPTIMIZE_LEVEL paramete r and the INLINE pragma. When PLSQL_OPTIMIZE_LEVEL=2 (the default),

    the INLINE pragma determines whether the following statement or declaration should be inlined or not. When PLSQL_OPTIMIZE_LEVEL=3, the optimizer may inline code

    automatically. In this c ase the INLINE pragma c an turn it off inlining fo r a statem ent, o r increase the likelihoo d that the op timizer will choo se to inline a state me nt. The

    relationship is easier to understand when you see the following example.

    These tests use an anonymous block with a function defined in the declaration block. The function is then called repeatedly in a loop. The settings for

    PLSQL_OPTIMIZE_LEVEL and the INLINE pragma are altered to switch subprogram inlining o n and o ff. First, we make s ure PLSQL_OPTIMIZE_LEVEL=2 and run the

    co de with no INLINE pragma set. With these settings we would not expect to see subprogram inlining taking place.

    ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2;

    SET SERVEROUTPUT ONDECLARE

    l_loops NUMBER := 10000000;l_start NUMBER;

    l_return NUMBER;

    FUNCTION add_numbers (p_1 IN NUMBER,p_2 IN NUMBER)

    RETURN NUMBER ASBEGIN

    RETURN p_1 + p_2;END add_numbers;

    BEGINl_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOP

    --PRAGMA INLINE (add_numbers, 'YES');l_return := add_numbers(1, i);

    END LOOP;

    DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');END;/Elapsed Time: 509 hsecs

    PL/SQL procedure successfully completed.

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    15/22

    SQL>

    This results in an elapsed time of 509 hsecs.

    Ne xt, we kee p the s ame optimization set ting, b ut include the INLINE pragma with a setting o f "YES" for the calls to t he ADD_NUMBERS function. We wo uld now expe ct

    subprog ram inlining to take place .

    ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2;

    SET SERVEROUTPUT ONDECLARE

    l_loops NUMBER := 10000000;l_start NUMBER;l_return NUMBER;

    FUNCTION add_numbers (p_1 IN NUMBER,p_2 IN NUMBER)

    RETURN NUMBER ASBEGIN

    RETURN p_1 + p_2;END add_numbers;

    BEGINl_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPPRAGMA INLINE (add_numbers, 'YES');l_return := add_numbers(1, i);

    END LOOP;

    DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');END;/Elapsed Time: 245 hsecs

    PL/SQL procedure successfully completed.

    SQL>

    This gives an elapsed time of 245 hsec, which is approximately half that of the previous test, implying that subprogram inlining is taking place.

    Ne xt, we make sure PLSQL_OPTIMIZE_LEVEL=3 and run the code with no INLINE pragma set. We would now expect the optimizer to implicitly choose to inline the

    ADD_NUMBERS call.

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    16/22

    ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=3;

    SET SERVEROUTPUT ONDECLARE

    l_loops NUMBER := 10000000;l_start NUMBER;l_return NUMBER;

    FUNCTION add_numbers (p_1 IN NUMBER,p_2 IN NUMBER)

    RETURN NUMBER ASBEGIN

    RETURN p_1 + p_2;END add_numbers;

    BEGINl_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOP--PRAGMA INLINE (add_numbers, 'YES');l_return := add_numbers(1, i);

    END LOOP;

    DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');END;/Elapsed Time: 245 hsecs

    PL/SQL procedure successfully completed.

    SQL>

    This gives an elapsed time of 245 hse c, which implies that subprog ram inlining is still taking place .

    Finally, we make sure PLSQL_OPTIMIZE_LEVEL=3 and run the c od e with an INLINE pragma set to "NO". We would expect there to be no inlining of the ADD_NUMBERS call

    now.

    ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=3;

    SET SERVEROUTPUT ONDECLARE

    l_loops NUMBER := 10000000;l_start NUMBER;l_return NUMBER;

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    17/22

    FUNCTION add_numbers (p_1 IN NUMBER,p_2 IN NUMBER)

    RETURN NUMBER ASBEGIN

    RETURN p_1 + p_2;END add_numbers;

    BEGINl_start := DBMS_UTILITY.get_time;

    FOR i IN 1 .. l_loops LOOPPRAGMA INLINE (add_numbers, 'NO');l_return := add_numbers(1, i);

    END LOOP;

    DBMS_OUTPUT.put_line('Elapsed Time: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');END;/Elapsed Time: 500 hsecs

    PL/SQL procedure successfully completed.

    SQL>

    This gives an elapsed time of 500 hsecs, which implies that inlining did not take place as we expected.

    The INLINE pragma only affects the following type s o f stateme nts.

    Assignment

    CallConditional

    CASE

    CONTINUE-WHEN

    EXECUTE IMMEDIATE

    EXIT-WHEN

    LOOPRETURN

    In each case, it affects every call to s pecified subprog ram from the state ment.

    The optimizer can choo se to ignore an INLINE pragma se tting of "YES" if it be lieve s inlining is undes irable, but a se tting of "NO" will always prev ent inlining.

    The compiler inlines subprograms early in the optimization process, which may preventing later, more powerful optimizations taking place. As a result, performance almost

    always improves with inlining, but in some cases it may not be effective.

    PL/Scope

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    18/22

    PL/Scope is a tool that gathers information about user defined identifiers at compile time. Collection of PL/Scope data is controlled by the PLSCOPE_SETTINGS parameter,

    which has a de fault se tting of "IDENTIFIERS:NONE". Switch this value to "IDENTIFIERS:ALL" to enable co llec tion.

    SQL> ALTER SESSION SET PLSCOPE_SETTINGS='IDENTIFIERS:ALL';

    Session altered.

    SQL>

    Data collection is performed fo r all objects c ompiled after the flag is se t, so we must now create an object to gather some data.

    CREATE OR REPLACE PROCEDURE test_plscope (p_in IN NUMBER) ASl_var NUMBER;

    BEGINl_var := p_in;

    l_var := l_var + 1;

    END;/

    The data is stored in the SYSAUX tablespace, so the current space used for PL/Scope data can be displayed with the following query.

    SELECT space_usage_kbytesFROM v$sysaux_occupantsWHERE occupant_name = 'PL/SCOPE';

    SPACE_USAGE_KBYTES------------------

    384

    1 row selected.

    SQL>

    The PL/Scope data is available from the %_IDENTIFIERS views. The following query displays data gathered during the compilation of the test_plscope procedure.

    COLUMN name FORMAT A30SELECT LPAD(' ', level*2, ' ') || name AS name, type, usage, usage_id, line, colFROM user_identifiersSTART WITH usage_context_id = 0CONNECT BY PRIOR usage_id = usage_context_id;

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/server.111/b28320/statviews_1089.htm#REFRN20599
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    19/22

    NAME TYPE USAGE USAGE_ID LINE COL------------------------------ ------------------ ----------- ---------- ---------- ----------

    TEST_PLSCOPE PROCEDURE DECLARATION 1 1 11TEST_PLSCOPE PROCEDURE DEFINITION 2 1 11

    P_IN FORMAL IN DECLARATION 3 1 25L_VAR VARIABLE DECLARATION 4 2 3L_VAR VARIABLE ASSIGNMENT 5 4 3

    P_IN FORMAL IN REFERENCE 6 4 12L_VAR VARIABLE ASSIGNMENT 7 6 3

    L_VAR VARIABLE REFERENCE 8 6 12

    8 rows selected.

    SQL>

    The likelihoo d is that mo st use rs will view the PL/Sco pe data via a PL/SQL IDE, such as S QL Deve loper.

    The documentation states that some identifiers will not listed unless the STANDARD package is rec ompiled after the PLSCOPE_SETTINGS parameter is set as follows. This

    results in ov er 7,00 0 invalid o bject s, m os t of which will not reco mpile eve n when using the urlrp.sql script. I don't rec om me nd this appro ach if you want a working instance .

    PL/SQL Native Compiler Generates Native Code Directly

    Native co mpilation o f PL/SQL co de as bee n pos sible s ince Oracle 9i. See :

    PL/SQL Native Com pilation (9i)PL/SQL Native Co mpilation Enhancem ents (1 0g )

    In these versions of the database, the PL/SQL code was co nverted to C co de, co mpiled and run as external procedures. Using natively co mpiled PL/SQL needed a C co mpileron the se rver and interve ntion fro m the DBA. In addition, using natively c om piled PL/SQL in a RAC environment c ould prove problem atic.

    In Oracle 1 1g , PL/SQL native c om pilation requires no C co mpiler, no DBA intervention and is fully suppo rted in a RAC environme nt. By s etting the PLSQL_CODE_TYPE to a

    value o fNATIVE, rather than the d efault value o fINTERPRETED, co de is co mpiled directly to machine c ode and stored in the SYSTEM tablespac e. When the c ode is called,

    it is loaded into shared memory, making it accessible for all sessions in that instance. The %_PLSQL_OBJECT_SETTINGS views include the c urrent PLSQL_CODE_TYPE

    setting fo r each PL/SQL object.

    Remembe r, native c ompilation will improve the spee d of procedure co de, but has no e ffec t on the performance o f SQL. When code performs lots of mathematical

    op erations, like the SIMPLE_INTEGERexample, native co mpilation c an produce co nsiderable performance improve ments. If co de is predom inantly performing SQL, littleimprovement will be noticed.

    As with previous database versions, it is possible to natively compile all PL/SQL code in the database, provided you follow the supported proce dure.

    New PL/SQL Compiler Warning

    A new PL/SQL com piler warning has bee n added to identify WHEN OTHERS exc eptio n handlers that do no re-raise errors using RAISE orRAISE_APPLICATION_ERROR.

    Such exception handlers can often hide code failures that result in hard to identify bugs. The example below shows the expected compiler warning when the

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/tuning.htm#BABEJGDIhttp://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#simple_integerhttp://www.oracle-base.com/articles/10g/PlsqlEnhancements10g.php#plsql_native_compilationhttp://www.oracle-base.com/articles/9i/plsql-native-compilation-9i.php
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    20/22

    PLSQL_WARNINGS flag is se t.

    SQL> ALTER SESSION SET plsql_warnings = 'enable:all';

    Session altered.

    SQL> CREATE OR REPLACE PROCEDURE others_test AS2 BEGIN3 RAISE_APPLICATION_ERROR(-20000, 'Force and exception');4 EXCEPTION5 WHEN OTHERS THEN6 NULL;7 END;8 /

    SP2-0804: Procedure created with compilation warnings

    SQL> SHOW ERRORSErrors for PROCEDURE OTHERS_TEST:

    LINE/COL ERROR-------- -----------------------------------------------------------------

    5/8 PLW-06009: procedure "OTHERS_TEST" OTHERS handler does not end inRAISE or RAISE_APPLICATION_ERROR

    SQL>

    This is o nly a warning me ssag e, so it only identifies po ssible problem co de, it doesn't prevent it.

    PLS-00436 Re striction in FORALL Statements Removed

    The PLS-00436 restriction has been removed, which means you can now reference the individual elements of a collection within the SET and WHERE clauses of a DML

    statement in a FORALL construct. To see this in action, create and populates a test table using the following code.

    CREATE TABLE forall_test (id NUMBER,description VARCHAR2(50)

    );

    INSERT INTO forall_test VALUES (1, 'ONE');INSERT INTO forall_test VALUES (2, 'TWO');INSERT INTO forall_test VALUES (3, 'THREE');INSERT INTO forall_test VALUES (4, 'FOUR');INSERT INTO forall_test VALUES (5, 'FIVE');

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    21/22

    COMMIT;

    The PL/SQL bloc k below po pulates a co llec tion with the e xisting data, ame nds the data in the c ollec tion, then updates the tab le with the am ended data. The final query displaysthe changed data in the table.

    DECLARETYPE t_forall_test_tab IS TABLE OF forall_test%ROWTYPE;l_tab t_forall_test_tab;

    BEGIN

    -- Retrieve the existing data into a collection.SELECT *BULK COLLECT INTO l_tabFROM forall_test;

    -- Alter the data in the collection.FOR i IN l_tab.first .. l_tab.last LOOP

    l_tab(i).description := 'Description for ' || i;END LOOP;

    -- Update the table using the collection.FORALL i IN l_tab.first .. l_tab.last

    UPDATE forall_test

    SET description = l_tab(i).descriptionWHERE id = l_tab(i).id;

    COMMIT;END;/

    SELECT * FROM forall_test;

    ID DESCRIPTION---------- ---------------------------

    1 Description for 12 Description for 23 Description for 34 Description for 45 Description for 5

    5 rows selected.

    SQL>

    No tice bo th the SET and WHERE clauses co ntain referenc es to individual co lumns in the c ollect ion. This m akes using bulk-binds fo r DML eve n easier as we no longe r nee d to

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01
  • 7/29/2019 Articles 11g Plsql New Features and Enhancements

    22/22

    maintain multiple c ollectio ns if we need to refe rence co lumns in the WHERE clause. It can also improve performance o f updates, as previous ve rsions required updates of the

    whole row using the ROW keyword, which included po tentially unnec es sary updates of primary key and fo reign key co lumns.

    For more information see:

    What's New in PL/SQL? - Oracle Database PL/SQL Language Reference 11g Release 1 (11.1)Oracle Database PL/SQL Package s and Types Reference 11g Release 1 (11.1)

    Hope this helps. Regards Tim...

    Back to the Top.

    7 co mments, read/add them...

    Home | Articles | Scripts | Forums | Blog | Certification | Misc | Search | About

    Copyright & Disclaimer

    HTMLCSS

    PDFmyURL.com

    http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://pdfmyurl.com/?otsrc=watermark&otclc=0.01http://jigsaw.w3.org/css-validator/check/refererhttp://validator.w3.org/check?uri=refererhttp://www.oracle-base.com/misc/site-info.php#copyrighthttp://www.oracle-base.com/misc/site-info.phphttp://www.oracle-base.com/search/http://www.oracle-base.com/misc/miscellaneous.phphttp://www.oracle-base.com/misc/ocp-certification.phphttp://www.oracle-base.com/blog/http://www.oracle-base.com/forums/http://www.oracle-base.com/dba/scripts.phphttp://www.oracle-base.com/articles/articles.phphttp://www.oracle-base.com/index.phphttp://www.oracle-base.com/misc/comments.php?page_id=772http://www.oracle-base.com/articles/11g/plsql-new-features-and-enhancements-11gr1.php#Tophttp://docs.oracle.com/cd/B28359_01/appdev.111/b28419/toc.htmhttp://docs.oracle.com/cd/B28359_01/appdev.111/b28370/whatsnew.htm#CJAEGHHH