Вы находитесь на странице: 1из 20

Practicing the Language Usability Enhancements

Copyright 2007, Oracle. All rights reserved.

Objectives
After completing this lesson, you should be able to: Implement the sequence calls to NEXTVAL and CURRVAL without using a SQL statement to retrieve the values Use the new CONTINUE statement to control the next loop iteration or to leave a loop Use both named and mixed notation calls to functions from a SQL statement Use the ALTER TABLE statement to change tables to readonly status

6-2

Copyright 2007, Oracle. All rights reserved.

Lesson Agenda
Changes to sequence calls The new CONTINUE statement

Named and mixed notation calls Read-only tables

6-3

Copyright 2007, Oracle. All rights reserved.

Sequence Enhancement in PL/SQL Expressions


Prior to Oracle Database 11g release: References to sequences were permitted only through SQL statements Use of CURRVAL and NEXTVAL pseudocolumns was not allowed in PL/SQL unless embedded in a SQL statement Using sequences in PL/SQL was cumbersome and required an additional SQL statement in a PL/SQL subroutine

5 4

6-4

Copyright 2007, Oracle. All rights reserved.

Sequence Enhancement in PL/SQL Expressions


Enhancements in Oracle Database 11g: You can use the CURRVAL and NEXTVAL pseudocolumns, qualified by a sequence name, directly in a PL/SQL expression. Sequence usability is improved. Less typing is required by the developer. The resulting code is clearer.

5 4

6-5

Copyright 2007, Oracle. All rights reserved.

Using Sequences in PL/SQL Expressions


Pre-Oracle Database 11g:
declare v_new_id NUMBER; BEGIN SELECT my_seq.NEXTVAL INTO v_new_id FROM Dual; END; /

Starting in Oracle Database 11g:


DECLARE v_new_id NUMBER; BEGIN v_new_id := my_seq.NEXTVAL; END; /

6-6

Copyright 2007, Oracle. All rights reserved.

Using Sequences in PL/SQL Expressions


Try to avoid using the old syntax anymore:
SELECT my_seq.NEXTVAL INTO v_new_id FROM dual;

6-7

Copyright 2007, Oracle. All rights reserved.

Lesson Agenda
Changes to sequence calls The new CONTINUE statement

Named and mixed notation calls Read-only tables

6-8

Copyright 2007, Oracle. All rights reserved.

PL/SQL CONTINUE Statement


Definition:
Adds the functionality to begin the next loop iteration Provides programmers with the ability to transfer control to the next iteration of a loop Uses parallel structure and semantics to the EXIT statement

Benefits:
Eases the programming process May see a small performance improvement over the previous programming workarounds to simulate the CONTINUE statement

6-9

Copyright 2007, Oracle. All rights reserved.

PL/SQL CONTINUE Statement: Usage


Offers you a simplified means to control loop iterations Can be more efficient than previous coding workarounds Is commonly used to filter data inside a loop body before the main processing begins

6 - 10

Copyright 2007, Oracle. All rights reserved.

PL/SQL CONTINUE Statement: Example


Total is: 1 End of Loop Total 2 Total is: 4 End of Loop Total 6 Total is: 9 End of Loop Total 12 Total is: 16 End of Loop Total 20 Total is: 25 End of Loop Total 30 Total is: 36 Total is: 43 Total is: 51 Total is: 60 Total is: 70 is:

DECLARE v_total SIMPLE_INTEGER := 0; BEGIN FOR i IN 1..10 LOOP v_total := v_total + i; 1 dbms_output.put_line ('Total is: '|| v_total); CONTINUE WHEN i > 5; v_total := v_total + i; dbms_output.put_line 2 ('End of Loop Total is: '|| v_total); END LOOP; END; /

is:

is:

is:

is:

PL/SQL procedure successfully completed.

6 - 11

Copyright 2007, Oracle. All rights reserved.

PL/SQL CONTINUE Statement: Example

CREATE OR REPLACE PROCEDURE two_loop IS v_total NUMBER := 0; BEGIN <<BeforeTopLoop>> FOR i IN 1..10 LOOP v_total := v_total + 1; dbms_output.put_line ('Total is: ' || v_total); FOR j IN 1..10 LOOP CONTINUE BeforeTopLoop WHEN i + j > 5; v_total := v_total + 1; END LOOP; END LOOP; END two_loop; Procedure created.

--RESULTS:

EXECUTE two_loop
Total Total Total Total Total Total Total Total Total Total is: is: is: is: is: is: is: is: is: is: 1 6 10 13 15 16 17 18 19 20

PL/SQL procedure successfully completed.

6 - 12

Copyright 2007, Oracle. All rights reserved.

CONTINUE Statement: Guidelines


The CONTINUE statement offers you the functionality to transfer control within a loop back to a new iteration or to leave the loop. The CONTINUE statement cannot appear outside a loop at all; this generates a compiler error. You cannot use the CONTINUE statement to pass through a procedure, function, or method boundary; this generates a compiler error.

6 - 13

Copyright 2007, Oracle. All rights reserved.

Lesson Agenda
Changes to sequence calls The new CONTINUE statement

Named and mixed notation calls Read-only tables

6 - 14

Copyright 2007, Oracle. All rights reserved.

Named and Mixed Notation from SQL


Definition:
PL/SQL allows arguments in a subroutine call to be specified using positional, named, or mixed notation. Before Oracle Database 11g, only the positional notation was supported in calls from SQL. Starting in Oracle Database 11g, named and mixed notation can be used for specifying arguments in calls to PL/SQL subroutines from SQL statements.

Benefits:
For long parameter lists, with most having default values, you can omit values from the optional parameters. You can avoid duplicating the default value of the optional parameter at each call site.

6 - 15

Copyright 2007, Oracle. All rights reserved.

Named and Mixed Notation from SQL: Example

CREATE OR REPLACE FUNCTION f ( p1 IN NUMBER DEFAULT 1, p5 IN NUMBER DEFAULT 5) RETURN NUMBER IS v number; BEGIN v:= p1 + (p5 * 2); RETURN v; END f; / Function created.
SELECT f(p5 => 10) FROM DUAL; F(P5=>10) ---------21

6 - 16

Copyright 2007, Oracle. All rights reserved.

Lesson Agenda
Changes to sequence calls The new CONTINUE statement

Named and mixed notation calls Read-only tables

6 - 17

Copyright 2007, Oracle. All rights reserved.

Read-Only Tables
Use the ALTER TABLE syntax to put a table into read-only mode: Prevents DDL or DML changes during table maintenance Changes it back into read/write mode
ALTER TABLE customers READ ONLY; -- perform table maintenance and then -- return table back to read/write mode ALTER TABLE customers READ WRITE;

6 - 18

Copyright 2007, Oracle. All rights reserved.

Summary
In this lesson, you should have learned how to: Implement the sequence calls to NEXTVAL and CURRVAL without using a SQL statement to retrieve the values. Use the new CONTINUE statement to control the next loop iteration or to leave a loop. Use both named and mixed notation calls to functions from a SQL statement. Use the ALTER TABLE statement to change tables to readonly status.

6 - 19

Copyright 2007, Oracle. All rights reserved.

Practice 6 Overview: Using the New SQL and PL/SQL Usability Features
This practice covers the following topics: Trying the new syntax for sequences Controlling loop iteration with the CONTINUE statement Using the named and mixed notation calls to functions from a SQL statement Changing the status of a table to read-only

6 - 20

Copyright 2007, Oracle. All rights reserved.

Вам также может понравиться