PLSQL 1
PLSQL 1
PLSQL 1
By K.Prajakta
prajakta.karthikeyan@tcs.com
PL/SQL
PL/SQLstands for Procedural Language/SQL
PL/SQLextends SQL by adding constructs
found in procedural languages, resulting in a
structural language that is more powerful
than SQL
APPLICATION /
Pro*(C/ C++/ COBOL)/
Forms/ Reports
Embedded Embedded
PL/SQLBLOCK PL/SQLBLOCK
DECLARE DECLARE
Procedural Procedural PL/SQL
BEGIN BEGIN Statement
Procedural Procedural Executor
SQL SQL
Procedural Procedural
SQL SQL
END; END;
Host language
Host language
Host language SQL
Statement
Executor
!
PL/SQL Block
PL/SQLcode is grouped into structure called blocks.
!
@
Variable Declarations
Examples
• DECLARE
firstName, lastName CHAR(20); -- illegal
• DECLARE
Emp_No NUMBER NOT NULL := 10109;
Total_Sal NUMBER(3,1);
XYZ NUMBER(2,2) := 31.8;
Salary NUMBER(9,2) := XYZ * 131;
V_CHR2 VARCHAR2(12) :="JANUARY";
TODAY_DATE := SYSDATE;
TRUEFALSE BOOLEAN;
• DECLARE
PI CONSTANT NUMBER(9,3) := 3.142; --Constants
Declaration
• DECLARE
Rec1 Emp%ROWTYPE; Emp –Table Name
Variable Assignment
• PL/SQL Expressions consist of Variables, Constants,
Literal, and Function Calls.
Syntax:
plsql_variable :=
plsq1_expression;
• Quick Notes :
:= (ASSIGNMENT) = (VALUE EQUALITY)
The datatype on the left and right side of an assignment
must be the same or implicitly convertible to each
other.
N := '7' -- legal
Column or table references are not allowed on either side
of the assignment operator (:=).
SCOTT.EMP.EMPNO := 1234;
Variable Assignment
Examples
N1 := 5 * N2 * 0.7;
B1 := TRUE;
D1 := '11-JUN-97';
C1 := NULL;
N1 := SQRT( N2 ** 2 – 4 * N3 * N4);
Note:
Nn is Number type
Bn is Boolean
Cn is Char type
Dn is Datetype
!
Scope of Variables and
Constants
SCOPE refers to the visibility of identifiers at
different points in the PL /SQL block
RULES:
1. An identifier is visible in the block in which it is
declared and all its sub-blocks unless rule #2 applies.
– if an identifier declared in an enclosing block is re-
declared in a sub-block, the original identifier
declared in the enclosing block is no longer visible in
the sub-block However, the newly declared identifier
has the rules of scope defined in rule #1. Ex to be
Given
DBMS_OUTPUT.PUT_LINE
An Oracle-supplied packaged procedure
An alternative for displaying data from a PL/SQL block
● Quick Notes:
The full Oracle syntax is supported for these statements.
A PL/SQL variable may be placed anywhere a construct
!
SQL in PL / SQL
Example: DELETE
BEGIN
DELETE FROM Emp
WHERE deptNo = 10 ;
END;
SQL in PL / SQL
SELECT
Syntax
SELECT col1, col2,.,. into var1,var2
FROM table name
WHERE …
• Quick Notes:
– A SELECT statement is the only DML that returns
data. You must provide a location for this data to be
stored via the INTO clause.
– A SELECT...INTO statement must return exactly one
row. Zero or multiple returned rows results in an error.
– For multi-row SELECT's use cursors (discussed later).
SQL in PL / SQL
Example: SELECT
DECLARE
part_Name Parts.name%TYPE;
num_In_Stock Parts.num%TYPE;
BEGIN
SELECT Name, Num INTO part_Name,
num_In_Stock
FROM Parts WHERE part_Id = 624;
--manipulate the retrieved data here--
END;
SQL in PL / SQL
Functional support
Note: Most of the SQL functions are available except the group functions.
Transaction Processing
• Definition
ROLLBACK
SAVEPOINT MARKER A
(ROLLBACK TO)
SAVEPOINT MARKER B
(ROLLBACK TO)
SAVEPOINT MARKER C
(ROLLBACK TO)
COMMIT
Syntax
SAVEPOINT < marker_name >;
ROLLBACK [WORK] TO [SAVEPOINT] < marker_name >;
Transaction Processing
E.G.
BEGIN
INSERT INTO temp(num,des)VALUES (1, 'row 1');
SAVEPOINT A;
… …
ROLLBACK TO SAVEPOINT B;
COMMIT;
END;
Control Structures
• Conditional: IF statements
• Iterative: LOOP and EXIT statements
• Sequential: GOTO and NULL statements
F
T F T
!
Conditional Control
• Condition:
Logical Comparisons form the basis of conditional
control in PL/SQL; the results of these comparisons
are always either TRUE, FALSE or NULL.
• Quick Notes:
– Anything compared with NULL results in a NULL
value.
– A NULL in an expression evaluates to NULL(except
concatenation).
– E.g..
5 + NULL -- evaluates to NULL
'PL'||NULL||'/SQL' -- evaluates to PL/SQL
!
Conditional Control: IF
• To conditionally execute a statement or sequence of
statements.
Syntax
If <condition> THEN
<sequence of statements>
[ELSIF <condition> THEN
<sequence of statements>]
-- ELSIFs may be repeated
[ELSE
<sequence of statements>]
END IF;
!
Conditional Control:
DECLARE
IF
num_jobs NUMBER (7);
actor_id actor.actorid%type;
BEGIN
actor_id := &actor_id;
SELECT COUNT(*) INTO num_jobs FROM auditions
WHERE actorid = actor_id AND
called_back = 'YES';
IF num_jobs > 40 THEN
UPDATE actor SET actor_rating = 'OSCAR time'
WHERE actorid = actor_id;
ELSIF num_jobs > 20 THEN
UPDATE actor SET actor_rating = 'daytime
soaps' WHERE actorid = actor_id;
ELSE
UPDATE actor SET actor_rating = 'waiter' where
actorid = actor_id;
END IF;
COMMIT;
END; !
Conditional Control:
The NULL trap
IF
BLOCK 1: BLOCK 2:
If a>=b THEN If b>=a THEN
Do_this…; Do_that…;
ELSE ELSE
Do_that…; Do_this ...;
END IF ; ENDIF;
!
@
Iterative Control
• Loops repeat a statement or sequence of statements
multiple times.
!
Iterative Control: LOOP
and EXIT
• Simple Loops repeat a sequence of statements multiple times.
Syntax
LOOP
<sequence of statements>
END LOOP; -- sometimes called an
'infinite' loop
• Exit Statements exit any type of loop immediately and program
control goes to the immediate statement after the loop
Syntax
EXIT WHEN <condition> ; -- 'infinite' loop insurance
!
Iterative Control: LOOP
and EXIT
DECLARE
ctr NUMBER(3) :=0;
BEGIN
LOOP
INSERT INTO temp(num,des)
VALUES (ctr,'ITERATION COMPLETE');
ctr := ctr+1;
IF ctr = 500 THEN
EXIT;
END IF;
END LOOP;
END; !
Iterative Control:
• FOR
Numeric FOR Loops repeat a sequence of statements
a fixed number of times.
Syntax
FOR <index> IN
[ REVERSE ]<integer>..<integer> LOOP <sequence of
statements>
END LOOP;
Quick Notes - Index:
– The Loop Index takes on each value in the range, one
at a time, either in forward or reverse order
– It is implicitly of type NUMBER.
– It is only defined within the loop.
– Value may be referenced in an expression, but a new
value may not be assigned to it within the loop
!
Iterative Control: FOR
Example1
BEGIN
FOR I IN 1..500 LOOP
INSERT INTO temp (message)
VALUES ('I will not sleep in class.');
END LOOP;
END;
Example2
DECLARE
my_index CHAR(20) := 'Fettuam'
BEGIN
FOR my_index IN REVERSE 21...30 LOOP
/* redeclares my index */
INSERT INTO temp (des) VALUES (my_index);
/* inserts the numbers 30 through 21 */
END LOOP;
END;
!
@
Iterative Control:
WHILE
• WHILE Loops repeat a sequence of statements until a specific
condition is no longer TRUE.
Syntax
WHILE <condition> LOOP
<sequence of statements>
END LOOP;
• Quick Notes:
– The term <condition> may be any PL/SQL condition (I.e., it
must return a BOOLEAN value of TRUE, FALSE, or
NULL).
– The sequence of statements will be repeated as long as
<condition> evaluates to TRUE.
!
Iterative Control:
WHILE
DECLARE
ctr NUMBER (3) := 0;
BEGIN
WHILE ctr < 500 LOOP
INSERT INTO temp (message)
VALUES ('Well, might sleep just a little');
ctr := ctr + 1;
END LOOP;
END;
!
CASE STATEMENT
CASE selector
WHEN expression1 THEN
result1
WHEN expression2 THEN
result2
...
WHEN expressionN THEN
resultN
[ELSE resultN+1] CASE
END; WHEN search_condition1 THEN
result1
WHEN search_condition2 THEN
result2
...
WHEN search_conditionN THEN
resultN
[ELSE resultN+1]
END;
CASE EXAMPLE
DECLARE
• Just another step grade CHAR(1);
appraisal VARCHAR2(20);
towards writing BEGIN
...
cleaner, easier to appraisal :=
CASE grade
read and WHEN 'A' THEN
'Excellent'
maintain code Good'
WHEN 'B' THEN 'Very
Case1.sql
Sequential Control:
NULL
• It does nothing other than pass the control to the next
statement
Syntax
NULL;
• Quick Notes:
– NULL statement and the Boolean non-value NULL are
different
– Make use of NULL in the IF condition for doing
nothing in any branch of the condition
!
Sequential Control:
NULL
Example1: Minimum PL/SQL block
BEGIN
NULL;
END;
Example2: Use in IF statement
BEGIN
IF <condition> THEN
… Statements …
ELSIF <condition> THEN
NULL; -- do nothing branch of the IF
statement
ELSE
… Statements …
ENDIF; !
Sequential Control:
GOTO
• “GO TO” Statements jump to a different place in the PL/SQL
block .
Syntax
<<label_name>> x:=x + 1; -- a statement label GOTO
GOTO label_name; -- jumps to x := x + 1
“GO TO” Statements have 2 parts
!
Other Uses for Statement
Labels
• Labels may label any statement
• In addition to their use as targets for GOTO
statements, labels may be used for:
1.Blocks
2.Loops
• Labelling a block and loop allows referencing of
DECLARED objects that would otherwise not be
visible because of scope rules.
• Labelling a loop also allows control to exit in
outer loops
!
@
Other Uses for Statement
Labels
Example1:
BEGIN
<<sample>>
DECLARE
deptno NUMBER :=10;
BEGIN
DECLARE
deptno NUMBER := 20;
BEGIN
UPDATE emp SET sal = sal * 1.1
WHERE deptno = sample.deptno;
COMMIT;
END;
END sample;
END; !
Other Uses for Statement
Labels
Example2:
«compute_loop»
FOR i IN 1..100
LOOP
<statements>
DECLARE
I NUMBER := 0;
BEGIN
INSERT INTO temp (num,des)
VALUES (compute_loop.i, 'COMPLETE');
END ;
END LOOP compute_loop; -- must include loop name here
!
Other Uses for Statement
Labels
Example3:
«outer loop»
WHILE a >b LOOP
b:= b +1;
«inner loop»
WHILE b > c LOOP
C := C + 2;
EXIT outer_loop WHEN c > 200;
END LOOP inner loop;
END LOOP outer_loop;
!
User Defined Records
DECLARE
TYPE DeptRectyp IS
RECORD(deptno number(2),dname char(14),loc char(13));
dept_rec DeptRectyp;
..
BEGIN
SELECT deptno ,dname,loc into dept_rec FROM dept
WHERE deptno = 30;
..
END;
!
@
PL/SQL Tables
!
PL/SQL Tables
• PL/SQL tables declared in two steps
Define table type
TYPE type_name
IS TABLE OF {column_type | variable%type |
table.column%column} [NOT NULL]
INDEX BY BINARY_INTEGER;
!
PL/SQL Tables
DECLARE
TYPE EnameTabTyp IS TABLE OF emp.ename%TYPE
INDEX BY BINARY_INTEGER;
TYPE SalTabTyp IS TABLE OF emp.sal%TYPE
INDEX BY BINARY_INTEGER;
ename_tab EnameTabTyp;
sal_tab SalTabTyp;
I BINARY_INTEGER := 0;
...
!
PL/SQL Tables
BEGIN
--load employee name and salaries into PL/SQL tables
FOR emprec IN (SELECT ename,sal from emp) LOOP
I := I +1;
ename_tab(I) := emprec.ename;
sal_tab(I) := emprec.sal;
END LOOP;
-- process the tables
process_sals(ename_tab,sal_tab);
..
END;
!
Cursor
• A work area to execute SQL statements and store
processing information.
• Every SQL DML statement processed by PL/SQL
has an associated CURSOR.
• Two Types of CURSORS
– 1. EXPLICIT
• Multiple row SELECT Statements
– 2. IMPLICIT
• All INSERT Statements
• All UPDATE Statements
• All DELETE Statements
• Single row SELECT....INTO Statements
!
@
Explicit Cursor:
Use
• STEP 1. Declare the cursor
Syntax:
DECLARE
CURSOR <cursor name>
IS <regular select: statement>;
• Quick Notes:
– The <regular select statement> must NOT
include the INTO clause in a single-row
SELECT...INTO statement
– Declared cursors are scoped just like variables.
!
Explicit Cursor:
Use
Example:
DECLARE
x NUMBER(7,2);
total NUMBER(5);
lower_sal_limit CONSTANT NUMBER(4) := 1200;
CURSOR c1 IS
SELECT ename FROM emp
WHERE sal > lower_ sal _limit;
BEGIN…
!
@
Explicit Cursor:
Use
• STEP 3. Fetch data from the cursor
Syntax:
FETCH <cursor name> into <var1, var2..>;
-- fetches values to the variable and increments the pointer to the
next value.
• Quick Notes:
– Retrieves one row of data from the cursor, and stores it
in the specified variables (similar to how a single-row
SELECT works).
– There must be exactly one INTO variable for each
column selected by the SELECT statement.
– The first column gets assigned to var1, the second
assigned var2, etc.
!
@
Explicit Cursor: Use
• STEP 4. Close the cursor
Syntax:
CLOSE <cursor name>;
-- closes the cursor , and the result set becomes
unidentified
( NOTE:
Once a cursor is closed ,it can be
reopened . Any other operation on a closed
cursor raises predefined exception
INVALID_CURSOR.)
!
@
Explicit Cursor:
Attributes
• %NOTFOUND
LOOP
FETCH my_cursor INTO my_ename, my_sal;
EXIT WHEN my_cursor%NOTFOUND;
--- process data here
END LOOP;
• %FOUND
FETCH my_cursor INTO my_ename, my_ sal;
WHILE my_cursor%FOUND LOOP;
-- process data here
FETCH my_cursor INTO my ename, my_sal;
END LOOP;
!
Explicit Cursor: Attributes
• %ROWCOUNT
LOOP
FETCH my_cursor INTO my_ename, my_sal;
EXIT WHEN (my_cursor%NOTFOUND)
OR (my_cursor%ROWCOUNT>
10);
--process data here
END LOOP;
• %ISOPEN
IF my_cursor%ISOPEN THEN
FETCH my_cursor into my_ename,my_sal;
ELSE
OPEN my_cursor;
END IF;
!
@
Explicit Cursor
Example: Reopening a cursor
DECLARE
total_bonus NUMBER(8,2) := 0;
min _sal emp.sal%TYPE := 1000;
-- start guessing
bonus_ amt emp . sal%TYPE;
CURSOR bonus IS SELECT sal *.10
FROM emp WHERE sal > min_ sal;
!
@
Explicit Cursor
Example: Reopening a cursor
!
@
Explicit
• Cursor FOR LoopsCursor:
specify aFOR loops
sequence of
statements to be repeated once for each row that is
returned by the cursor.
Syntax
FOR <record_name> IN <cursor_name> LOOP
-- statements to be repeated go
here
END LOOP;
– For each row that satisfies the query associated with the
cursor, an implicit FETCH is executed into the
components of record_name.
!
Explicit
DECLARE
Cursor: FOR
sal_limit NUMBER(4) := 0;
loops
total_sal NUMBER(9,2):=0;
CURSOR my_cursor IS SELECT ename,sal FROM
emp WHERE sal > sal_limit;
BEGIN
sal_limit := 1200;
FOR cursor_row IN my_cursor LOOP
INSERT INTO temp(message,num) VALUES
(cursor_row.ename, cursor_row.sal);
total_sal := total_sal +
cursor_row.sal;
END LOOP;
COMMIT;
END;
!
@
Implicit
• An Implicit Cursor Cursorassociated
is automatically
with any SQL DML statement that does not have
an explicit cursor associated with it.
– INSERT, UPDATE , DELETE, SELECT...INTO
• Quick Notes:
– Implicit cursor is called the "SQL'' cursor - it stores
information concerning the processing of the last SQL
statement not associated with an explicit cursor
– OPEN, FETCH, and CLOSE don't apply.
– All cursor attributes apply.
!
@
• SQL %NOTFOUND
Implicit Cursor: Attributes
UPDATE emp SET sal = sal*10.0 WHERE ename = 'WARD';
IF SQL%NOTFOUND THEN -- WARD wasn't found
INSERT INTO emp(empno, ename, sal)
VALUES (4, 'WARD', 9999);
END IF;
• SQL %FOUND
• SQL%ROWCOUNT
DELETE FROM baseball_team WHERE batting_avg< . 100;
• SQL%ISOPEN
(always evaluates to FALSE) !
@
BREAK