2. About PL/SQL
• PL/SQL: Stands for “Procedural Language” extension to SQL.
• Is Oracle Corporation's standard data access language for relational
databases
• Seamlessly integrates procedural constructs with SQL
3. About PL/SQL
• Provides a block structure for executable units of code.
Maintenance of code is made easier with such a well- defined
structure.
• Provides procedural constructs such as:
- Variables, constants, and data types
- Control structures such as conditional statements and loops
- Reusable program units that are written once and executed many
times
4. Benefits of PL/SQL
Benefits of PL/SQL
• Integration of procedural constructs with SQL
• Improved performance
• Exception handling
6. PL/SQL Block Structure
DECLARE (optional)
- Variables, cursors, user-defined exceptions
BEGIN (mandatory)
- SQL statements
- PL/SQL statements
EXCEPTION (optional)
- Actions to perform when exceptions occur
END; (mandatory)
7. Block Types
• Anonymous
• Procedure
• Function
Anonymous
[Declare]
Begin
--Statements
[Exception]
End
Procedure
PROCEDUER name
IS
Begin
--Statements
[Exception]
End
Function
FUNCTION name
RETURN
Begin
--Statements
RETURN value
[Exception]
End
8. Examining an Anonymous Block
SET SERVEROUTPUT ON;
DECLARE
v_hello VARCHAR2(20):= 'Hello World!';
BEGIN
dbms_output.put_line(v_hello);
END;
9. Enabling Output of a PL/SQL Block
• To enable output in SQL Developer, execute the following command
before running the PL/SQL block:
SET SERVEROUTPUT ON;
• Use a predefined Oracle package and its procedure in the anonymous
block:
DBMS_OUTPUT.PUT_LINE();
Ex:
DBMS_OUTPUT.PUT_LINE('The First Name of the Employee is' ||
v_fname);
11. Use of Variables
Variables can be used for:
• Temporary storage of data
• Manipulation of stored values
• Reusability
12. Requirements for Variable Names
A variable name:
• Must start with a letter
• Can include letters or numbers
• Can include special characters (such as $,_, and #)
• Must contain no more than 30 characters
• Must not include reserved words
14. Guidelines for Declaring and Initializing
PL/SQL Variables
1. Follow consistent naming conventions.
2. Use meaningful identifiers for variables.
3. Initialize variables that are designated as NOT NULL and CONSTANT.
4. Initialize variables with the assignment operator (:) or the DEFAULT
keyword:
V_myName VARCHAR2(20):='John';
V_myName VARCHAR2 (20) DEFAULT 'John';
5.Declare one identifier per line for better readability and code maintenance.
6. Avoid using column names as identifiers.
7. Use the NOT NULL constraint when the variable must hold a value.
19. %TYPE Attribute
Is used to declare a variable according to:
• A database column definition
• Another declared variables
prefixed with:
• The database table and column name
• The name of the declared variable
23. Bind Variables
Bind variables are:
• Created in the environment
• Also called host variables
• Created with the VARIABLE keyword*
• Used in SQL statements and PL/SQL blocks
• Accessed even after the PL/SQL block is executed
• Referenced with a preceding colon
• Values can be output using the PRINT command.
24. Using AUTOPRINT with Bind Variables
Use the SET AUTOPRINT ON command to automatically display the
bind variables used in a successful PL/SQL block.
25. Bind Variables
SET AUTOPRINT ON;
VARIABLE b_result NUMBER
BEGIN
SELECT SALARY INTO :b_result
FROM employees
WHERE employee_id = 144;
END;
/
PRINT b_result
29. IF THEN ELSE Statement
IF condition THEN
Statements
ELSE
Statements
End IF;
30. IF THEN ELSE Statement
DECLARE
v_MyNumber Number :=-18;
BEGIN
IF v_MyNumber > 0
THEN
dbms_output.put_line('It is +');
ELSE
dbms_output.put_line('It is -');
End IF;
END;
/
31. IF THEN ELSE Statement
IF condition THEN
Statements
ELSEIF condition THEN
Statements
ELSE
Statements
End IF;
32. IF THEN ELSE Statement
DECLARE
v_MyNumber NUMBER := 0;
BEGIN
IF v_MyNumber > 0 THEN
dbms_output.put_line('It is +');
ELSIF v_MyNumber < 0 THEN
dbms_output.put_line('It is -');
ELSE
dbms_output.put_line('It is Zero');
END IF;
END;
35. Case Statement
DECLARE
v_score NUMBER := 85;
v_grade VARCHAR2(2);
BEGIN
CASE
WHEN v_score >= 90 THEN
v_grade := 'A';
WHEN v_score >= 80 THEN
v_grade := 'B';
WHEN v_score >= 70 THEN
v_grade := 'C';
WHEN v_score >= 60 THEN
v_grade := 'D';
ELSE
v_grade := 'F';
END CASE;
dbms_output.put_line('Grade: ' || v_grade);
END;
/
37. Iterative Control: Loop Statements
• Loops repeat a statement (or a sequence of statements) multiple
times.
• There are three loop types:
1. Basic loop
2. WHILE loop
3. FOR loop