PL SQL
PL SQL
PL/SQL-1 1
1
Objectives
-2- INTERNAL
Introduction
Why PL/SQL
• SQL does not have any procedural capabilities such as
looping and branching nor does it have any conditional
checking capabilities vital for data testing before
storage.
-3- INTERNAL
Introduction contd..
-4- INTERNAL
Introduction contd..
-5- INTERNAL
PL/SQL Block
• A standard PL/SQL code segment is called a block. A
standard PL/SQL block is made up of three sections.
-6- INTERNAL
Block Structure
DECLARE
<declaration of variables/constants/cursors/records/exceptions>
BEGIN
<executable statements>
---
[EXCEPTION]
WHEN <exception1> THEN
----
WHEN <exception2> THEN
–
WHEN OTHERS THEN
---
END;
-7- INTERNAL
Scalar Data Types
TRUE 25-JAN-01
256120.08 Atlanta
INTERNAL
Base Scalar Data Types
CHAR [(maximum_length)]
VARCHAR2 (maximum_length)
NUMBER [(precision, scale)]
BINARY_INTEGER
PLS_INTEGER
BOOLEAN
BINARY_FLOAT
BINARY_DOUBLE
INTERNAL
Base Scalar Data Types
DATE
TIMESTAMP
TIMESTAMP WITH TIME ZONE
TIMESTAMP WITH LOCAL TIME ZONE
INTERVAL YEAR TO MONTH
INTERVAL DAY TO SECOND
INTERNAL
LOB Data Type Variables
Book
(CLOB)
Photo
(BLOB)
Movie
(BFILE)
NCLOB
INTERNAL
Variables and Constants
Variables
• A PL/SQL variable can be used either in SQL or
PL/SQL statements provided it is declared before
referencing it.
• DECLARE V_NUMBER ;
Constants
• Declaration of a constant is similar to the declaration of
a variable except that the keyword CONSTANT must
be added and a value must be assigned.
- 12
INTERNAL
-
Declaration Using Attributes
%TYPE attribute
- 13
INTERNAL
-
DECLARE
V_ENAME EMP.ENAME%TYPE;
AMT NUMBER(5,2);
V_AMT AMT%TYPE;
BEGIN
---
---
- 14
INTERNAL
-
Declaration Using Attributes contd..
%ROWTYPE attribute
• This attribute is used to declare a variable to be a
record having the same structure as a row in a table.
DECLARE
EREC EMP%ROWTYPE;
BEGIN
- 15
INTERNAL
-
Operators
Logical:
• AND
• OR
• NOT
- 16
INTERNAL
-
Conditional Control
- 17
INTERNAL
-
Conditional Control
DECLARE
AGE NUMBER:=8
SEX CHAR(1):='M';
BEGIN
IF AGE<12 AND SEX='M' THEN
DBMS_OUTPUT.PUT_LINE('MALE CHILD');
ELSIF AGE<12 AND SEX='F' THEN
DBMS_OUTPUT.PUT_LINE('FEMALE CHILD');
ELSE
DBMS_OUTPUT.PUT_LINE('NOT A CHILD');
END IF;
END;
- 18
INTERNAL
-
Iterative Control
1. Endless Loop
LOOP
statements
..........
END LOOP;
- 19
INTERNAL
-
Iterative Control contd..
2. For Loop
FOR i IN 1..10
LOOP
statements
.........
END LOOP;
- 20
INTERNAL
-
Iterative Control contd..
3. While Loop
WHILE (condition)
LOOP
.........
statements
.........
END LOOP;
- 21
INTERNAL
-
Iterative Control contd..
SET SERVEROUT ON
DECLARE
S NUMBER:=0;
BEGIN
FOR I IN 1..10
LOOP
S:=S+I;
END LOOP;
DBMS_OUTPUT.PUT_LINE('SUM IS '||S);
END;
- 22
INTERNAL
-
Iterative Control contd..
SET SERVEROUT ON
DECLARE
I NUMBER:=0;
S NUMBER:=0;
BEGIN
WHILE(I<=10)
LOOP
S:=S+I;
I:=I+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('SUM IS '||S);
END;
- 23
INTERNAL
-
SQL Statements in PL/SQL
INTERNAL
SELECT Statements in PL/SQL
INTERNAL
SELECT Statements in PL/SQL
Example:
DECLARE
v_fname VARCHAR2(25);
BEGIN
SELECT first_name INTO v_fname
FROM employees WHERE employee_id=200;
DBMS_OUTPUT.PUT_LINE(' First Name is : '||v_fname);
END;
/
INTERNAL
Retrieving Data in PL/SQL
Retrieve hire_date and salary for the
specified employee.
Example:
DECLARE
v_emp_hiredate employees.hire_date%TYPE;
v_emp_salary employees.salary%TYPE;
BEGIN
SELECT hire_date, salary
INTO v_emp_hiredate, v_emp_salary
FROM employees
WHERE employee_id = 100;
END;
/
INTERNAL
Retrieving Data in PL/SQL
Return the sum of the salaries for all the
employees in the specified department.
Example:
DECLARE
v_sum_sal NUMBER(10,2);
v_deptno NUMBER NOT NULL := 60;
BEGIN
SELECT SUM(salary) -- group function
INTO v_sum_sal FROM employees
WHERE department_id = v_deptno;
DBMS_OUTPUT.PUT_LINE ('The sum of salary is ' || v_sum_sal);
END;
INTERNAL
Using PL/SQL to Manipulate Data
INSERT
MERGE
UPDATE
INTERNAL
Inserting Data
Example:
BEGIN
INSERT INTO employees
(employee_id, first_name, last_name, email,
hire_date, job_id, salary)
VALUES(employees_seq.NEXTVAL, 'Ruth', 'Cores',
'RCORES',CURRENT_DATE, 'AD_ASST', 4000);
END;
/
INTERNAL
Updating Data
Example:
DECLARE
sal_increase employees.salary%TYPE := 800;
BEGIN
UPDATE employees
SET salary = salary + sal_increase
WHERE job_id = 'ST_CLERK';
END;
/
INTERNAL
Deleting Data
Example:
DECLARE
deptno employees.department_id%TYPE := 10;
BEGIN
DELETE FROM employees
WHERE department_id = deptno;
END;
/
INTERNAL
Cursors
INTERNAL
Implicit Cursors(SQL Cursor)
- 34
INTERNAL
-
SQL Cursor Attributes for Implicit Cursors
INTERNAL
Explicit Cursors
• The set of rows returned by a multirow query is called
the "active set."
- 36
INTERNAL
-
Explicit Cursors contd..
OPEN Statement
- 37
INTERNAL
-
Explicit Cursors contd..
FETCH Statement
• The FETCH statement retrieves the rows in the active
set one at a time.
- 38
INTERNAL
-
Explicit Cursors contd..
CLOSE Statement
CLOSE c1;
- 39
INTERNAL
-
Explicit Cursors contd..
Fetching data with cursor
DECLARE
CURSOR C1 IS SELECT * FROM EMPLOYEE;
EMPROW EMPLOYEE%ROWTYPE;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO EMPROW;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('NAME ' || EMPROW.ENAME||'
JOB '||EMPROW.DESIG);
END LOOP;
CLOSE C1;
END;
/
cursor1
- 40
INTERNAL
-
Cursor Attributes
• Each cursor that you explicitly define has four
attributes: %NOTFOUND, %FOUND,
%ROWCOUNT, and %ISOPEN.
- 41
INTERNAL
-
%ISOPEN Attribute contd..
- 42
INTERNAL
-
Exceptions
- 44
INTERNAL
-
Declaring Exceptions
- 45
INTERNAL
-
Exception Handlers
- 46
INTERNAL
-
Exception Handlers contd..
- 47
INTERNAL
-
Predefined Exceptions
- 48
INTERNAL
-
Predefined Exceptions
DECLARE
EMPNO NUMBER:=555;
VENAME EMPLOYEE.ENAME%TYPE;
VJOB EMPLOYEE.DESIG%TYPE;
BEGIN
SELECT ENAME,DESIG INTO VENAME,VJOB FROM EMPLOYEE
WHERE ENO=EMPNO;
DBMS_OUTPUT.PUT_LINE ('NAME ' || VENAME||' JOB '||VJOB);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO EMPLOYEE WITH THE GIVEN
NUMBER');
END;
/
EXCP1
- 49
INTERNAL
-
Predefined Exceptions
Sno Exception Sno Exception
1 ACCESS_INTO_NULL 12 ROWTYPE_MISMATCH
2 CASE_NOT_FOUND 13 SELF_IS_NULL
3 COLLECTION_IS_NULL 14 STORAGE_ERROR
4 CURSOR_ALREADY_OPEN 15 SUBSCRIPT_BEYOND_COUNT
5 DUP_VAL_ON_INDEX 16 SUBSCRIPT_OUTSIDE_LIMIT
6 INVALID_CURSOR 17 SYS_INVALID_ROWID
7 INVALID_NUMBER 18 TIMEOUT_ON_RESOURCE
8 LOGIN_DENIED 19 TOO_MANY_ROWS
9 NO_DATA_FOUND 20 VALUE_ERROR
10 NOT_LOGGED_ON 21 ZERO_DIVIDE
11 PROGRAM_ERROR
INTERNAL
Introduction to Procedures
- 51
INTERNAL
-
Introduction to procedures contd..
- 52
INTERNAL
-
Introduction to procedures contd..
BEGIN
STUCOURSE(1);
END;
/
- 53
INTERNAL
-
Introduction to Procedures contd..
Executing Procedures
• The syntax used to execute a procedure depends on
the environment from which the procedure is called.
- 54
INTERNAL
-
Introduction to procedures contd..
- 55
INTERNAL
-
Introduction to Procedures contd..
Dropping a Procedure
• A procedure can be deleted from the database by
using the following command:
- 56
INTERNAL
-
Introduction to Stored Functions
- 57
INTERNAL
-
Introduction to Stored Functions contd..
- 58
INTERNAL
-
Introduction to Stored Functions contd..
Executing functions
• The syntax for executing a function is:
Syntax:
Variable:=function_name(arg_list);
DECLARE
VMARKS NUMBER;
BEGIN
VMARKS:=GET_MARKS(1);
DBMS_OUTPUT.PUT_LINE(VMARKS);
END;
/
- 59
INTERNAL
-
Introduction to Stored Functions contd..
- 60
INTERNAL
-
Thank You
INTERNAL