PLSQLPPT 2263
PLSQLPPT 2263
Introduction
Benefits
Basic Constructs
Anonymous blocks
Procedures
Functions
Packages
Triggers
Cursors
Dynamic SQL
Introduction
Embedded SQL (PL/SQL, JAVA/ VB & DB)
Database Server Level Programming
(PL/SQL, Transact-SQL, IBM DB2-Cobol, ProC,
ProCobol)
Database Client Programming
Developer 9i, JDeveloper 9i, Java (J2EE), VB, .Net
- Benefits
More powerful than pure SQL because it
combines the power of SQL and
Iteration (loops)
Selection (Ifs)
Cursors
Block Structures
Stored Procedures
etc.
- Basic Constructs
Basic Structure
Running a program
Variables
SELECT INTO
Comments
IFs
LOOPs
Output
-- Basic Structure
DECLARE
BEGIN
EXCEPTION
END;
5ICS 33
- OUTPUT …
SET SERVEROUTPUT ON;
BEGIN
DBMS_OUTPUT.PUT_LINE('This is my fist program');
END;
/
6ICS 334
--- Basic Structure: Example
DECLARE
v_id INTEGER;
v_empno NUMBER;
BEGIN
v_id := 1234567;
SELECT EMPNO
INTO V_EMPNO
FROM EMP
WHERE empno = v_id;
DBMS_OUTPUT.PUT_LINE('Value is '||v_empno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No record exists');
END;
/
7ICS 334
--- Basic Structure: Example
DECLARE
v_id INTEGER;
BEGIN
v_id := 1234567;
DELETE
FROM EMP
WHERE id = v_id;
END;
/
8ICS 334
-- Running a Program
DECLARE
BEGIN
EXCEPTION
END;
/
9ICS 334
-- Variables
Common Data Types
NUMBER
DATE
INTEGER
VARCHAR2
CHAR
BOOLEAN
Declaration
V_salary NUMBER(9,2);
V_id INTEGER;
V_dob DATE;
V_name VARCHAR2(35);
V_gender CHAR;
V_salary emp.salary%TYPE;
10ICS 334
-- SELECT INTO
DECLARE
v_job emp.job%TYPE;
v_sal emp.sal%TYPE;
v_empno emp.empno%TYPE;
BEGIN
v_empno := 1234567;
END;
/
11ICS 334
-- Another Example
declare
v_ename emp.ename%TYPE;
v_sal emp.sal%TYPE;
begin
select ename, sal
into v_ename, v_sal
from emp
where empno = '7844';
DBMS_OUTPUT.PUT_LINE('solution is:'||v_ename||'---'||v_sal);
exception
When others then
DBMS_OUTPUT.PUT_LINE('no record');
end;
/
BEGIN
END;
/
13ICS 334
-- IFs
IF – END IF
IF – ELSE – END IF
IF – ELSIF – ELSE – END IF
14ICS 334
--- IF – END IF
DECLARE
…
…
BEGIN
…
…
v_commison := 7500;
IF v_dept = 10 THEN
v_commision := 5000;
END IF;
…
…
END;
/
15ICS 334
--- IF – ELSE – END IF
DECLARE
…
…
BEGIN
…
…
IF v_dept = 10 THEN
v_commision := 5000;
ELSE
v_commision := 7500;
END IF;
…
…
END;
/
16ICS 334
--- IF – ELSIF – ELSE – END IF
DECLARE
…
…
BEGIN
…
…
IF v_dept = 10 THEN
v_commision := 5000;
ELSIF v_dept = 20 THEN
v_commison := 5500;
ELSIF v_dept = 30 THEN
v_commison := 6200;
ELSE
v_commision := 7500;
END IF;
…
…
END;
/
17ICS 334
-- LOOPs
LOOP – EXIT WHEN – END LOOP
FOR – LOOP – END LOOP
WHILE – LOOP – END LOOP
18ICS 334
--- LOOP – EXIT WHEN – END LOOP
DECLARE
…
v_deptno dept.deptno%TYPE := 50;
v_counter integer := 1;
…
BEGIN
…
LOOP
INSERT INTO dept(deptno)
VALUES(v_deptno);
v_counter := v_counter + 1;
v_deptno := v_deptno + 10;
EXIT WHEN v_counter > 5;
END LOOP;
…
END;
/
19ICS 334
--- FOR – LOOP - END LOOP
DECLARE
…
v_deptno dept.deptno%TYPE := 50;
v_counter integer;
…
BEGIN
…
FOR v_counter IN 1..5 LOOP
INSERT INTO dept(deptno)
VALUES(v_deptno);
v_deptno := v_deptno + 10;
END LOOP;
…
END;
/
20ICS 334
--- WHILE – LOOP - END LOOP
DECLARE
…
v_deptno dept.deptno%TYPE := 50;
v_counter integer;
…
BEGIN
…
v_counter := 1;
WHILE v_counter <= 5 LOOP
INSERT INTO dept(deptno)
VALUES(v_deptno);
v_deptno := v_deptno + 10;
END LOOP;
…
END;
/
21ICS 334
… - OUTPUT
SET SERVEROUTPUT ON;
DECLARE
v_sum_sal emp.sal%TYPE;
v_deptno emp.deptno%TYPE := 10;
BEGIN
SELECT SUM(sal)
INTO v_sum_sal
FROM emp
WHERE deptno = v_deptno;
END;
/
22ICS 334
- Anonymous Block
DECLARE
v_id INTEGER;
BEGIN
v_id := 1234567;
DELETE
FROM EMP
WHERE id = v_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No record exists');
END;
/
23ICS 334
-- Nesting Anonymous Blocks
24ICS 334
Exceptions
Examples are
NO_DATA_FOUND
OTHERS
25ICS 334
- Procedure
Is a block with a name
The DECLARE key word is not used
Parameters can be
IN
OUT
IN OUT
Is stored (USER_SOURCE)
26ICS 334
-- Creating or Replacing a Procedure …
CREATE OR REPLACE PROCEDURE pname( ) IS
BEGIN
EXCEPTION
END;
/
27ICS 334
… -- Creating or Replacing a Procedure
SET SERVEROUTPUT ON;
v_job EMP.job%TYPE;
v_sal EMP.sal%TYPE;
BEGIN
EXCEPTION
END;
/
SQL> Show errors
SQL> execute proc_test(5893);
28ICS 334
-- Invoking a Procedure
DECLARE
…
BEGIN
…
proc_test(’23’);
…
END;
/
Or
SQL> exec proc_test(‘1123’)
29ICS 334
Another Example
SQL> ed
Wrote file afiedt.buf
Procedure created.
31ICS 334
-- Creating or Replacing a Function …
CREATE OR REPLACE FUNCTION fname( )
RETURN datatype IS
BEGIN
EXECPTION
END;
/
32ICS 334
… -- Creating or Replacing a Function
v_sum_sal emp.sal%TYPE;
BEGIN
SELECT SUM(sal)
INTO v_sum_sal
FROM emp
WHERE deptno = p_deptno;
RETURN v_sum_sal;
END;
/
33ICS 334
-- Invoking a Function
DECLARE
…
v_sal emp.sal%TYPE;
…
BEGIN
…
v_sal := sum_dept_sal(10);
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_sal));
…
END;
/
34ICS 334
Example, passing parameters
35ICS 334
Exercise
Write down a procedure that displays all
records (all columns) from EMP table. Make
sure program should restricted to fix number
of records of this table. Display Date and
TIME portions of column that has data type
DATE.
Write down a procedure that can insert large
number of records entered at the time of its
execution. You can use EMP table and
appending counter with some columns’ values
36ICS 334
- Triggers
Is a stored subprogram associated with a
table.
Are fired by an event
Are mainly used for
Security
Enforce complex integrity constraint
Prevent invalid transaction
Event logging
37ICS 334
-- Creating or Replacing Triggers
38ICS 334
… -- Trigger with function usage Example
39ICS 334
… --Exercise
40ICS 334
- Cursors …
Is a pointer to a row.
Its is mainly used with tables which return
more than one row.
It is handled implicitly and explicitly.
41ICS 334
… - Cusrors …
CURSOR c_emp IS
SELECT empno, ename, job
FROM emp
WHERE deptno = 20;
42ICS 334
… - Cursors
DECLARE
CURSOR c_emp IS
SELECT empno, ename, job
FROM emp
WHERE deptno = 20;
BEGIN
43ICS 334
- Example
Given a table with first
three columns are
composite keys. Write
a PL/SQL program
using cursor to update
cat_template1 with
LONG column
cat_template.
Assuming this tables
contains more than
40,000 records.
44ICS 334
… - Example’s solution
45ICS 334
Dynamic SQL
begin
execute immediate 'create table tt(id number(3))';
end;
/
46ICS 334
Program to find even or odd
Declare
n number(4):=&n;
Begin
if mod(n,2)=0
then
dbms_output.put_line(n||' even number');
else
dbms_output.put_line(n||' odd number');
end if;
end;
/
Execution:
SQL> @e:\plsql\even.sql
Enter value for n: 5
old 2: n number(4):=&n;
new 2: n number(4):=5;
5 odd number
47ICS 334
Even number
Declare
NUM1 number:=0;
begin
loop
NUM1 := NUM1+2;
dbms_output.put_line (NUM1||’,’);
exit when NUM1=100;
end loop;
end;
48ICS 334
Procedure:
declare
n number(4):=&n;
s number(4):=0;
r number(4);
begin
while n>0
loop
r:=mod(n,10);
s:=(s*10)+r;
n:=trunc(n/10);
end loop;
dbms_output.put_line(‘the reverse number is:’);
dbms_output.put_line(s);
end;
/
Execution:
SQL> @e:\plsql\rev.sql
Enter value for n: 457
old 2: n number(4):=&n;
new 2: n number(4):=457;
49ICS 334