PL/SQL: Procedural Language/Structured Query Language
PL/SQL: Procedural Language/Structured Query Language
17. Iterative statements / Loops: we have to use the “EXIT” and “EXIR WHEN” keyword to terminate the loop
a. Simple Loop
b. While Loop
c. Numeric for Loop
d. Cursor for Loop
xiii. Cursor:
1. Cursor is a pointer to a memory area called context area
2. It is used to hold the information about the processing of SELECT statement or DML
statement
3. Context Area: It is used to hold the information of following
a. Rows returned by query
b. Number of rows processed by query
c. A pointer to the parsed query in the shared pool
4. Types of Cursors:
a. Implicit Cursor
b. Explicit Cursor
5. Implicit Cursor:
a. Automatically created by Oracle Server
b. User cannot control the behavior of cursor
c. Oracle server creates an implicit cursor for any PL/SQL block which executes
an SQL statements
6. Explicit Cursor:
a. It is user defined cursors
b. User can create cursor for any statement which returns more than one row
of data
c. User has full control on Explicit cursor
d. To create explicit cursor, we need to follow four (4) steps:
i. Declare
ii. Open
iii. Fetch
iv. Close
e. Syntax: CURSOR cursor_name IS select_statement (In Declaration block)
i. Open cursor_name; (In Execution block)
ii. FETCH cursor_name INTO PL/SQL variable; (To fetch the data from
cursor)
iii. Close cursor_name; (closing the cursor)
SET SERVEROUTPUT ON;
DECLARE
v_name VARCHAR2(30);
--Declare Cursor
CURSOR cur_RebellionRider IS
SELECT first_name FROM EMPLOYEES
WHERE employee_id < 105;
BEGIN
OPEN cur_RebellionRider;
LOOP
FETCH cur_RebellionRider INTO v_name;
DBMS_OUTPUT.PUT_LINE (v_name);
EXIT WHEN cur_RebellionRider%NOTFOUND;
END LOOP;--Simple Loop End
CLOSE cur_RebellionRider;
END;
/
xiv. Cursor Parameter
CURSOR cur _ name (parameter list) IS SELECT statement;
OPEN cur _ name (argument list)
ET SERVEROUTPUT ON;
DECLARE
v_name VARCHAR2 (30);
--Declare Cursor
CURSOR p_cur_RebellionRider (var_e_id VARCHAR2) IS
SELECT first_name FROM EMPLOYEES
WHERE employee_id < var_e_id;
BEGIN
OPEN p_cur_RebellionRider (105);
LOOP
FETCH p_cur_RebellionRider INTO v_name;
EXIT WHEN p_cur_RebellionRider%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_name );
END LOOP;
CLOSE p_cur_RebellionRider;
END;
22. Stored Procedures:
a. Stored Procedures is a self-contained sub program that is meant to do some specific task
b. Procedures are named PL/SQL blocks thus they can be reused because they are stored in the
database as database object
CREATE OR REPLACE PROCEDURE pr_RebellionRider IS
var_name VARCHAR2 (30):= 'Manish';
var_web VARCHAR2 (30) := 'RebellionRider.com';
BEGIN
DBMS_OUTPUT.PUT_LINE('Whats Up Internet? I am '||var_name||'
from '||var_web);
END Pr_RebellionRider;
CREATE OR REPLACE PROCEDURE emp_sal( dep_id NUMBER, sal_raise
NUMBER)
IS
BEGIN
UPDATE emp SET salary = salary * sal_raise WHERE department_id
= dep_id;
END;
c. We have to use “Execute” commands to run the stored procedures
23. Calling notations:
a. Positional notation – we have to specify value for each formal parameter in seq manner
b. Named notation -
c. Mixed calling notation
24. PL/SQL Packages:
a. Packages are stored libraries in the database which allow us to group PL/SQL objects under one
name
b. Packages are logical groups of related PL/SQL objects
c. Permanently stored in the database schema
d. Packages includes:
i. Stored Procedures
ii. PL/SQL functions
iii. Database cursors
iv. Type declaration
v. Variables
e. Package Architecture
i. Package specification
ii. Package Body
f. Package Specification: Also known as package header (Whatever we declare in this section are
publicly available and can be referenced outside of package)
i. Syntax:
1. CREATE OR REPLACE PACKAGE PKG_NAME IS
DECLARATION OF ALL THE PACKAGE ELEMENT;
END [PKG_NAME];
g. Package Body: We provide actual structure of all the package elements
i. It contains both declaration of variables as well as definition of package element
ii. Syntax:
1. CREATE OR REPLACE PACKAGE BODY PKG_NAME IS
VARIABLE DECLARATION;
TYPE DECLARATION;
BEGIN
IMPLEMENTATION OF PACKAGE ELEMENTS
END [PKG_NAME];
h. Example:
i. -----PACKAGE SPECIFICATION----
CREATE OR REPLACE PACKAGE pkg_RebellionRider IS
FUNCTION prnt_strng RETURN VARCHAR2;
PROCEDURE proc_superhero(f_name VARCHAR2, l_name VARCHAR2);
END pkg_RebellionRider;
ii. -----PACKAGE BODY-----------------
CREATE OR REPLACE PACKAGE BODY pkg_RebellionRider IS
--Function Implimentation
FUNCTION prnt_strng RETURN VARCHAR2 IS
BEGIN
RETURN 'RebellionRider.com';
END prnt_strng;
--Procedure Implimentation
PROCEDURE proc_superhero(f_name VARCHAR2, l_name VARCHAR2) IS
BEGIN
INSERT INTO new_superheroes (f_name, l_name) VALUES(f_name,
l_name);
END;
END pkg_rrdr;
BEGIN
DBMS_OUTPUT.PUT_LINE (PKG_RebellionRider.PRNT_STRNG);
END;
25. PL/SQL Functions:
a. Function is a self-contained sub-program meant to do some specific well-defined task
b. Functions can be stored into the database as database objects
CREATE [OR REPLACE] FUNCTION function_name
(Parameter 1, Parameter 2…)
RETURN datatype
IS
Declare variable, constant etc.
BEGIN
Executable Statements
Return (Return Value);
END;
c. With PL/SQL functions it is mandatory to specify a return value. To specify the return value, we use
“RETURN” keyword followed by the datatype in header of function.
d. Example:
CREATE OR REPLACE FUNCTION circle_area (radius NUMBER)
RETURN NUMBER IS
--Declare a constant and a variable
Pi CONSTANT NUMBER(7,2) := 3.141;
Area NUMBER(7,2);
BEGIN
--Area of Circle pi*r*r;
area := pi * (radius * radius);
RETURN area;
END;
26. Exceptional Handling:
SET SERVEROUTPUT ON;
DECLARE
var_dividend NUMBER := 24;
var_divisor NUMBER := 0;
var_result NUMBER;
ex_DivZero EXCEPTION;
BEGIN
IF var_divisor = 0 THEN
RAISE ex_DivZero;
END IF;
var_result := var_dividend/var_divisor;
DBMS_OUTPUT.PUT_LINE('Result = ' ||var_result);
EXCEPTION WHEN ex_DivZero THEN
DBMS_OUTPUT.PUT_LINE('Error Error - Your Divisor is Zero');
END;