You are on page 1of 22

Ads by Go o gle

Oracle

Oracle 11G

Ho me

Articles

Scripts

Fo rums

Blo g

Certificatio n

Misc

Search

Abo ut

Printer Friendly

Oracle 8i | Oracle 9i | Oracle 10g | Oracle 11g | Oracle 12c | Miscellaneo us | PL/SQL | SQL | Oracle RAC | Oracle Apps | Linux
Ho me Artic le s 11g He re Searc h

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


Oracle 11g includes a substantial number o f PL/SQL new features and enhancements. In o rder to prevent this article beco ming to o larg e so me o f these features have been split o ff into separate articles, but the fo llo wing sectio ns represent an explanatio n o f the all the new features listed in the What's New in PL/SQL? sectio n o f the PL/SQL Lang uag e Reference manual. To pics co vered in this article: Enhancements to Reg ular Expressio n Built-in SQL Functio ns SIMPLE_INTEGER Datatype CONTINUE Statement Sequences in PL/SQL Expressio ns Dynamic SQL Enhancements Generalized Invo catio n Named and Mixed No tatio n in PL/SQL Subpro g ram Invo catio ns Auto matic Subpro g ram Inlining PL/Sco pe PL/SQL Native Co mpiler Generates Native Co de Directly New PL/SQL Co mpiler Warning PLS-00436 Restrictio n in FORALL Statements Remo ved To pics co vered in separate articles o n this site: Cro ss-Sessio n PL/SQL Functio n Result Cache Trig g er Enhancements in Oracle Database 11g Release 1 PL/SQL Hierarchical Pro filer Native Oracle XML DB Web Services Database Resident Co nnectio n Po o l

Enhancements to Regular Expression Built- in SQL Functions


PDFmyURL.com

The REGEXP_INSTR and REGEXP_SUBSTR functio ns include a new SUBEXPR parameter that limits the pattern match to a specific subexpressio n 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 functio n returns the number o f times the search pattern appears in so urce string .
PDFmyURL.com

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 o f integ er arithmetic in natively co mpiled co de, but o nly sho ws marg inal perfo rmance impro vements in interpreted co de. The fo llo wing pro cedure co mpares the perfo rmance o f the SIMPLE_INTEGER and PLS_INTEGER datatypes. CREATE OR REPLACE PROCEDURE simple_integer_test_proc AS l_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 LOOP l_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

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 mo de the perfo rmance impro vement o f the SIMPLE_INTEGER datatype is no t spectacular. SQL> SET SERVEROUTPUT ON SQL> EXEC simple_integer_test_proc; PLS_INTEGER: 47 hsecs SIMPLE_INTEGER: 44 hsecs PL/SQL procedure successfully completed. SQL> We natively co mpile the pro cedure by altering the PLSQL_CODE_TYPE value fo r the sessio n and reco mpiling the pro cedure. ALTER SESSION SET PLSQL_CODE_TYPE=NATIVE; ALTER PROCEDURE simple_integer_test_proc COMPILE; Natively co mpiling the pro cedure pro duces dramatic speed impro vements fo r bo th datatypes, but mo re so fo r the SIMPLE_INTEGER datatype. SQL> SET SERVEROUTPUT ON SQL> EXEC simple_integer_test_proc; PLS_INTEGER: 10 hsecs SIMPLE_INTEGER: 2 hsecs PL/SQL procedure successfully completed. SQL> The speed impro vements are a result o f two fundamental differences between the two datatypes. First, SIMPLE_INTEGER and PLS_INTEGER have the same rang e (2,147,483,648 thro ug h 2,147,483,647), but SIMPLE_INTEGER wraps ro und when it exceeds its bo unds, rather than thro wing an erro r like PLS_INTEGER. SET SERVEROUTPUT ON DECLARE
PDFmyURL.com

