Basic PL-SQL
Basic PL-SQL
Agenda
Introduction to PL/SQL
Advantages of PL/SQL
PL/SQL programming Fundamentals
Basic usage of Toad Tool
PL/SQL usage in LIMS
What is PL/SQL?
DECLARE
d REAL;
BEGIN
NULL; -- identifiers available here: a (CHAR), b, d
END;
-- identifiers available here: a (CHAR), b
END;
/
PL/SQL Control Structures
Conditional control:
IF Statements
-IF-THEN-ELSE
-IF-THEN-ELSIF
CASE Statements
Simple CASE
Searched CASE
-> IF condition THEN
statement1;
ELSE
statement2;
END IF;
-> CASE
WHEN expression1 THEN satement1;
WHEN expression2 THEN satement2;
ELSE satement3;
END CASE;
Controlling Loop Iterations
Loops repeat a statement or sequence of
statements multiple times.
There are three loop types:
Basic loop :
FOR loop
WHILE loop
Syntaxes:
Basic loop:
LOOP
statement1;
. . .
EXIT [WHEN condition];
END LOOP;
FOR Loop:
FOR counter IN lower_bound..upper_bound
LOOP
statement1;
statement2;
. . .
END LOOP;
Do not declare the counter; it is declared implicitly.
'lower_bound .. upper_bound' is required syntax.
While loop:
WHILE condition
LOOP
statement1;
statement2;
. . .
END LOOP;
Use the basic loop when the statements inside the loop must
execute at least once.
Use the WHILE loop if the condition has to be evaluated at the start
of each iteration.
Use a FOR loop if the number of iterations is known.
Cursor basics and usage in PL/SQL
Cursor Attributes:
Attribute Type Description
%ISOPEN Boolean Evaluates to TRUE if the cursor
is open
%NOTFOUND Boolean Evaluates to TRUE if the most
recent fetch does not return a row
%FOUND Boolean Evaluates to TRUE if the most
recent fetch returns a row;
complement of %NOTFOUND
%ROWCOUNT Number Evaluates to the total number of
rows returned so far
Declare
CURSOR my_cursor IS
SELECT service_id FROM service
where record_type_cd = 'DIGITAL VOIP SERVICE'
and service_id=1938135 and service_version_no=1;
service_id NUMBER(12);
Begin
OPEN my_cursor;
LOOP
FETCH my_cursor INTO service_id;
IF my_cursor%NOTFOUND
THEN
EXIT;
ELSE
Dbms_output.put_line ('service id is' || service_id);
END IF;
END LOOP;
End;
Stored Procedures and Functions
3 Types of modes
IN
Read only
Pass By value
OUT
Write Only
Pass by reference
IN OUT
Read Write
Pass by reference
Example:
Creation
CREATE PROCEDURE raise_sal(
p_id IN NUMBER,
p_amount IN NUMBER )
IS
BEGIN
null;
END raise_sal;
Invocation Example:
Begin
raise_sal(100, 2000);
End;
Package
A package is a unit which encapsulates functions, procedure and
collections.
Once created, can be used as a user defined Library.
Has 2 parts
Specification – Declaration visible through component selecter.
Body – Definition – This is Hidden.
Both can contain TYPEs, variables, procedures and functions.
CREATE [OR REPLACE] PACKAGE package_name IS
declarations ;
END package_name ;
-- c. Fill up the first 100 elements (starting from index 1) with numbers of your choice.
BEGIN
FOR indx IN 1 .. 100
LOOP
my_numbers ( indx ) := indx * 2;
dbms_output.put_line(indx);
dbms_output.put_line(my_numbers(indx));
END LOOP;
END;
/
Nested tables
exception handlers
An exception handler is a sequence of statements to be
processed when a certain exception occurs
When an exception handler is complete processing of
TOO_MANY_ROWS ORA-(01427)
- a single row SELECT returned more than one row
NO_DATA_FOUND ORA-(01403)
- a single row SELECT returned no data
INVALID_CURSOR ORA-(01001)
- invalid cursor was specified
VALUE_ERROR ORA-(06502)
- arithmetic ,numeric, string , conversion, or constraint error occurred.
ZERO_DIVIDE ORA-(01476)
- attempted to divide by zero
DUP_VAL_ON_INDEX ORA-(00001)
- attempted to insert a duplicate value into a column that has a unique
index specified.
Syntax
WHEN <exception_name [OR <exception_name…]>
THEN <sequence of statements>
OR
WHEN OTHERS
THEN -- if used , must be last handler
<sequence of statements>
Eg:
DECLARE
employee_num emp.empno%TYPE;
BEGIN
SELECT empno INTO employee_num FROM emp
WHERE ename= ‘BLAKE’;
INSERT INTO temp VALUES(NULL, empno,Blake's employee_num’);
DELETE FROM emp WHERE ename =‘BLAKE’;
EXCEPTION
WHEN TOO_MANY_ROWS OR NO_DATA_FOUND THEN
ROLLBACK;
INSERT INTO temp VALUES (NULL,’Blake not found, or more than one Blake’);
COMMIT;
E.g.
DECLARE
x NUMBER;
my_exception EXCEPTION; -- a new object type.
Raise your_exception;
RAISE my_exception;
DECLARE
A EXCEPTION;
B EXCEPTION;
C EXCEPTION;
x NUMBER;
BEGIN
…
BEGIN
IF X=1 THEN RAISE A;
ELSEIF X=2 THEN RAISE B;
ELSE RAISE C;
EXCEPTION
WHEN A THEN
...
END;
EXCEPTION
WHEN B THEN
...
END;
PL/SQL usage in LIMS
Useful Documents and links
Data Types