Database Engineering: PL/SQL
Database Engineering: PL/SQL
PL/SQL
Korra Sathya Babu
Department of Computer Science
NIT Rourkela
11/29/15
Lecture 3
Oracle
Oracle
Oracle
Oracle
Oracle
Release
Release
Release
Release
Release
11/29/15
Version
Version
Version
Version
Version
8 released with SQL object technology and support for terabytes of data
8i Internet technology
9i Application Server released with support for building portals
10g support of grid computing
11g on windows and linux based systems
Lecture 3
DDL
DML
Classification of SQL
Commands
DCL
GRANT, REVOKE
TCL
11/29/15
Lecture 3
Hospital Database
Lecture 3
SQL Vs PL/SQL
SQL
Non Procedural capabilities
Time consuming processing
No error handling routines
PL/SQL
Procedural capabilities
Reduced network traffic
Error handling
Resource sharing
11/29/15
Lecture 3
PL/SQL Block
DECLARE
Declaration of memory variables, constants, cursors, etc in PL/SQL
BEGIN
SQL executable statements
Pl/SQL executable statements
EXCEPTION
SQL or PL/SQL code to handle errors that may arise during the
execution of the ode block between BEGIN and EXCEPTION section
END ;
11/29/15
Lecture 3
File creation,execution
Steps to Create a PL/SQL Program and execute
Start Notepad
Type the Program code
Type a slash (/) at the end of the program and press enter
Save the file (example MyFirstProgram)
Switch back to SQL Plus Prompt
Execute the Program by typing one of the following commands
START MyFirstProgram
@ MyFirstProgram
EXEC MyFirstProgram
RUN MyFirstProgram
11/29/15
Lecture 3
Program
11/29/15
Lecture 3
:||n3);
Program
11/29/15
Lecture 3
IF Statement
IF <Condition> THEN
<Action>
ELSE
<Action>
11/29/15
Lecture 3
Nested IF Statement
IF <Condition> THEN
<Action>
ELSIF <Condition> THEN
<Action>
ELSIF <Condition> THEN
<Action>
END IF;
10
Example (create a file to execute FOR loop and display the variable)
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE(This is a demo for FOR Loop);
FOR CTR IN 1 .. 5 LOOP
-- The variable CTR neednt be declared
DBMS_OUTPUT.PUT_LINE(Loop Number:||CTR);
END LOOP;
END;
/
SET SERVEROUTPUT OFF
11/29/15
Lecture 3
11
Lecture 3
12
Lecture 3
13
Cursors
Cursors are used when the SQL select statement is expected to return
more than one row
Cursor is a buffer which will store the results of the recent query
11/29/15
Lecture 3
14
Cursor
Syntax
CURSOR <Cursorname> IS <SELECT Statement>;
Example
CURSOR CUR_SAL IS SELECT EMPNO, BASIC FROM SALARY WHERE BASIC > 2000;
11/29/15
Lecture 3
15
Cursor Attributes
There are four Cursor Attributes used to provide information
on the status of the cursor
%NOTFOUND
To determine if a row was retrieved. Use after FETCH
NOTFOUND is TRUE if a row was not retrieved
NOTFOUND is FALSE if a row was retrieved
%FOUND
To determine if a row was retrieved. Use after FETCH
%ROWCOUNT
ROWCOUNT is zero when the cursor is opened
ROWCOUNT returns the number of rows retrieved
%ISOPEN
To determine if a Cursor is open
ISOPEN is TRUE if a cursor is open
ISOPEN is FALSE if a cursor is not open
11/29/15
Lecture 3
16
Cursor Demonstration
Write a code to display no, name and basic of 3 highest paid employees
--To display no, name and basic of 3 highest paid employees
SET SERVEROUTPUT ON
DECLARE
ENO SALARY.EMP_NO%TYPE;
ENAME EMP.EMP_NAME%TYPE;
EBASIC SALARY.BASIC%TYPE;
CURSOR CUR_SAL IS SELECT DISTINCT SALARY.EMP_NO, EMP.EMP_NAME, SALARY.BASIC
FROM SALARY, EMP
WHERE SALARY.EMP_NO = EMP.EMP_NO
ORDER BY BASIC DESC;
BEGIN
OPEN CUR_SAL;
LOOP
FETCH CUR_SAL INTO ENO, ENAME, EBASIC;
EXIT WHEN CUR_SAL%NOTFOUND;
EXIT WHEN (CUR_SAL%ROWCOUNT>3);
DBMS_OUTPUT.PUT_LINE(ENO|| ||ENAME|| ||EBASIC);
END LOOP
CLOSE CUR_SAL;
END;
/
SET SERVEROUTPUT OFF
11/29/15
Lecture 3
17
Triggers
A PL/SQL code that defines some action that the database
should take when some database related events occurs
Triggers are executed when a DML command(Insert, Delete, Update)
is performed on the table for which the trigger is written
Triggers are executed automatically
CREATE TRIGGER Privilege (its a part of Resource role) is
required for a user to create a trigger
Usage
11/29/15
Lecture 3
18
Triggers
Syntax
CREATE [or REPLACE ] TRIGGER <Trigger_name>
[BEFORE/AFTER]
[DELETE/INSERT/UPDATE] [of column]
ON [user]. Table
[FOR EACH ROW]
[WHEN <condition>]
[PL/SQL Block]
..
..
11/29/15
Lecture 3
19
Triggers
Trigger will be
fired for each row
of action
otherwise for
each statement
only
example:
Triggers can be many for a single table but only one of each
type (update, delete or insert)
Triggers cannot include COMMIT, ROLLBACK, SAVEPOINT
11/29/15
Lecture 3
20
Procedures
Stored database objects that use a
PL/SQL statement(s) in their body
CREATE
CREATEOR
ORREPLACE
REPLACE
PROCEDURE
PROCEDURE procedure_name
procedure_name(( parameters
parameters)) IS
IS
BEGIN
BEGIN
procedure_body
procedure_body
END;
END;
DROP
DROPPROCEDURE
PROCEDURE<my-proc>;
<my-proc>;
21
Example procedure
Define the procedure:
CREATE
CREATEPROCEDURE
PROCEDUREtestProcedure
testProcedureAS
AS
BEGIN
BEGIN
INSERT
INSERTINTO
INTOStudent
Student VALUES
VALUES(5,
(5, 'Joe');
'Joe');
END;
END;
or
CALL
CALLtestProcedure
testProcedure
22
Example procedure
CREATE OR REPLACE PROCEDURE HELLO IS
BEGIN
DBMS_OUTPUT.PUT_LINE(Hello World);
END;
11/29/15
Lecture 3
23
Which
Whichififyou
you call,
call, will
will promptly
promptly display:
display:
SQL>
SQL> CALL
CALLDISPN(1234567891);
DISPN(1234567891);
NN isis 1234567891
1234567891
24
Multiple Parameters
You can also have multiple parameters. For example, you can
accept A and B and display their sum and product.
CREATE
CREATE OR
OR REPLACE
REPLACE
PROCEDURE
PROCEDURE DISP_AB
DISP_AB (A
(AINT,
INT,BB INT)
INT) IS
IS
BEGIN
BEGIN
DBMS_OUTPUT.PUT_LINE(A
DBMS_OUTPUT.PUT_LINE(A++ BB == |||| (A
(A++ B));
B));
DBMS_OUTPUT.PUT_LINE(A
DBMS_OUTPUT.PUT_LINE(A** BB == |||| (A
(A** B));
B));
END;
END;
25
Functions
Like procedures but with return values
CREATE
CREATE OR
OR REPLACE
REPLACE
FUNCTION
FUNCTION function_name
function_name (function_params)
(function_params)
RETURN
RETURN return_type
return_type IS
IS
BEGIN
BEGIN
function_body
function_body
RETURN
RETURN something_of_return_type;
something_of_return_type;
END;
END;
DROP
DROP FUNCTION
FUNCTION <functionName>;
<functionName>;
Function example
CREATE
CREATEOR
ORREPLACE
REPLACE
FUNCTION
FUNCTIONADD_TWO
ADD_TWO(A
(AINT,B
INT,BINT)
INT)RETURN
RETURNINT
INTIS
IS
BEGIN
BEGIN
RETURN
RETURN(A
(A++B);
B);
END;
END;
To
Torun
runit,it,well
wellwrite
writeaasmall
smallpiece
pieceof
ofcode
codethat
thatcalls
callsthis:
this:
BEGIN
BEGIN
DBMS_OUTPUT.PUT_LINE(RESULT
DBMS_OUTPUT.PUT_LINE(RESULTIS:
IS: ||||ADD_TWO(12,34));
ADD_TWO(12,34));
END;
END;
Which
Whichgives
givesthe
theoutput:
output:
RESULT
RESULTIS:
IS:46
46
27