l_simple_integer SIMPLE_INTEGER := 2147483645; BEGIN FOR i IN 1 .. 4 LOOP l_simple_integer := l_simple_integer + 1; DBMS_OUTPUT.PUT_LINE(TO_CHAR(l_simple_integer, 'S9999999999')); END LOOP; FOR i IN 1 .. 4 LOOP l_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> Seco nd, SIMPLE_INTEGER can never have a NULL value, either when it is declared, o r by assig nment. DECLARE l_simple_integer SIMPLE_INTEGER; BEGIN NULL; END; / * ERROR at line 2: ORA-06550: line 2, column 20: PLS-00218: a variable declared NOT NULL must have an initialization assignment SQL> DECLARE l_simple_integer SIMPLE_INTEGER := 0; BEGIN l_simple_integer := NULL;
PDFmyURL.com

END; / * ERROR at line 4: ORA-06550: line 4, column 23: PLS-00382: expression is of wrong type ORA-06550: line 4, column 3: PL/SQL: Statement ignored SQL> The remo val o f o verflo w and NULL checking result in a sig nificant reductio n in o verhead co mpared to PLS_INTEGER.

CONTINUE Statement
The CONTINUE statement jumps o ut o f the current lo o p interatio n and starts the next o ne. It can be used o n its o wn, o r as part o f a CONTINUE WHEN statement, as sho wn belo w. SET SERVEROUTPUT ON DECLARE l_number NUMBER := 0; BEGIN FOR i IN 1 .. 100 LOOP CONTINUE 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 THEN CONTINUE; END IF; -- Do something here! l_number := l_number + 1; END LOOP; DBMS_OUTPUT.put_line('IF .. CONTINUE: ' || l_number); END; /
PDFmyURL.com

CONTINUE WHEN : 50 IF .. CONTINUE: 50 PL/SQL procedure successfully completed. SQL> This type o f pro cessing has always been po ssible using IF statements either o n their o wn o r with exceptio ns o r GOTO statements, but the CONTINUE statement is neater and bring s PL/SQL in line with o ther lang aug es. The fo llo wing examples sho w the type o f co de necessary to perfo rm the same task befo re the CONTINUE statement was added to PL/SQL. SET SERVEROUTPUT ON DECLARE ex_continue EXCEPTION; l_number NUMBER := 0; BEGIN FOR i IN 1 .. 100 LOOP BEGIN IF MOD(i,2) != 0 THEN RAISE ex_continue; END IF; -- Do something here! l_number := l_number + 1; EXCEPTION WHEN ex_continue THEN NULL; END; END LOOP; DBMS_OUTPUT.put_line('EXCEPTION: ' || l_number); l_number := 0; FOR i IN 1 .. 100 LOOP IF 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

l_number := 0; FOR i IN 1 .. 100 LOOP IF MOD(i,2) = 0 THEN GOTO label_continue; END IF; -- Do something here! l_number := l_number + 1; << label_continue >> NULL; END LOOP; DBMS_OUTPUT.put_line('GOTO END; / EXCEPTION: 50 IF : 50 GOTO : 50 : ' || l_number);

PL/SQL procedure successfully completed. SQL>

Sequences in PL/SQL Expressions


The NEXTVAL and CURRVAL sequence pseudo co lumns can no w be accessed in PL/SQL expressio ns as well as queries. This makes the co de lo o k simpler, and the do cumentatio n sug g ests it impro ves perfo rmance. The fo llo wing example co mpares the speed o f the o rig inal and new metho ds o f accessing these sequence values. CREATE SEQUENCE test1_seq START WITH 1000000; SET SERVEROUTPUT ON DECLARE l_start NUMBER; l_loops NUMBER := 100000; l_value NUMBER; BEGIN l_start := DBMS_UTILITY.get_time; FOR i IN 1 .. l_loops LOOP SELECT test1_seq.NEXTVAL INTO l_value FROM dual;
PDFmyURL.com

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 LOOP l_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 LOOP SELECT test1_seq.CURRVAL INTO l_value FROM 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 LOOP l_value := test1_seq.CURRVAL; END LOOP; DBMS_OUTPUT.put_line('CURRVAL Expression=' || (DBMS_UTILITY.get_time - l_start) || ' hsecs'); END; / NEXTVAL NEXTVAL CURRVAL CURRVAL

