PL/SQL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

PL/SQL

Sabyasachi Moitra
moitrasabyasachi@hotmail.com
What is PL/SQL?
• The PL/SQL programming language was developed by Oracle
Corporation in the late 1980s as procedural extension language for
SQL and the Oracle relational database.
• PL/SQL is a completely portable, high-performance transaction-
processing language.
• PL/SQL is tightly integrated with SQL.
• It supports structured programming through functions and procedures.

2
PL/SQL Block Structure

3
• DECLARE
- Declaration of memory variables, constants, cursors, etc. in PL/SQL.
- Optional block.
• BEGIN
- SQL & PL/SQL executable statements.
- Actual data manipulation, retrieval, looping & branching constructs
are specified in this block.
• EXCEPTION
- SQL or PL/SQL code to handle errors that may arise during the
execution of the code block between BEGIN & EXCEPTION section.
- Optional block.
• END
- Marks the end of the PL/SQL block.

4
SELECT Operation

5
Output
THE JOB OF KING IS PRESIDENT
THE SALARY OF KING IS 5000
PL/SQL procedure successfully completed.

6
INSERT Operation

7
UPDATE Operation

8
DELETE Operation

9
IF…ELSE Statement

10
Output
OPERATIONS
PL/SQL procedure successfully completed.

11
FOR Loop

12
Output
Do not sleep in the class!!
Do not sleep in the class!!
Do not sleep in the class!!
Do not sleep in the class!!
Do not sleep in the class!!
PL/SQL procedure successfully completed.

13
WHILE Loop

14
Output
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.

15
DO…WHILE Loop

16
Output
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.

17
CURSOR
• Private workspace used to evaluate any query.
• Oracle creates a memory area (context area), for processing an SQL
statement, which contains all the information needed for processing
the statement.
• A cursor is a pointer to the context area.
• It holds the rows (one or more) returned by a SQL statement.
• There are two types of cursors:-
- Implicit Cursor – Automatically created by Oracle whenever an SQL
statement is executed.
- Explicit Cursor – Created by the programmer him(her)self.

18
Attributes
Attribute Description
%FOUND Returns true when the last executable statement
affects at least one row.
%NOTFOUND Returns true when the last executable statement
doesn’t affect at least one row.
%ISOPEN Returns true if the cursor is already open.
%ROWCOUNT Returns the number of rows affected by the last
executable statement.

19
How CURSOR works?
open

fetch

exit
condition

close

20
Example

21
Output
ACCOUNTING
RESEARCH
SALES
OPERATIONS
PL/SQL procedure successfully completed.

22
CURSOR with FOR Loop

23
Output
ACCOUNTING
RESEARCH
SALES
OPERATIONS
PL/SQL procedure successfully completed.

24
CURSOR with WHILE Loop

25
Output
TURNER
ADAMS
JAMES
FORD
MILLER
SMITH
ALLEN
WARD
MARTIN
BLAKE
CLARK
SCOTT
KING
JONES
PL/SQL procedure successfully
completed.
26
Fetching an entire row in a CURSOR

27
Output
10
ACCOUNTING
NEW YORK
20
RESEARCH
DALLAS
30
SALES
CHICAGO
40
OPERATIONS
BOSTON
PL/SQL procedure successfully completed.

28
EXCEPTION – Try…Catch

29
Output
Enter value for dno: 50
old 4: cursor c1 is select dname from dept where deptno = &dno;
new 4: cursor c1 is select dname from dept where deptno = 50;
Data is not in table!!
PL/SQL procedure successfully completed.

30
EXCEPTION – Throws

31
Output
ERROR at line 1:
ORA-20005: Data is not in Table
ORA-06512: at line 10

32
Creating PROCEDURE
create or replace procedure
get_dname(dno number, dnm out varchar2)
as
begin
select dname into dnm from dept where deptno = dno;
end get_dname;
/
Output
Procedure created.

[NOTE: If output is “Procedure Created with Compilation error”, then to


see the errors run the “show error” statement in the editor.]

33
Calling PROCEDURE
set serveroutput on
declare
deptname dept.dname%type;
begin
get_dname(20, deptname);
dbms_output.put_line('Department is ' || deptname);
end;
/
Output
Department is RESEARCH
PL/SQL procedure successfully completed.

