Wednesday 29 August 2012

Control Statements in PL/SQL



Control statements are the most important PL/SQL extension to SQL.

PL/SQL has three categories of control statements:


Conditional selection statements: this let you run different statements for different data values. For more information, see Conditional Selection Statements

Loop statements: this let you repeat the same statements with a series of different data values.
For more information, see LOOP Statements

Sequential control statements: which allow you to go to a specified, labeled statement, or to do nothing. For more information, see Sequential Control Statements


Conditional Compilation


Conditional compilation lets you customize the functionality in a PL/SQL application without removing source code. For example, you can:

Use new features with the latest database release, and disable them when running the application in an older database release.

Activate debugging or tracing statements in the development environment, and hide them when running the application at a production site. For more information, see "Conditional Compilation"

Processing a Query Result Set One Row at a Time


PL/SQL lets you issue a SQL query and process the rows of the result set one at a time. You can use a simple loop, or you can control the process precisely by using individual statements to perform the query, retrieve data, and finish processing.


Processing Query Result Rows One at a Time


BEGIN
FOR someone IN (SELECT * FROM employees WHERE employee_id < 120)
LOOP
DBMS_OUTPUT.PUT_LINE (
'First name = ' || someone.first_name ||
', Last name = ' || someone.last_name
);
END LOOP;
END;
/


Result:

First name = Steven, Last name = King
First name = Neena, Last name = Kochhar
First name = Lex, Last name = De Haan
First name = Alexander, Last name = Hunold
First name = Bruce, Last name = Ernst
First name = David, Last name = Austin
First name = Valli, Last name = Pataballa
First name = Diana, Last name = Lorentz
First name = Nancy, Last name = Greenberg
First name = Daniel, Last name = Faviet
First name = John, Last name = Chen
First name = Ismael, Last name = Sciarra
First name = Jose Manuel, Last name = Urman
First name = Luis, Last name = Popp
First name = Den, Last name = Raphaely
First name = Alexander, Last name = Khoo
First name = Shelli, Last name = Baida
First name = Sigal, Last name = Tobias
First name = Guy, Last name = Himuro
First name = Karen, Last name = Colmenares

Data Abstraction in PL/SQL


Data Abstraction


Data abstraction lets you work with the essential properties of data without being too involved with details. You can design a data structure first, and then design algorithms that manipulate it.

Topics:

■ Cursors
Composite Variables
%ROWTYPE Attribute
%TYPE Attribute
Abstract Data Types

Cursors


A cursor is a pointer to a private SQL area that stores information about processing a specific SQL statement or PL/SQL SELECT INTO statement. You can use the cursor to retrieve the rows of the result set one at a time. You can use cursor attributes to get information about the state of the cursor—for example, how many rows the statement has affected so far. For more information about cursors, see Cursors

Composite Variables


A composite variable has internal components, which you can access individually. You can pass entire composite variables to subprograms as parameters. PL/SQL has two kinds of composite variables, collections and records.

In a collection, the internal components are always of the same data type, and are called elements. You access each element by its unique subscript. Lists and arrays are classic examples of collections.


In a record, the internal components can be of different data types, and are called fields. You access each field by its name. A record variable can hold a table row, or some columns from a table row. For more information about composite variables, see Chapter 5, "PL/SQL Collections and Records."



%ROWTYPE Attribute


The %ROWTYPE attribute lets you declare a record that represents either a full or partial row of a database table or view. For every column of the full or partial row, the record has a field with the same name and data type. If the structure of the row changes, then the structure of the record changes accordingly. For more information about
%ROWTYPE, see %ROWTYPE Attribute



%TYPE Attribute


The %TYPE attribute lets you declare a data item of the same data type as a previously declared variable or column (without knowing what that type is). If the declaration of the referenced item changes, then the declaration of the referencing item changes accordingly. The %TYPE attribute is particularly useful when declaring variables to hold database values. For more information about %TYPE, see %TYPE Attribute

Abstract Data Types



An Abstract Data Type (ADT) consists of a data structure and subprograms that manipulate the data. The variables that form the data structure are called attributes. The subprograms that manipulate the attributes are called methods. ADTs are stored in the database. Instances of ADTs can be stored in tables and used as PL/SQL variables. ADTs let you reduce complexity by separating a large system into logical components, which you can reuse.

In the static data dictionary view *_OBJECTS, the OBJECT_TYPE of an ADT is TYPE.
In the static data dictionary view *_TYPES, the TYPECODE of an ADT is OBJECT.


For more information about ADTs, see CREATE TYPE Statement
 

Note: ADTs are also called user-defined types and object types.


See Also: Oracle Database Object-Relational Developer's Guide for information about ADTs