SELECT=2196 hsecs Expression=2203 hsecs SELECT=1007 hsecs Expression=1003 hsecs

PL/SQL procedure successfully completed. SQL> Yo u can see that as far as elapsed time is co ncerned, there is little difference between the two metho ds.

Dynamic SQL Enhancements


Native dynamic SQL and the DBMS_SQL packag e no w suppo rt dynamic SQL statements larg er than 32 KB. The EXECUTE IMMEDIATE statement, OPEN-FOR statement
PDFmyURL.com

and DBMS_SQL.PARSE pro cedure all accept SQL statements in the fo rm o f CLOBs. The DBMS_SQL.TO_REFCURSOR functio n co nverts a DBMS_SQL curso r ID into a REF CURSOR. SET SERVEROUTPUT ON DECLARE 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; BEGIN l_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_NUMBER functio n co nverts a REF CURSOR into a DBMS_SQL curso r ID. SET SERVEROUTPUT ON DECLARE l_ref_cursor SYS_REFCURSOR; l_cursor NUMBER; l_count NUMBER := 0; BEGIN OPEN l_ref_cursor FOR 'SELECT * FROM emp';
PDFmyURL.com

l_cursor := DBMS_SQL.to_cursor_number(l_ref_cursor); WHILE DBMS_SQL.fetch_rows(l_cursor) > 0 LOOP l_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 additio n, the DBMS_SQL packag e no w suppo rts all datatypes suppo rted by native dynamic SQL.

Generalized Invocation
Generalized invo catio n allo ws a subtype to invo ke a metho d o f a parent type (supertype) using the fo llo wing syntax. (SELF AS supertype_name).method_name The fo llo wing example sho ws this in actio n. First, we create a type with so me attributes and a member functio n. 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 IS BEGIN RETURN 'id=' || id || ' description=' || description; END; END;
PDFmyURL.com

/ Next, we create a subtype o f this o bject, which adds a new attribute and metho d as well as o verriding the show_attributes member functio n. 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 IS BEGIN RETURN (self AS my_type).show_attributes || ' short_desc=' || short_desc; END; MEMBER FUNCTION show_parent_attributes RETURN VARCHAR2 IS BEGIN RETURN (self AS my_type).show_attributes; END; END; / No tice the metho d o f the parent type is accessed using the g eneralized invo catio n syntax. The co de belo w creates a subtype and invo kes its member functio ns. SET SERVEROUTPUT ON DECLARE 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 1 show_parent_attributes=id=1 description=Long description for 1 PL/SQL procedure successfully completed. SQL> A type can invo ke the member functio ns o f any parent type in this way, reg ardless o f the depth o f the inheritance.
PDFmyURL.com

Named and Mixed Notation in PL/SQL Subprogram Invocations


Prio r to 11g , PL/SQL invo ked fro m SQL had to have its parameters passed using po sitio nal no tatio n, making it difficult to determine the meaning o f parameters. Oracle 11g allo ws po sitio nal, named and mixed no tatio n to be used when calling PL/SQL fro m SQL, as sho wn belo w. -- Build a test function with multiple parameters. CREATE OR REPLACE FUNCTION test_func(p_value_1 IN p_value_2 IN RETURN NUMBER AS BEGIN RETURN 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

NUMBER DEFAULT 0, NUMBER DEFAULT 0)

Automatic Subprogram Inlining


