11G PLSQL
11G PLSQL
PLSQL NOTES
INDEX
• VARIABLE DECLARATION . . . . . . . . 05
• CONTROL STATEMENTS . . . . . . . . . 18
• COMPOSITE VARIABLES . . . . . . . . 26
• EXPLICIT CURSORS . . . . . . . . . . 31
• EXCEPTIONS . . . . . . . . . . . . . 43
• PROCEDURES . . . . . . . . . . . . . 52
• FUNCTIONS . . . . . . . . . . . . . 55
• PACKAGES . . . . . . . . . . . . . . 58
• ORACLE PACKAGES . . . . . . . . . . 67
• DYNAMIC SQL . . . . . . . . . . . . 75
• TRIGGERS . . . . . . . . . . . . . . 77
PLSQL NOTES
Server Connection
Important Instructions
• Each student has his own unix login id to server.
• Use ssh -l command to login to server.
e.g. ssh -l nagnath 172.24.8.60
• in above case nagnath is unix id.
• Default password is xxxxxx for all students.
• Change password using passwd command after first
login.
PL/SQL PROCEDURAL
PLSQL BLOCK STATEMENT
BLOCK EXECUTION
The SQL engine will parse and execute the query or DML
statement and return the expected output back to the PL/SQL
engine.
This switch between the two engines is called
context switching.
SQL
DATABASE
PLSQL
VARIABLE DECLARATION
DATA DICTIONARY VIEWS
• USER_OBJECTS
• USER_TABLE
• USER_VIEW
• USER_INDEX
• USER_SEQUENCE
• USER_SOURCE
• USER_CONSTRAINTS
Types of variables
CHAR [(maximum length)]
VARCHAR2 (maximum length)
LONG
NUMBER [(precision , scale)]
BINARY INTEGER
PLS_INTEGER
BOOLEAN
BINARY INTEGER:-
Base type for integer between -2,147,483,647
and 2,147,483,647
PLS_INTEGER :-
Base type for signed integers between -
2,147,483,647 and 2,147,483,647.PLS_INTEGER values require
less storage and are faster than NUMBER and BINARY_INTEGER
values
BOOLEAN :-
Base type that stores one of three possible
values used for logical calculations:TRUE,FALSE,NULL
This is the first program in plsql to print hello
world.
as
-- First Program of Plsql
-- This Program Prints
-- Hello World on the Screen
begin
dbms_output.put_line('Hello World');
end;
--OUTPUT
SQL> @sp11.sql
Procedure created.
No errors.
Hello Word
dbms_output.put_line('Hello World')
--Semi colon missing at the end
end;
/
OUTPUT
SQL> @sp11_1.sql
Warning: Procedure created with compilation errors.
SQL> show errors
Errors for PROCEDURE SP11_1:
LINE/COL ERROR
-----------------------------------------------------------------
7/1 PLS-00103: Encountered the symbol
:= . ( % ;
as
v_myname CHAR(50);
v_myage NUMBER(2) NOT NULL := 21;
v_myname := ‘Nagnath’;
dbms_output.put_line('My Name is '||v_myname);
end;
/
show errors
OUTPUT:
SQL> @sp12.sql
Procedure created.
No errors.
SQL> exec sp12
My Name is Nagnath
My Age is 21
PL/SQL procedure successfully completed
Passing value to variables.
as
begin
dbms_output.put_line('Name Entered By User: '||l_name);
OUTPUT:
SQL> @sp13.sql
Procedure created.
No errors.
SQL> exec sp13('nagnath',21);
Name Entered By User: nagnath
Age Entered By User: 21
PL/SQL procedure successfully completed.
Selecting values from database tables.
l_employee_name varchar2(30);
begin
select last_name
into l_employee_name
from Employees
where employee_id = l_employee_id;
OUTPUT
SQL> @sp13_1.sql
Procedure created.
No errors.
SQL> exec sp13_1
Employees Name is: King
PL/SQL procedure successfully completed.
Use of %type variable.
as
l_last_name employees.last_name%type;
begin
select last_name into
l_last_name from
employees
where employee_id = l_employee_id;
end;
/
show errors
SQL> @sp15
Procedure created.
No errors.
SQL> set serveroutput on
SQL> exec sp15(200);
Name of Employee is: Whalen
PL/SQL procedure successfully completed.
Use of %type variable
begin
select last_name
into l_last_name
from employees
where employee_id = 101;
dbms_output.put_line(l_last_name);
end;
/
show errors
OUTPUT:
SQL> @sp14.sql
Procedure created.
No errors.
SQL> exec sp14
Kochhar
PL/SQL procedure successfully completed.
Scope of variables in procedure.
• Inner procedures can define their own variables with same name
as global variable and access.
SQL> @sp16.sql
Procedure created.
No errors.
In Inner
Block2 2
Local Variable
In Inner
Block2 100
Global
Variable In
Global Variable
PL/SQL procedure successfully completed
DML Statements in Procedure
--OUTPUT
create table test(test_id number,test_name varchar2(10));
Table created.
SQL> @sp17.sql
Procedure created.
No errors.
SQL> exec sp17
PL/SQL procedure successfully completed.
SQL> select * from test;
TEST_ID TEST_NAME
---------- ----------
1 sql
CONTROL STATEMENTS
Control Statement : If -Else Condition
l_hire_date employees.hire_date%type;
l_salary employees.salary%type;
l_years_of_service number := 0;
l_new_salary number := 0;
begin
select hire_date,
salary
into l_hire_date,
l_salary
from employees
where employee_id = p_employee_id;
else
l_new_salary := l_salary;
dbms_output.put_line('No salary raise ');
end if;
update employees
set salary = l_new_salary
where employee_id = p_employee_id;
commit;
end;
/
show errors
• IF condition THEN
• statements;
• statements;]
• [ELSE
• statements;]
• END IF;
OUTPUT
SQL> @sp21.sql
Procedure created.
No errors.
SQL> select salary from employees where employee_id=101;
SALARY
----------
17000
SQL> exec sp21(101);
Giving 10% raise
PL/SQL procedure successfully completed.
SQL> select salary from employees where employee_id=101;
SALARY
----------
18700
l_appraisal varchar2(100);
begin
l_appraisal := CASE p_grade
END;
OUTPUT
SQL> @sp22.sql
Procedure created.
No errors.
SQL> exec sp22('C');
Grade: C Appraisal Good
PL/SQL procedure successfully completed.
i number;
Begin
i := 1;
loop
dbms_output.put_line(to_char(i) );
exit when i >= p_loop_counter;
i := i +
1; end loop;
end;
/
show errors
OUTPUT
SQL> @sp23.sql
Procedure created.
No errors.
SQL> exec sp23(5);
1 2 3 4 5
OUTPUT
SQL> @sp24.sql
Procedure created.
No errors.
SQL> exec sp24(5);
1 2 3 4 5
OUTPUT
SQL> @sp25.sql
Procedure created.
No errors.
SQL> exec sp25(3);
1 2 3
------------------------
3
2
1
PL/SQL procedure successfully completed.
Infinite Loop
as
begin
loop
dbms_output.put_line('Hi');
end loop;
while ( true)
loop
dbms_output.put_line('Hi');
end loop;
end;
/
show errors
COMPOSITE VARIABLES
Composite Variable:Records
begin
select last_name,salary
into emp_sal
from employees
where employee_id = 100;
dbms_output.put_line('Employee Name: '||emp_sal.last_name);
dbms_output.put_line('Employees Salary: '||emp_sal.salary);
end;
/
show errors
• Composite Data type Records stores more than one data type under
single record.
OUTPUT:
SQL> @sp31.sql
Procedure created.
No errors.
SQL> set serveroutput on
SQL> exec sp31
Employee Name: King
Employees Salary: 24000
PL/SQL procedure successfully completed.
dbms_output.put_line('Name: '||emp_record.last_name);
end;
/
show errors
OUTPUT:
SQL> @sp32.sql
Procedure created.
No errors.
SQL> set serveroutput on
SQL> exec sp32
Name: King
Department Id: 90
PL/SQL procedure successfully completed.
emp_array emp_type;
begin
for i in 101..104
loop
select *
into emp_array(i)
from employees
where employee_id = i;
end loop;
for i in emp_array.FIRST..emp_array.LAST
loop
dbms_output.put_line(emp_array(i).first_name||
' '||emp_array(i).last_name);
end loop;
end;
/
show errors
OUTPUT
SQL> @sp33.sql
Procedure created.
No errors.
SQL> exec sp33
Neena Kochhar
Lex De Haan
Alexander Hunold
Bruce Ernst
PL/SQL procedure successfully completed.
arr_1(i) := l_region_name;
end loop;
dbms_output.put_line(arr_1.count());
dbms_output.put_line(arr_1(1));
end;
/
show errors
• This data type is useful when user knows exact length for array
declaration.
OUTPUT
SQL> @sp34.sql
Procedure created.
No errors.
SQL> exec sp34
4
Europe
PL/SQL procedure successfully completed.
16
SQL> @sp18.sql Procedure created. No errors.
---------- ----------
EXPLICIT CURSORS
Explicit Cursors
OUTPUT
SQL> !vi sp61.sql
SQL> @sp61.sql
Procedure created.
No errors.
SQL> exec sp61
Hartstein
13000 Fay 6000
PL/SQL procedure successfully completed.
OUTPUT
SQL> @sp62.sql
Procedure created.
No errors.
SQL> exec sp62
Name: Hartstein
Salary: 13000
Name: Fay
Salary: 6000
PL/SQL procedure successfully completed.
dbms_output.put_line('salary: '||
to_char(rec_c1.last_name));
end loop;
end;
/
show errors
OUTPUT:
SQL> @sp63.sql
Procedure created.
No errors.
SQL> exec sp63
Name: Hartstein
salary:
Hartstein Name:
Fay salary: Fay
PL/SQL procedure successfully completed.
OUTPUT:
SQL> @sp64.sql
Procedure created.
No errors.
SQL> exec sp64
Name: Hartstein
Salary: 13000
Name: Fay
Salary: 6000
PL/SQL procedure successfully completed.
rec_c1 c1%rowtype;
--Declaration of cursor
begin
for rec_c1 in c1(90,'AD_VP') loop
--Passing Paramenters to cursor
dbms_output.put_line('Depatment 80 '||' Job
id is SA_MAN');
dbms_output.put_line('Employee ID:
'|| to_char(rec_c1.employee_id));
dbms_output.put_line('Employee Name: '||
rec_c1.last_name);
end loop;
open c1(deptno,job);
--Passing Paramenters to cursor
loop
fetch c1 into rec_c1;
exit when c1%notfound;
dbms_output.put_line(deptno||' Job id is '||
job);
dbms_output.put_line('Employee ID:
'|| to_char(rec_c1.employee_id));
dbms_output.put_line('Employee Name: '||
rec_c1.last_name);
end loop;
close c1;
end;
/
show errors
SQL> @sp65.sql
Procedure created.
No errors.
60 Job id is IT_PROG
60 Job id is IT_PROG
open c1;
loop
fetch c1 into rec_c1;
exit when c1%notfound;
if rec_c1.salary < 7000 then l_new_sal
:= rec_c1.salary * 1.25;
update employees set salary = l_new_sal
where employee_id = rec_c1.employee_id;
else
l_new_sal := rec_c1.salary * 1.15;
update employees set salary = l_new_sal
where employee_id = rec_c1.employee_id;
end if;
dbms_output.put_line
(rpad(rec_c1.last_name,10)||
rpad(rec_c1.salary,10)||
rpad(l_new_sal,10));
end loop;
close c1;
end;
/
show errors
SQL> @sp66.sql
Procedure created.
No errors.
SQL> @sp67.sql
Procedure created.
No errors.
EXCEPTIONS
as
l_last_name employees.last_name%type;
begin
select last_name
into l_last_name
from employees
dbms_output.put_line(l_last_name);
exception
end;
/
show errors
OUTPUT
SQL> @sp81.sql
Procedure created.
No errors.
commit;
exception
when duplicate_key then dbms_output.put_line('Cannot
insert duplicate
department, department already exist');
end;
/
show errors
SQL> @sp82.sql
Procedure created.
No errors.
SQL> set serveroutput on
SQL> exec sp82
Cannot insert duplicate department, department already exist
PL/SQL procedure successfully completed.
Exceptions : Others
OUTPUT
SQL> @sp83.sql
Procedure created.
No errors.
In Exception
found
l_last_name employees.last_name%type;
l_salary employees.salary%type;
l_new_sal float;
invalid_raise exception;
begin
select last_name,salary
into l_last_name,l_salary
from employees
where employee_id = 100;
--OUTPUTSQL> @sp84.sql
Procedure created.
No errors.
SQL> exec sp84
Not Applicable Salary raise
PL/SQL procedure successfully completed.
begin
dbms_output.put_line('In outer Block');
select salary
into l_salary
from employees
where employee_id = l_employee_id;
begin
select last_name
into l_last_name
from employees;
end;
exception
when no_data_found then
dbms_output.put_line('No data found');
end;
/
show errors
OUTPUT
SQL> @sp86.sql
Procedure created.
No errors.
In outer Block
In Inner Block
In outer Block
No data found
PROCEDURES
FUNCTIONS
Functions
is
l_dept_name departments.department_name%type;
begin
select department_name
into l_dept_name
from departments
where department_id = dept_no;
return l_dept_name;
end;
/
show errors
OUTPUT
SQL> @sp10_1.sql
Function created.
No errors.
LAST_NAME DEPARTMENT_NAME
------------------------- ------------------------------
King Executive
2 from employees
LAST_NAME GET_DEPT_NAME(DEPARTMENT_ID)
------------------------ ------------------------------
King Executive
PACKAGES
Packages
end calculator;
/
show errors
SQL> @cal_pac.sql
Package created.
No errors.
SQL> @cal.sql
Package body
created. No errors.
Addition is: 50
Package
count_add number := 0;
count_subtract number := 0;
end calculator;
/
show errors
is
begin
dbms_output.put_line(text);
end print;
end calculator;
/
show errors
SQL> @cal1.sql
Package body
created. No errors.
SQL> @cal_pac1.sql
Package created.
No errors.
SQL> @cal1.sql
Package body
created. No errors.
Addition is: 7
Subtraction is: 5
end calculator1;
show errors
end calculator1;
/
show errors
Addition is: 30
Addition is: 60
ORACLE PACKAGES
cc => 'mithilesh@server1.example.com',
ora_no := sqlcode;
ora_msg := sqlerrm;
OUTPUT
SQL> @sp11_1.sql
Procedure created.
No errors.
v_file_name :='utl_file.txt';
v_file :=
UTL_FILE.FOPEN(v_dir,v_file_name,'r');
--File Open in read only mode
loop
begin
UTL_FILE.GET_LINE(v_file,v_line);
exception
when no_data_found then
exit;
end;
dbms_output.put_line(v_line);
end loop;
end;
/
show errors
OUTPUT
hello
how r u??
gaurav
OUTPUT
SQL> @sp11_2.sql
Procedure created.
No errors.
SQL> exec
sp11_2 hello
how r u??
gaurav
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'update_sales',
job_type => 'STORED_PROCEDURE',
job_action => 'sp11_4',
start_date => '20-APR-10 03.10.00.000000000 PM
ASIA/CALCUTTA',
repeat_interval => 'FREQ=SECONDLY;INTERVAL=10',
end_date => '20-APR-10 03.11.00.000000000 PM
ASIA/CALCUTTA',
comments => 'My new job');
OUTPUT
15:04:48 SQL> truncate table test_sch;
Table truncated.
15:04:57 SQL> @sp11_6.sql
Procedure created.
No errors.
no rows selected
COUNT(*)
----------
0
15:09:41 SQL> /
COUNT(*)
----------
100
15:10:04 SQL> /
COUNT(*)
----------
200
15:10:14 SQL> /
COUNT(*)
----------
300
15:10:27 SQL> /
COUNT(*)
----------
400
15:10:37 SQL> /
COUNT(*)
----------
500
15:10:46 SQL> /
COUNT(*)
----------
600
15:10:53 SQL> /
COUNT(*)
----------
600
DYNAMIC SQL
Dynamic Sql
dbms_output.put_line(l_count);
end;
/
show errors
OUTPUT
SQL> @sp12_1.sql
Procedure created.
No errors.
TRIGGERS
Trrigers
declare
v_error VARCHAR2(2000);
begin
OUTPUT
SQL> @14_1.sql
Trigger created.
No errors.
2 values(215,'Bhide','abc@gmail.com',sysdate,'IT_PROG',26000);
insert into
employees(EMPLOYEE_ID,LAST_NAME,EMAIL,HIRE_DATE,JOB_ID,SALARY)
ERROR at line 1:
Triggers : Updating,Inserting,Deleting
declare
ChangeType varchar2(10);
begin
/* Use 'I' for an INSERT, 'D' for DELETE, and 'U' for UPDATE. */
if inserting then
ChangeType := 'I';
else
ChangeType := 'D';
end if;
end trig_example;
/
show errors
OUTPUT
SQL> @sp14_2.sql
Trigger created.
No errors.
SQL> select * from CHANGES_RECORD;
no rows selected
SQL> insert into trig_eg values(1,'aaa');
1 row created.
SQL> insert into trig_eg values(2,'bbb');
1 row created.
SQL> update trig_eg set name = 'xxxx' where id = 2;
1 row updated.
SQL> delete from trig_eg where id = 2;
1 row deleted.
SQL> select * from CHANGES_RECORD;
CHANGE USER_NAME CHANGE_DA
---------- ---------- ---------
I GAURAV 18-APR-10
I GAURAV 18-APR-10
U GAURAV 18-APR-10
D GAURAV 18-APR-10
ID NAME
---------- ----------
1 aaa