01 - Introduction To PL SQL
01 - Introduction To PL SQL
Introduction to PL/SQL
- Jayendra Khatod
Copyright CDAC-ACTS
Objectives
Introduction
About PL/SQL
PL/SQL Environment
Benefits of PL/SQL
Write PL/SQL Block
Declare PL/SQL variables
Execute a PL/SQL block
Copyright CDAC-ACTS
About PL/SQL
PL/SQL is Oracle Corporations procedural
language extension to SQL, the standard data
access language for relational databases.
PL/SQL offers modern software engineering
features such as data encapsulation,
exception handling, information hiding, and
therefore brings state-of-the-art programming
capability.
Copyright CDAC-ACTS
About PL/SQL
PL/SQL incorporates many of the advanced
features in programming languages
With PL/SQL, you can use SQL statements to
retrieve Oracle data and PL/SQL control
statements to process the data.
Copyright CDAC-ACTS
PL/SQL Environment
PL/SQL engine
PL/SQL
BLOCK
PL/SQL
BLOCK
PL/SQL
SQL
Procedural
statement
executor
Oracle server
Copyright CDAC-ACTS
Benefits of PL/SQL
You can reuse programs
You can declare variables
You can program with procedural language
control structures
PL/SQL can handle errors
Copyright CDAC-ACTS
VARCHAR2(5);
column_name
v_variable
table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
Copyright CDAC-ACTS
Anonymous Block
Procedure
[DECLARE]
BEGIN
--statements
-statements
[EXCEPTION]
END;
PROCEDURE name
IS
Function
[EXCEPTION]
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
-statements
RETURN value;
[EXCEPTION]
END;
END;
BEGIN
--statements
-statements
Copyright CDAC-ACTS
Use of Variables
Variables are used for:
Temporary storage of data
Manipulation of stored values
Reusability
Ease of maintenance
Copyright CDAC-ACTS
10
Examples
Declare
v_hiredate
v_deptno
v_location
c_comm
DATE;
NUMBER(2) NOT NULL := 10;
VARCHAR2(13) := 'Atlanta';
CONSTANT NUMBER := 1400;
Copyright CDAC-ACTS
11
12
v_ename := 'Paul';
Copyright CDAC-ACTS
13
Copyright CDAC-ACTS
14
Copyright CDAC-ACTS
15
Copyright CDAC-ACTS
16
emp.ename%TYPE;
NUMBER(7,2);
v_balance%TYPE := 10;
Copyright CDAC-ACTS
17
DBMS_OUTPUT.PUT_LINE
Example
SET SERVEROUTPUT ON
DEFINE p_annual_sal = 60000
DECLARE
v_sal NUMBER(9,2) := &p_annual_sal;
BEGIN
v_sal := v_sal/12;
DBMS_OUTPUT.PUT_LINE ('The monthly salary is ' ||
TO_CHAR(v_sal));
END;
Copyright CDAC-ACTS
18
Commenting Code
Prefix single-line comments with two dashes (--).
Place multi-line comments between the symbols
/* and */.
Example
...
v_sal NUMBER (9,2);
BEGIN
/* Compute the annual salary based on the
monthly salary input from the user */
v_sal := &p_monthly_sal * 12;
END;
-- This is the end of the block
Copyright CDAC-ACTS
19
Copyright CDAC-ACTS
20
Summary
PL/SQL blocks are composed of the following
sections:
Declarative (optional)
Executable (required)
Exception handling (optional)
A PL/SQL block can be an anonymous block, a
procedure, or a function.
DBMS_OUTPUT.PUT_LINE
SQL Functions in PL/SQL
Copyright CDAC-ACTS
21
Thank You !
Copyright CDAC-ACTS
22