Every call to a pro cedure o r functio n causes a slig ht, but measurable, perfo rmance o verhead, which is especially no ticeable when the subpro g ram is called within a lo o p. Avo iding pro cedures and functio ns is no t an o ptio n, as it g o es ag ainst the co ncept o f mo dular pro g ramming , making pro g rams bulky and difficult to manag e. Auto matic subpro g ram inlining can reduce the o verheads asso ciated with calling subpro g rams, whilst leaving yo ur o rig inal so urce co de in its no rmal mo dular state. This is do ne by replacing the subpro g ram calls with a co py o f the co de in the subpro g ram at co mpile time. The pro cess o f subpro g ram inlining is co ntro lled by the PLSQL_OPTIMIZE_LEVEL parameter and the INLINE prag ma. When PLSQL_OPTIMIZE_LEVEL=2 (the default), the INLINE prag ma determines whether the fo llo wing statement o r declaratio n sho uld be inlined o r no t. When PLSQL_OPTIMIZE_LEVEL=3, the o ptimizer may inline co de auto matically. In this case the INLINE prag ma can turn it o ff inlining fo r a statement, o r increase the likeliho o d that the o ptimizer will cho o se to inline a statement. The relatio nship is easier to understand when yo u see the fo llo wing example. These tests use an ano nymo us blo ck with a functio n defined in the declaratio n blo ck. The functio n is then called repeatedly in a lo o p. The setting s fo r PLSQL_OPTIMIZE_LEVEL and the INLINE prag ma are altered to switch subpro g ram inlining o n and o ff. First, we make sure PLSQL_OPTIMIZE_LEVEL=2 and run the co de with no INLINE prag ma set. With these setting s we wo uld no t expect to see subpro g ram inlining taking place. ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2; SET SERVEROUTPUT ON DECLARE l_loops NUMBER := 10000000; l_start NUMBER; l_return NUMBER; FUNCTION add_numbers (p_1 IN NUMBER, p_2 IN NUMBER) RETURN NUMBER AS BEGIN RETURN p_1 + p_2; END add_numbers; BEGIN l_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

SQL> This results in an elapsed time o f 509 hsecs. Next, we keep the same o ptimizatio n setting , but include the INLINE prag ma with a setting o f "YES" fo r the calls to the ADD_NUMBERS functio n. We wo uld no w expect subpro g ram inlining to take place. ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2; SET SERVEROUTPUT ON DECLARE l_loops NUMBER := 10000000; l_start NUMBER; l_return NUMBER; FUNCTION add_numbers (p_1 IN NUMBER, p_2 IN NUMBER) RETURN NUMBER AS BEGIN RETURN p_1 + p_2; END add_numbers; BEGIN l_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 g ives an elapsed time o f 245 hsec, which is appro ximately half that o f the previo us test, implying that subpro g ram inlining is taking place. Next, we make sure PLSQL_OPTIMIZE_LEVEL=3 and run the co de with no INLINE prag ma set. We wo uld no w expect the o ptimizer to implicitly cho o se to inline the ADD_NUMBERS call.

PDFmyURL.com

ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=3; SET SERVEROUTPUT ON DECLARE l_loops NUMBER := 10000000; l_start NUMBER; l_return NUMBER; FUNCTION add_numbers (p_1 IN NUMBER, p_2 IN NUMBER) RETURN NUMBER AS BEGIN RETURN p_1 + p_2; END add_numbers; BEGIN l_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 g ives an elapsed time o f 245 hsec, which implies that subpro g ram inlining is still taking place. Finally, we make sure PLSQL_OPTIMIZE_LEVEL=3 and run the co de with an INLINE prag ma set to "NO". We wo uld expect there to be no inlining o f the ADD_NUMBERS call no w. ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=3; SET SERVEROUTPUT ON DECLARE l_loops NUMBER := 10000000; l_start NUMBER; l_return NUMBER;
PDFmyURL.com

FUNCTION add_numbers (p_1 IN NUMBER, p_2 IN NUMBER) RETURN NUMBER AS BEGIN RETURN p_1 + p_2; END add_numbers; BEGIN l_start := DBMS_UTILITY.get_time; FOR i IN 1 .. l_loops LOOP PRAGMA 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 g ives an elapsed time o f 500 hsecs, which implies that inlining did no t take place as we expected. The INLINE prag ma o nly affects the fo llo wing types o f statements. Assig nment Call Co nditio nal CASE CONTINUE-WHEN EXECUTE IMMEDIATE EXIT-WHEN LOOP RETURN In each case, it affects every call to specified subpro g ram fro m the statement. The o ptimizer can cho o se to ig no re an INLINE prag ma setting o f "YES" if it believes inlining is undesirable, but a setting o f "NO" will always prevent inlining . The co mpiler inlines subpro g rams early in the o ptimizatio n pro cess, which may preventing later, mo re po werful o ptimizatio ns taking place. As a result, perfo rmance almo st always impro ves with inlining , but in so me cases it may no t be effective.

