Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
[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.
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;
Introducing to PL/SQL WHILE Loop WHILE condition LOOP sequence_of_statements; END LOOP;