Sei sulla pagina 1di 15

PL/SQL

What is PL/SQL?
PL/SQL stands for Procedural Language extensions to the Structured Query Language or SQL.. SQL is the powerful tool for both querying and update data in relational databases. Oracle introduced PL/SQL to extend some limitations of SQL to provide a more comprehensive solution for building mission-critical applications running on Oracle database.

Advantages of PL/SQL
Block Structures: PL SQL consists of blocks of code, which can be nested within each other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and reused. Procedural Language Capability: PL SQL consists of procedural language constructs such as conditional statements (if else statements) and loops like (FOR loops). Better Performance: PL SQL engine processes multiple SQL statements simultaneously as a single block, thereby reducing network traffic.

Error Handling: PL/SQL handles errors or exceptions effectively during the execution of a PL/SQL program. Once an exception is caught, specific actions can be taken depending upon the type of the exception or it can be displayed to the user with a message.

Introducing PL/SQL block structure and anonymous block


PL/SQL program units organize the code into blocks. A block without name is known as anonymous block. The anonymous block is the simplest unit in PL/SQL. It is called anonymous block because it is not saved in the database. The anonymous blocks are only one-time use and useful in certain situations such as creating test units.

The following illustrates anonymous block syntax:

[DECLARE] Declaration statements; BEGIN Execution statements; [EXCEPTION] EXCEPTION handling statements; END; /

The anonymous block has three basic parts that are declaration, execution, and exception handling. Only execution part is required and the others are optional. The declaration part allows you to define data types, structures, and variables. You can also declare a variable in the declaration part by giving it a name, a data type and a initial value. You can both define and declare variables in the declaration part. The execution part is required in block structure and it must have at least one statement. The execution part is where you put the execution code or business logic. You can use both procedural and SQL code inside execution part. The exception handling part is starting with the keyword EXCEPTION. The exception part is where you put the code to manage exceptions. You can either catch or handle exceptions in this part. Note that the single forward slash (/) is a signal to tell SQL*Plus to execute the PL/SQL block.

PL/SQL Variable Declaration


To declare a variable, you type a variable name followed by the data type and terminated by a semicolon (;). You can also explicitly add length constraint to the data type in a set of parentheses

e.g DECLARE v_first_name VARCHAR2(20); v_last_name VARCHAR2(20); n_employee_id NUMBER; d_hire_date DATE; BEGIN NULL; END;

DECLARE v_first_name EMPLOYEES.FIRST_NAME%TYPE; v_last_name EMPLOYEES.LAST_NAME%TYPE; n_employee_id EMPLOYEES.EMPLOYEE_ID%TYPE; d_hire_date EMPLOYEES.HIRE_DATE%TYPE; BEGIN NULL; END;

Introduction to PL/SQL IF Statement IF condition THEN sequence_of_statements; END IF; IF condition THEN sequence_of_if_statements; ELSE sequence_of_else_statements; END IF;

IF condition1 THEN sequence_of_statements1 ELSIF condition2 THEN sequence_of_statements2 ELSE sequence_of_statements3 END IF;

Introduction to PL/SQL LOOP Statement

LOOP sequence_of_statements; END LOOP; LOOP ... EXIT; END LOOP;

LOOP ... EXIT WHEN condition; END LOOP;

Introducing to PL/SQL FOR Loop


FOR loop_counter IN [REVERSE] lower_bound .. higher_bound LOOP sequence_of_statements; END LOOP;

Introducing to PL/SQL WHILE Loop WHILE condition LOOP sequence_of_statements; END LOOP;

Potrebbero piacerti anche