You are on page 1of 19

PL/SQL

By, Shrinivas Achalkar

FEATURES

PL/SQL Block Structure PL/SQL Variables and Constants

Conditional Control
Iterative Control Cursor

PL/SQL BLOCK STRUCTURE

[DECLARE

-- declarations] BEGIN -- statements [EXCEPTION -- handlers] END;

PL/SQL VARIABLES AND CONSTANTS

Declaring Variables

Variables can have any SQL datatype, such as CHAR, DATE, or NUMBER, or aPL/SQL-only datatype, such as BOOLEAN or PLS_INTEGER. Example:
DECLARE part_no NUMBER(6); part_name VARCHAR2(20); in_stock BOOLEAN; part_price NUMBER(6,2);

Can also declare nested tables, variable-size arrays, and records using the TABLE, VARRAY, and RECORD composite datatypes.

DECLARING VARIABLES

%TYPE

Provides the datatype of a variable or database column.


Useful when declaring variables that will hold database values. Example:
v_last_name employees.last_name%TYPE;

DECLARING VARIABLES

%ROWTYPE
%ROWTYPE attribute provides a record type that represents a row in a table. Columns in a row and corresponding fields in a record have the same names and datatypes. Example:

DECLARE dept_rec departments%ROWTYPE; BEGIN SELECT * INTO dept_rec FROM departments; DBMS_OUTPUT.PUT_LINE(dept_rec.deptname); END

&VARIABLE
Is a bind variable. Used to take value from user at run time. Eg.

DECLARE
empid number; emp_rec employee%ROWTYPE;

BEGIN
SELECT * INTO emp_rec FROM employee WHERE emp_id=&empid; DBMS_OUTPUT.PUT_LINE(emp_rec.ename);

END

PL/SQL VARIABLES AND CONSTANTS


Assigning Values to a Variable 3 ways: 1. Using assignment operator(:=)

Example: hours_worked NUMBER := 40;

2. Using INTO keyword in SELECT statement

Example:
DECLARE bonus NUMBER(8,2); emp_id NUMBER(6) := 100; BEGIN SELECT salary * 0.10 INTO bonus FROM employees WHERE employee_id = emp_id; END;

PL/SQL VARIABLES AND CONSTANTS


o

3. By passing variable as an OUT or IN OUT parameter to subprogram, then assigning value Declaring Constants
o

Example:
o

credit_limit CONSTANT NUMBER := 5000.00;

BIND VARIABLES
You do need to specify bind variables with dynamic SQL i.e. bind variables are used to take value from user at run time Syntax

CONDITIONAL CONTROL

IF statement:

Syntax:
IF condition THEN Statements; [ELSEIF condition THEN Statements; [ELSE Statements;]] END IF

CONDITIONAL CONTROL

CASE Statement:

Syntax:
CASE
WHEN condition THEN Statements; .[n] ELSE Statements;

END CASE Condition can only be eqality condition.

Eg. WHEN job_id=st_clerk THEN

ITERATIVE CONTROL

Endless LOOP:

LOOP
-- sequence of statements

END LOOP;

For endless loop to terminate, you must write EXIT WHEN statement. Eg.
LOOP emp_id:=emp_id+1; EXIT WHEN empid > 10; END LOOP

ITERATIVE CONTROL

WHILE LOOP:

Syantax:
WHILE condition LOOP Statements; END LOOP

Eg.
WHILE emp_id < 10 LOOP DBMS_OUTPUT.PUT_LINE(emp_id); emp_id:=emp_id+1; END LOOP

ITERATIVE CONTROL

FOR LOOP:

Is an optimized for loop for cursor so that a record variable need not be created. Syntax:
FOR ctrl_var IN start . . End LOOP Statements; END LOOP

Here, no need to declare ctrl_var. Inside loop, you cant assign value to ctrl_var, Its read only variable. Instead of start . . End, ypu can write SQL query.

Eg.
BEGIN FOR someone IN (SELECT * FROM employees WHERE employee_id < 120 ) LOOP
DBMS_OUTPUT.PUT_LINE('First name = ' || someone.first_name ||, Last name = ' || someone.last_name);

END LOOP; END;

CURSOR Is a memory object. It creates a stream between session and oracle process i.e. database. It fetch the data row by row from table and passes it to session. When the last row is fetched ,implicitly the cursor is closed so that the memory occupied by cursor gets released. Types:

Implicit cursor Explicit cursor

CURSOR

Implicit cursor:

Created when DML statement is fired i.e. INSERT, UPDATE, DELETE. Implicit cursor name is sql.

Explicit cursor:
Used to retrive multiple rows. Eg.

DECLARE CURSOR c1 IS SELECT * FROM Employee ORDER BY emp_id;

When u declare a cursor, the query isnt get fired. The query get fired when u fetch 1st row into record type.

THANK YOU

You might also like