PL/Scope
PDFmyURL.com

PL/Sco pe is a to o l that g athers info rmatio n abo ut user defined identifiers at co mpile time. Co llectio n o f PL/Sco pe data is co ntro lled by the PLSCOPE_SETTINGS parameter, which has a default setting o f "IDENTIFIERS:NONE". Switch this value to "IDENTIFIERS:ALL" to enable co llectio n. SQL> ALTER SESSION SET PLSCOPE_SETTINGS='IDENTIFIERS:ALL'; Session altered. SQL> Data co llectio n is perfo rmed fo r all o bjects co mpiled after the flag is set, so we must no w create an o bject to g ather so me data. CREATE OR REPLACE PROCEDURE test_plscope (p_in l_var NUMBER; BEGIN l_var := p_in; l_var := l_var + 1; END; / The data is sto red in the SYSAUX tablespace, so the current space used fo r PL/Sco pe data can be displayed with the fo llo wing query. SELECT space_usage_kbytes FROM v$sysaux_occupants WHERE occupant_name = 'PL/SCOPE'; SPACE_USAGE_KBYTES -----------------384 1 row selected. SQL> The PL/Sco pe data is available fro m the %_IDENTIFIERS views. The fo llo wing query displays data g athered during the co mpilatio n o f the test_plscope pro cedure. COLUMN name FORMAT A30 SELECT LPAD(' ', level*2, ' ') || name AS name, type, usage, usage_id, line, col FROM user_identifiers START WITH usage_context_id = 0 CONNECT BY PRIOR usage_id = usage_context_id;
PDFmyURL.com

IN

NUMBER) AS

NAME -----------------------------TEST_PLSCOPE TEST_PLSCOPE P_IN L_VAR L_VAR P_IN L_VAR L_VAR 8 rows selected. SQL>

TYPE -----------------PROCEDURE PROCEDURE FORMAL IN VARIABLE VARIABLE FORMAL IN VARIABLE VARIABLE

USAGE USAGE_ID LINE COL ----------- ---------- ---------- ---------DECLARATION 1 1 11 DEFINITION 2 1 11 DECLARATION 3 1 25 DECLARATION 4 2 3 ASSIGNMENT 5 4 3 REFERENCE 6 4 12 ASSIGNMENT 7 6 3 REFERENCE 8 6 12

The likeliho o d is that mo st users will view the PL/Sco pe data via a PL/SQL IDE, such as SQL Develo per. The do cumentatio n states that so me identifiers will no t listed unless the STANDARD packag e is reco mpiled after the PLSCOPE_SETTINGS parameter is set as fo llo ws. This results in o ver 7,000 invalid o bjects, mo st o f which will no t reco mpile even when using the urlrp.sql script. I do n't reco mmend this appro ach if yo u want a wo rking instance.

PL/SQL Native Compiler Generates Native Code Directly


Native co mpilatio n o f PL/SQL co de as been po ssible since Oracle 9i. See: PL/SQL Native Co mpilatio n (9i) PL/SQL Native Co mpilatio n Enhancements (10g ) In these versio ns o f the database, the PL/SQL co de was co nverted to C co de, co mpiled and run as external pro cedures. Using natively co mpiled PL/SQL needed a C co mpiler o n the server and interventio n fro m the DBA. In additio n, using natively co mpiled PL/SQL in a RAC enviro nment co uld pro ve pro blematic. In Oracle 11g , PL/SQL native co mpilatio n requires no C co mpiler, no DBA interventio n and is fully suppo rted in a RAC enviro nment. By setting the PLSQL_CODE_TYPE to a value o f NATIVE, rather than the default value o f INTERPRETED, co de is co mpiled directly to machine co de and sto red in the SYSTEM tablespace. When the co de is called, it is lo aded into shared memo ry, making it accessible fo r all sessio ns in that instance. The %_PLSQL_OBJECT_SETTINGS views include the current PLSQL_CODE_TYPE setting fo r each PL/SQL o bject. Remember, native co mpilatio n will impro ve the speed o f pro cedure co de, but has no effect o n the perfo rmance o f SQL. When co de perfo rms lo ts o f mathematical o peratio ns, like the SIMPLE_INTEGER example, native co mpilatio n can pro duce co nsiderable perfo rmance impro vements. If co de is predo minantly perfo rming SQL, little impro vement will be no ticed. As with previo us database versio ns, it is po ssible to natively co mpile all PL/SQL co de in the database, pro vided yo u fo llo w the suppo rted pro cedure.