34
Creating FUNCTION
create or replace function
get_location(dno in dept.deptno%type)
return varchar2
is
deptloc dept.loc%type;
begin
select loc into deptloc from dept where deptno = dno;
return deptloc;
end get_location;
/
Output
Function created.

35
Calling FUNCTION
via PL/SQL via SQL
set serveroutput on select get_location(20) from dept;
declare
deptloc varchar2(10) := Output
get_location(20); LOC
begin -------------
dbms_output.put_line('Location DALLAS
is ' || deptloc);
end;
/
Output
Location is DALLAS
PL/SQL procedure successfully completed.

36
PROCEDURE vs FUNCTION
Procedure Function
Must not have the return type. Must have the return type.
May return 0 or more values. May return only one values.
We can call functions from the procedure. Procedure cannot be called from function.
Procedure supports input and output parameters. Function supports only input parameter.
Three types of arguments:-
IN  Input type
OUT  Output type
INOUT  Both type

37
PACKAGE
• Packages are schema objects that groups logically related PL/SQL
types, variables, and subprograms.
• A package will have two mandatory parts:−
- Package creation
- Package body definition

38
PACKAGE Creation
create package name_salary
as
procedure get_emp_name(eno number);
procedure get_emp_salary(eno number);
end name_salary;
/
Output
Package created.

39
PACKAGE Body Definition
create package body name_salary
as
procedure get_emp_name(eno number)
is
empname emp.ename%TYPE;
begin
select ename into empname from emp where empno = eno;
dbms.output.put_line(empname);
end get_emp_name;
procedure get_emp_salary(eno number)
is
salary emp.sal%TYPE;
begin
select sal into salary from emp where empno = eno;
dbms.output.put_line(salary);
end get_emp_salary;
end name_salary;
/

Output
Package body created.

40
Accessing PACKAGE
set serveroutput on
begin
name_salary. get_emp_name(7839);
name_salary. get_emp_name(7839);
end;
/

Output
KING
5000
PL/SQL procedure successfully completed.

41
TRIGGER
• A user-defined PL/SQL block associated with a specific table.
• Implicitly fired (executed) when a triggering statement is issued against
the table.
• Made up of (parts):-
- Triggering event (INSERT/UPDATE/DELETE)
- Trigger type (BEFORE/AFTER, per statement or per row)
- Trigger restriction (optional)
* WHEN clause
- Trigger action
* PL/SQL BLOCK

42
Creating TRIGGER
create or replace trigger salary_check
before insert or update of sal, job on emp
for each row
when (new.job <> 'PRESIDENT')
declare
minsal number;
maxsal number;
begin
select min(sal), max(sal) into minsal, maxsal from emp
group by job
having job = :new.job;
if (:new.sal < minsal or :new.sal > maxsal) then
raise_application_error(-20100, 'Salary out of Range Error!');
end if;
end;
/

Output
Trigger created.

43
Firing TRIGGER
insert into emp values(8000, ‘ROSHNI', 'MANAGER', 7839,
'01-JAN-2000', 1000, 1000, 40);

Output
ORA-20100: Salary out of Range Error!
ORA-06512: at "SALARY_CHECK", line 8
ORA-04088: error during execution of trigger 'SALARY_CHECK'

44
Enabling, Disabling & Dropping
TRIGGER
ALTER TRIGGER <trigger name> DISABLE;
ALTER TABLE <table name> DISABLE TRIGGER <trigger name>;

ALTER TRIGGER <trigger name> ENABLE;


ALTER TABLE <table name> ENABLE TRIGGER <trigger name>;

DROP TRIGGER <trigger name>;

45
Applications of TRIGGER
• Maintaining derived fields
• Implementing complex security rules
• Enforcing complex business rules
• Performing value-based auditing
• Making implied changes
• Maintaining table replication

46
References
• Courtesy of TutorialsPoint – PL/SQL Tutorial. URL:
http://www.tutorialspoint.com/plsql/
• Ivan Bayross, SQL, PL/SQL The Programming Language of Oracle, 4th
Revised Edition, BPB Publications, 2009
• Courtesy of JavaTPoint – Java JDBC Tutorial. URL:
http://www.javatpoint.com/java-jdbc

47

You might also like