The main feature of SQL (non-procedural) is also its drawback: control statements (
decision-making or
iterative control) cannot be used if only SQL is to be used. PL/SQL provides the functionality of other procedural programming languages, such as decision making, iteration etc. A PL/SQL program unit is one of the following: PL/SQL anonymous block,
procedure,
function,
package specification, package body, trigger, type specification, type body, library. Program units are the PL/SQL source code that is developed, compiled, and ultimately executed on the database.
PL/SQL anonymous block The basic unit of a PL/SQL source program is the block, which groups together related declarations and statements. A PL/SQL block is defined by the keywords DECLARE, BEGIN, EXCEPTION, and END. These keywords divide the block into a declarative part, an executable part, and an exception-handling part. The declaration section is optional and may be used to define and initialize constants and variables. If a variable is not initialized then it defaults to
NULL value. The optional exception-handling part is used to handle run-time errors. Only the executable part is required. A block can have a label. For example: > -- this is optional DECLARE -- this section is optional number1 NUMBER(2); number2 number1%TYPE := 17; -- value default text1 VARCHAR2(12) := ' Hello world '; text2 DATE := SYSDATE; -- current date and time BEGIN -- this section is mandatory, must contain at least one executable statement SELECT street_number INTO number1 FROM address WHERE name = 'INU'; EXCEPTION -- this section is optional WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error Code is ' || TO_CHAR(sqlcode)); DBMS_OUTPUT.PUT_LINE('Error Message is ' || sqlerrm); END; The symbol := functions as an
assignment operator to store a value in a variable. Blocks can be nested – i.e., because a block is an executable statement, it can appear in another block wherever an executable statement is allowed. A block can be submitted to an interactive tool (such as SQL*Plus) or embedded within an Oracle Precompiler or
OCI program. The interactive tool or program runs the block once. The block is not stored in the database, and for that reason, it is called an anonymous block (even if it has a label).
Function The purpose of a PL/SQL function is generally used to compute and return a single value. This returned value may be a single scalar value (such as a number, date or character string) or a single collection (such as a nested table or array).
User-defined functions supplement the built-in functions provided by Oracle Corporation. The PL/SQL function has the form: CREATE OR REPLACE FUNCTION [(input/output variable declarations)] RETURN return_type [AUTHID ] -- heading part amount number; -- declaration block BEGIN -- executable part RETURN ; [Exception none] RETURN ; END; Pipe-lined table functions return collections and take the form: CREATE OR REPLACE FUNCTION [(input/output variable declarations)] RETURN return_type [AUTHID ] [] [declaration block] BEGIN PIPE ROW ; RETURN; [Exception exception block] PIPE ROW ; RETURN; END; A function should only use the default IN type of parameter. The only out value from the function should be the value it returns.
Procedure Procedures resemble functions in that they are named program units that can be invoked repeatedly. The primary difference is that
functions can be used in a SQL statement whereas procedures cannot. Another difference is that the procedure can return multiple values whereas a function should only return a single value. The procedure begins with a mandatory heading part to hold the procedure name and optionally the procedure parameter list. Next come the declarative, executable and exception-handling parts, as in the PL/SQL Anonymous Block. A simple procedure might look like this: CREATE PROCEDURE create_email_address ( -- Procedure heading part begins name1 VARCHAR2, name2 VARCHAR2, company VARCHAR2, email OUT VARCHAR2 ) -- Procedure heading part ends AS -- Declarative part begins (optional) error_message VARCHAR2(30) := 'Email address is too long.'; BEGIN -- Executable part begins (mandatory) email := name1 || '.' || name2 || '@' || company; EXCEPTION -- Exception-handling part begins (optional) WHEN VALUE_ERROR THEN DBMS_OUTPUT.PUT_LINE(error_message); END create_email_address; The example above shows a standalone procedure - this type of procedure is created and stored in a database schema using the CREATE PROCEDURE statement. A procedure may also be created in a PL/SQL package - this is called a Package Procedure. A procedure created in a PL/SQL anonymous block is called a nested procedure. The standalone or package procedures, stored in the database, are referred to as "
stored procedures". Procedures can have three types of parameters: IN, OUT and IN OUT. • An IN parameter is used as input only. An IN parameter is passed by reference, though it can be changed by the inactive program. • An OUT parameter is initially NULL. The program assigns the parameter value and that value is returned to the calling program. • An IN OUT parameter may or may not have an initial value. That initial value may or may not be modified by the called program. Any changes made to the parameter are returned to the calling program by default by copying but - with the NO-COPY hint - may be passed
by reference. PL/SQL also supports external procedures via the Oracle database's standard ext-proc process.
Package Packages are groups of conceptually linked functions, procedures, variables, PL/SQL table and record TYPE statements, constants, cursors, etc. The use of packages promotes re-use of code. Packages are composed of the package specification and an optional package body. The specification is the interface to the application; it declares the types, variables, constants, exceptions, cursors, and subprograms available. The body fully defines cursors and subprograms, and so implements the specification. Two advantages of packages are: • Modular approach, encapsulation/hiding of
business logic, security, performance improvement, re-usability. They support
object-oriented programming features like
function overloading and encapsulation. • Using package variables one can declare session level (scoped) variables since variables declared in the package specification have a session scope.
Trigger A
database trigger is like a stored procedure that Oracle Database invokes automatically whenever a specified event occurs. It is a named PL/SQL unit that is stored in the database and can be invoked repeatedly. Unlike a stored procedure, you can enable and disable a trigger, but you cannot explicitly invoke it. While a trigger is enabled, the database automatically invokes it—that is, the trigger fires—whenever its triggering event occurs. While a trigger is disabled, it does not fire. You create a trigger with the CREATE TRIGGER statement. You specify the triggering event in terms of triggering statements, and the item they act on. The trigger is said to be created on or defined on the item—which is either a table, a
view, a schema, or the database. You also specify the timing point, which determines whether the trigger fires before or after the triggering statement runs and whether it fires for each row that the triggering statement affects. If the trigger is created on a table or view, then the triggering event is composed of DML statements, and the trigger is called a DML trigger. If the trigger is created on a schema or the database, then the triggering event is composed of either DDL or database operation statements, and the trigger is called a system trigger. An INSTEAD OF trigger is either: A DML trigger created on a view or a system trigger defined on a CREATE statement. The database fires the INSTEAD OF trigger instead of running the triggering statement.
Purpose of triggers Triggers can be written for the following purposes: • Generating some
derived column values automatically • Enforcing referential integrity • Event logging and storing information on table access • Auditing • Synchronous replication of tables • Imposing security authorizations • Preventing invalid transactions == Data types ==