New PL/SQL Compiler Warning


A new PL/SQL co mpiler warning has been added to identify WHEN OTHERS exceptio n handlers that do no re-raise erro rs using RAISE o r RAISE_APPLICATION_ERROR. Such exceptio n handlers can o ften hide co de failures that result in hard to identify bug s. The example belo w sho ws the expected co mpiler warning when the
PDFmyURL.com

PLSQL_WARNINGS flag is set. SQL> ALTER SESSION SET plsql_warnings = 'enable:all'; Session altered. SQL> 2 3 4 5 6 7 8 CREATE OR REPLACE PROCEDURE others_test AS BEGIN RAISE_APPLICATION_ERROR(-20000, 'Force and exception'); EXCEPTION WHEN OTHERS THEN NULL; END; /

SP2-0804: Procedure created with compilation warnings SQL> SHOW ERRORS Errors for PROCEDURE OTHERS_TEST: LINE/COL ERROR -------- ----------------------------------------------------------------5/8 PLW-06009: procedure "OTHERS_TEST" OTHERS handler does not end in RAISE or RAISE_APPLICATION_ERROR SQL> This is o nly a warning messag e, so it o nly identifies po ssible pro blem co de, it do esn't prevent it.

PLS- 00436 Restriction in FORALL Statements Removed


The PLS-00436 restrictio n has been remo ved, which means yo u can no w reference the individual elements o f a co llectio n within the SET and WHERE clauses o f a DML statement in a FORALL co nstruct. To see this in actio n, create and po pulates a test table using the fo llo wing co de. CREATE TABLE forall_test ( id NUMBER, description VARCHAR2(50) ); INSERT INSERT INSERT INSERT INSERT INTO INTO INTO INTO INTO forall_test forall_test forall_test forall_test forall_test VALUES VALUES VALUES VALUES VALUES (1, (2, (3, (4, (5, 'ONE'); 'TWO'); 'THREE'); 'FOUR'); 'FIVE');
PDFmyURL.com

COMMIT; The PL/SQL blo ck belo w po pulates a co llectio n with the existing data, amends the data in the co llectio n, then updates the table with the amended data. The final query displays the chang ed data in the table. DECLARE TYPE 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_tab FROM 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).description WHERE id = l_tab(i).id; COMMIT; END; / SELECT * FROM forall_test; ID ---------1 2 3 4 5 DESCRIPTION --------------------------Description for 1 Description for 2 Description for 3 Description for 4 Description for 5

5 rows selected. SQL> No tice bo th the SET and WHERE clauses co ntain references to individual co lumns in the co llectio n. This makes using bulk-binds fo r DML even easier as we no lo ng er need to
PDFmyURL.com

maintain multiple co llectio ns if we need to reference co lumns in the WHERE clause. It can also impro ve perfo rmance o f updates, as previo us versio ns required updates o f the who le ro w using the ROW keywo rd, which included po tentially unnecessary updates o f primary key and fo reig n key co lumns. Fo r mo re info rmatio n see: What's New in PL/SQL? - Oracle Database PL/SQL Lang uag e Reference 11g Release 1 (11.1) Oracle Database PL/SQL Packag es and Types Reference 11g Release 1 (11.1) Ho pe this helps. Reg ards Tim... Back to the To p.

7 co mments, read/add them...

Ho me | Articles | Scripts | Fo rums | Blo g | Certificatio n | Misc | Search | Abo ut


Co pyrig ht & Disc la ime r HTML CSS

PDFmyURL.com