Anda di halaman 1dari 46

Chapter 01

Introduction to Oracle PLSQL

what is oracle PLSQL ?


PLSQL Block Types :-Anonymous ( unnamed)
-Procedures
-Functions
-Packages
-Triggers
PLSQL Block Structure :[Declare]
_________________ v1 ;

( Declaration Section )

_________________ v2 ;
...
Begin
__________________ s1 ; ( Execution section )
__________________ s2 ;
__________________ s3 ;
....

[Exception]

when exception_name then


________________________s4 ;( Exception Section )
________________________s5 ;
..

when Exception_name then


________________________s6 ;
________________________s7 ;
..
End ;
*************************************************************************
*********************************************************
Declaring Variables :variable types :

Scalar : ____________
|

| one location only

____________ can store only one value


need name , data type , width
Composite:

__________________________________ More than one location


|

| can store number of values = no of locations

__________________________________ each location need name , data


type , width 1

Scalar Variable declaration:-

variable_name [constant] data_type [not null] [:= value] ;


Example
set serveroutput on ;
declare
v_empno
v_name
v_sal

number(4)
varchar2(50)

;
:= 'SCOTT' ;

number(6) not null := 200

v_pi constant number(5,2)

:= 3.14

begin
v_empno := 7788 ;
dbms_output.put_line(v_empno);
dbms_output.put_line(v_name);
dbms_output.put_line(v_sal);
dbms_output.put_line(v_pi);
end;

2)session variable declaration :syntax : variable variable_name data_type;

;
;

example : variable g_empno

number

Example
variable g_empno number
____________________________________________
begin
:g_empno := 200 ;
end;
_____________________________________________
print :g_empno
_______________________________________________

3)substitution variable :select *


from emp
where deptno = 20 ;

select *
from emp
where deptno = &p ;

select &p_col1 , &p_col2 , &p_col3


from emp
where deptno = &p ;

4) %Type Attribute :variable_name


v_empno

table_name.column_name%type ;
emp.empno%type ;

declare
v_empno
v_name
v_sal

emp.empno%type
emp.ename%type

;
:= 'SCOTT' ;

emp.sal%type not null := 200

v_pi constant number(5,2)

:= 3.14 ;

begin
v_empno := 7788 ;
dbms_output.put_line(v_empno);
dbms_output.put_line(v_name);
dbms_output.put_line(v_sal);
dbms_output.put_line(v_pi);
end;

///////////////////////////////////////////END////////////////////////////////////////////////////////////
///////////////////////////////////////////////////

Chapter 02
writting Executable statement

writting select statement :-

syntax
select select_list
into variable_list
from table_name
[where condition ]

Example:
declare
v_name emp.ename%type ;
v_sal

emp.sal%type;

begin
select ename , sal
into v_name , v_sal
from emp
where empno= 7788 ;
dbms_output.put_line( v_name );
dbms_output.put_line( v_sal);

end;
_________________________________________________________________________
_____________________________________________________________

writting oracle Functions


1)single row functions :Example
Declare
v_char
v_number

char(5) ;
number(4,1);

v_date

date ;

begin
v_char

:= substr('Alexandria' ,1,3);

v_number := round (98.23,1);


v_date

:= sysdate ;

dbms_output.put_line(v_char);
dbms_output.put_line(v_number);
dbms_output.put_line(v_date);
end;
_________________________________________________________________________
______________________________________________________

2) multiple row functions :declare


v_sum number(9) ;
v_max number(6);
v_min number(6);
begin
select sum(sal),max(sal) ,min(sal)
into

v_sum , v_max ,v_min

from emp;
dbms_output.put_line(v_sum);
dbms_output.put_line(v_max);
dbms_output.put_line(v_min);
end;
_________________________________________________________________________
______________________________________________________________

writting DMLS (insert ,update , delete ,merge) :begin


insert into dept(deptno,dname ,loc )
values (50,'HR','alex');
update emp
set sal=sal*1.2
where deptno=20 ;
delete from dept
where deptno=40;
end;

using case function:declare


v_grade

varchar2(10) :=&p_g ;

v_result varchar2(25) ;
begin
v_result := case
when v_grade <50

then 'FAIL'

when v_grade between 50 and 64

then 'PASS'

when v_grade between 65 and 74

then 'GOOD'

when v_grade between 75 and 84

then 'V.GOOD'

when v_grade between 85 and 100 then 'Excellent'


else 'In valid grade'
end ;
dbms_output.put_line(v_result);
end;

/////////////////////////////////////////////END//////////////////////////////////////////////////////////
///////////////////////////////////////////////

Chapter 03
Control structures and loops
1) if statement :if condition then
_______________________
_______________________
_______________________
end if;
------------------------------------------------------------------------if condition then
__________________
__________________
__________________

else
__________________
__________________
__________________
end if;
--------------------------------------------------------------------------if Condition1 then
__________________
__________________
__________________
elsif condition2 then
__________________
__________________
__________________
else
__________________
__________________
__________________
end if;
---------------------------------------------------------------------------Example 1 :declare
v_sal number (9) ;
begin
select sal
into v_sal
from emp
where empno = 7788 ;
if v_sal between 3000 and 4000 then
dbms_output.put_line('salary is between 3000 and 4000') ;
update emp set sal = sal * 1.2
where empno =7788;
end if;
end;
________________________________________________________________
___________________________________________
Example 2 :declare

v_sal number (9) ;


begin
select sal into v_sal from emp where empno = 7788 ;
if v_sal between 2000 and 3000 then
dbms_output.put_line('salary is between 2000 and 3000') ;
update emp set sal = sal * 1.2 where empno =7788;
else
dbms_output.put_line(' salary out of range 2000 and 3000');
end if;
end;
________________________________________________________________
___________________________________________

Example 3 :declare
v_sal number (9) ;
begin
select sal into v_sal from emp where empno = 7788 ;
if v_sal between 2000 and 3000 then
dbms_output.put_line('salary is between 2000 and 3000') ;
update emp set sal = sal * 1.2 where empno =7788;
elsif v_sal between 3001 and 4000 then
dbms_output.put_line('salary is between 3001 and 4000') ;
update emp set sal = sal * 1.3 where empno =7788;

else
dbms_output.put_line(' salary out of range 3001and 4000');
end if;
end;
________________________________________________________________
_______________________________________________
2) Loops :1) Basic Loop
2) While loop
3) For Loop
____________________________________________________________
2.1) Basic Loop :syntax
loop
____________
____________
____________
exit when condition ;
end loop;
Example :Declare
v_counter
begin
loop

number(3) := 0;

v_counter := v_counter +1 ;
dbms_output.put_line(v_counter);
exit when v_counter=10;
end loop;
end;
2.2) While loop :while ( condition ) loop
----------------------------------------------------------------------------end loop;
Example :Declare
v_counter

number(3) := 0;

begin
while ( v_counter < 10) loop
v_counter := v_counter + 1 ;
dbms_output.put_line(v_counter);
end loop;
end;
________________________________________________________________
______________________________________________

2.3) for loop :syntax


for variable_name in lower_limit .. upper_limit loop
---------------------------------------------------------------------end loop;

Example
begin
for v_counter in 1..10 loop
dbms_output.put_line( v_counter );
end loop;
end;
begin
for v_counter in reverse 1..10 loop
dbms_output.put_line( v_counter );
end loop;
end;
set serveroutput on
/////////////////////////////////END////////////////////////////////////////////////////////
/////////////////////////////////////////

chapter 04
Working with cursors
what is cursor ?
cursor steps
declare : cursor cursor_name is select statement .
open

: open cursor_name.

fetch

: fetch cursor_name into variable_name .

test
: cursor_namefound , cursor_name%notfound ,
cursor_name%rowcount
close

: close cursor_name;

____________
|empno | sal |
____________
|7788 | 200 |
Cursor_name%found
cursor_name%notfound
|7756 | 300 |
|7569 | 400 |

|7782 | 200 |
Cursor_name%Open
cursor_name%rowcount
|7756 | 300 |
|7920 | 300 |
|9876 | 600 |

emp_rec
-------empno

sal

____________
|7788 | 200 |
____________
Example
declare
cursor emp_cur is
select empno , sal
from emp ;
emp_rec emp_cur%rowtype;
begin
open emp_cur ;
loop
fetch emp_cur into emp_rec ;
if emp_rec.sal > 2000 then
update emp
set sal = sal * 1.1
where empno = emp_rec.empno ;
dbms_output.put_line ( emp_rec.empno);
end if;
exit when emp_cur%notfound ;
end loop;

close emp_cur ;
end;

cursor for loop:declare


cursor emp_cur is
select empno , sal
from emp ;
begin
for emp_rec in emp_cur loop
if emp_rec.sal > 2000 then
update emp
set sal = sal * 1.1
where empno = emp_rec.empno ;
dbms_output.put_line ( emp_rec.empno);
end if ;
end loop;
end;

cursor with subquery :begin


for emp_rec in (select empno , sal from emp ) loop
if emp_rec.sal > 2000 then
update emp
set sal = sal * 1.1
where empno = emp_rec.empno ;
dbms_output.put_line ( emp_rec.empno);
end if ;
end loop;
end;

cursor with parameter :declare


cursor emp_cur ( p_deptno number) is
select empno , sal
from emp
where deptno = p_deptno ;
begin
for emp_rec in emp_cur ( 10 ) loop
if emp_rec.sal > 2000 then
update emp
set sal = sal * 1.1
where empno = emp_rec.empno ;
dbms_output.put_line ( emp_rec.empno);
end if ;
end loop;
dbms_output.put_line ( ' i have finished loop 1
' );
for emp_rec in emp_cur (20) loop
if emp_rec.sal > 1000 then
update emp
set sal = sal * 1.2
where empno = emp_rec.empno ;
dbms_output.put_line ( emp_rec.empno);
end if ;
end loop;
end;

for update , where current :declare


cursor emp_cur is
select empno , sal
from emp
for update ;
begin
for emp_rec in emp_cur loop

if emp_rec.sal > 2000 then


update emp
set sal = sal * 1.1
where current of emp_cur;
dbms_output.put_line ( emp_rec.empno);
end if ;
end loop;
end ;
/////////////////////////////////END////////////////////////////////////////////////////////
//////////////////////////////////////

Chapter 05
Working with composite data types
Chapter content
1) Record Variables
2) Table Variables
3) Table of records
Introduction
what is variable? ( memory location )
Variable types
1) Scalar : store only one value has( name , data type , width )
variable_name
;
v_empno
_______
|
|
_______

[ constant ] data_type [ not null ] [ := value ]


number(4)

not null

:= 7788 ;

2) composite :
2.1 Record variable
consists of group of locations each location has name , data
type , width
acts like record in a table
1

for each location ( name , data type , width )


no of values that can be store d here = no of locations
___________________________________________
2.2 table variable
consists of unlimited no of locations all has one data type and
one width
but can store unlimited number of values
1
2
3
4
______________________________________________
1) Record variables :
1.1) designed Record variable
Syntax
type type_name is record
( column_name data_type ,
column_name data_type ,
.....);

to reference record variables you can use


variable_name.location_name ;
Example
Declare
type emp_rec_type is record
(eno number(4) ,
name varchar2(50),
job varchar2(50));
emp_rec emp_rec_type ;
_____________________________________
emp_rec :
_________
eno

name

job

begin
emp_rec.eno := 2 ;
emp_rec.name := 'A' ;
emp_rec.job := 'Analyst' ;
dbms_output.put_line(emp_rec.eno);
dbms_output.put_line(emp_rec.name);
dbms_output.put_line(emp_rec.job);
end;
..............................................................................

1.2) %rowtype record variable


Syntax
variable_name

table_name%rowtype ;

Example
Declare
emp_rec

emp%rowtype ;

emp_rec :
________
empno ename job

mgr

hiredate

sal

comm deptno

begin
select *
into emp_rec
from emp
where empno = 7788 ;
if emp_rec.sal > 2000 then
update emp set sal= sal * 1.2 where empno= emp_rec.empno
;
dbms_output.put_line ( ' empno no : ' || emp_rec.empno || '
has been updated' ) ;
end if;
dbms_output.put_line ( emp_rec.job ) ;
end;

Declare
emp_rec
emp%rowtype ;
begin
select *
into emp_rec
from emp
where empno = &o ;
if emp_rec.sal > 2000 then
update emp set sal= sal * 1.2 where empno= emp_rec.empno
;
dbms_output.put_line ( ' empno no : ' || emp_rec.empno || '
has been updated' ) ;
end if;
dbms_output.put_line ( emp_rec.job ) ;
end;
____________________________________________
2) table variables :
syntax
;

type type_name is table of data_type index by binary_integer

variable_name
.............................
1
2
3
4

index

Example
Declare
type name_table_type is table of varchar2(50) index by
binary_integer ;
name_table
name_table

name_table_type ;
index

scot 1
t
Alen 2
war
d

200

....

begin
name_table (1) := 'Scott' ;
name_table (2) := 'Alen' ;
name_table (200) := 'ward' ;
dbms_output.put_line ( name_table (1) );
dbms_output.put_line ( name_table (2) );
dbms_output.put_line ( name_table (200) );
end;
________________________________________________________________
___________________________________________________

3) table of records
Declare
type emp_table_type is table of emp%rowtype index by
binary_integer ;
emp_table

emp_table_type ;

index empno ename job


comm deptno
1
2

mgr

hiredate

sal

scot
t
Analy
st

.....

______
begin
emp_table(1).ename := 'Scott' ;
emp_table(2).job := 'Analyst' ;
dbms_output.put_line ( emp_table(1).ename);
dbms_output.put_line ( emp_table(2).job );
end;

/////////////////////////////////END////////////////////////////////////////////////////////
//////////////////////////////////////

Chapter 06
Handling Exceptions
what is exception ?
Exception types:
1) oracle predefined
( has code , name , text )
2) oracle non-predefined ( has code , text )
3) user defined ( no code , no name , no text )
handling exceptions
[Declare]
begin
---------------------s1
---------------------s2
---------------------s3
---------------------s4
[Exception]
when exception_name then
---------------------------s1
---------------------------s2
---------------------------s3
when exception_name then
---------------------------s1
---------------------------s2
---------------------------s3
when others then
---------------------------s1
---------------------------s2
---------------------------s3
end ;

1) oracle predefined exception


Example
declare
v_sal number(4) ;
begin
select sal into v_sal from emp
where empno = 77 ;
dbms_output.put_line ( ' empno no 77 has salary = ' || v_sal
);
exception
when no_data_found then
dbms_output.put_line ( 'Invalid Employee Number');
end;
_____________________________________________
2) Oracle non-predefined
Declare
child_found
exception ;
pragma exception_init ( child_found , -02292) ;
begin
delete from dept where deptno = 10;
dbms_output.put_line (' deptno 10 has been deleted ');
exception
when child_found then
dbms_output.put_line ( ' this department has employees you
canot delete it ');
end;
________________________________________________________________
___________________________________________________

3) user defined exception


declare
over_flow exception ;
begin
for i in 1..5 loop
if i = 3 then
raise over_flow ;
else
dbms_output.put_line ( i ) ;
end if;
end loop;
exception
when over_flow then
dbms_output.put_line ( ' i reaches 3 ' ) ;
end;
________________________________________________________________
__________________________________________________

4) Propagating Exceptions
Declare

v_sal
number(6) ;
begin
begin
select sal into v_sal from emp where empno = 7 ;
exception
when no_data_found then
dbms_output.put_line('invalid employee
number');
end;
select sal into v_sal from emp where deptno = 10 ;
insert into dept
values (50 ,'dept50' , 'Alex');
update emp set sal = sal * 1.2 where empno = 7788 ;
Exception
when too_many_rows then
dbms_output.put_line ( ' adjust your search condition ') ;
end;
________________________________________________________________
_______________________________________________________

5) raise application Error


Example
begin

if to_char ( sysdate , 'hh24:mi' ) not between '09:00' and


'18:00' then
raise_application_error (-20201 , 'you canot update emp table
out of business hours');
else
update emp set sal = sal * 1.2 where empno = 7566 ;
end if;
end;
________________________________________________________________
_______________________________________________________

/////////////////////////////////END////////////////////////////////////////////////////////
//////////////////////////////////////

Chapter 07
creating procedures and functions

what is procedure ? what is function ?


PLSQL Block
Declare
Begin
Exception
End ;
******************************************************
plsql block is stored as function if it is used to calculate a value .
plsql block is stored as procedures if it is used to perform an
action .
Creating Procedures
syntax
create [or replace] procedure procedure_name
(prameter_1_name datatype ,
parameter_2_name datatype , ..... )
is
begin
exception
end;
*****************************************************

Example
create or replace procedure update_sal
( p_empno number ,
p_value number )
is
begin

update emp
set sal = sal + p_value
where empno = p_empno ;
end ;
calling procedure
a) execute update_sal ( 7788 , 200) ;
....................................
b)from plsql block
-------------begin
delete from emp where empno = 7788 ;
update_sal (7566 , 500 );
insert into dept values ( 60 , 'ss' , 'cairo');
end;
**********************************************
creating functions :
syntax
create [or replace] function function_name
( parameter_1_name data type ,
parameter_2_name data type , ..... )
return return_data_type
is
begin
return value ;
[exception]
end;
***********************************************

Example
create or replace function calc_tax
( p_empno
number ,
p_tax
number)
return number
is
v_t number (6,2) ;
begin

select sal * p_tax / 100


into v_t
from emp
where empno = p_empno ;
return v_t ;
end;
-----------------------------------------------------------------------------Function call
a) From plsal block
declare
x number(6,2) ;
begin
x := calc_tax ( 7566 , 10 );
dbms_output.put_line( x ) ;
end;
--------------------------------------------------------------------------------b) from select statement
select empno , ename , sal, calc_tax ( empno , 10 )
from emp;
c) using execute command
variable gt number
execute :gt := calc_tax ( 7566 , 10 );
print :gt
*************************************************

Parameter Modes:
- in
- Out
- in out
b) out parameters
Example

Create or replace procedure get_emp_data


( p_empno
p_ename
p_job

in

number ,

out varchar2,

out varchar2 )

is
begin
select ename , job
into p_ename , p_job
from emp
where empno = p_empno ;
end;
-----------declare
v_n
v_j

varchar2(15) ;
varchar2(15) ;

begin
get_emp_data (7566 , v_n , v_j ) ;
dbms_output.put_line (v_n) ;
dbms_output.put_line (v_j) ;
end ;
*****************************************

c) in out parameter
create or replace procedure format_phone
(p_phone in out varchar2 )
is
begin
p_phone := '(' || substr ( p_phone , 1, 2 ) ||
')' || substr ( p_phone ,3,3)
'-' || substr ( p_phone ,6) ;

||

end ;
***************************************
declare
tt

varchar2(15) := '035570515' ;

begin
format_phone (tt) ;
dbms_output.put_line(tt);
end;
*****************************************
parameter calling methods ( Method of passing parameters )
Example
create or replace procedure add_dept
( p_deptno dept.deptno%type ,
p_dname
p_loc

dept.dname%type default 'Unknown' ,

dept.loc%type

default 'Unknown' )

is
begin
insert into dept (deptno , dname , loc )
values (p_deptno , p_dname , p_loc ) ;
end;
****************************************
Calling
begin
add_dept(70) ;
add_dept(80 , 'HR') ;
add_dept(90 , 'HR' , 'Alex' );
add_dept(95 ,p_loc =>'Alex' );
end;
*****************************************
desc procedure_name

desc update_sal
drop procedure procedure_name ;
drop procedure update_Emp ;
drop function calc_tax ;
*****************************************
to know what are the procedures and functions in your current
user
select object_name , object_type
from user_objects
where object_type in ( 'PROCEDURE' , 'FUNCTION');

/////////////////////////////////END////////////////////////////////////////////////////////
//////////////////////////////////////

Chapter 08
Creating and Managing Packages
PartI
what is package?
package components
package specification
package body

package component types


public
private
1) Syntax for Creating Packages
1.1) Package Specification
Create [or replace] package package_name
is
__________________ ( constructs declarations ,variables , types ,
cursors, ...)
__________________
__________________
End ;
1.2) package body
Create [or replace] package body package_name
is
__________________ ( real implementation of program units )
__________________
__________________
end ;
_______________________________________________________________
________________________________________________________

Example:
Create or replace package emp_pk
is
procedure update_comm (p_empno emp.empno%type ,
p_comm emp.comm%type ) ;
function calc_tax (p_empno emp.empno%type , p_tax
number ) return number ;
End ;
/
Create or replace package body emp_pk
is
-----------------------------------------------------------------------------

function validate_comm ( p_comm emp.comm%type) return


boolean
is
v_maxsal number(6) ;
begin
select max(sal) into v_maxsal from emp ;
if p_comm >= v_maxsal then
return false ;
else
return true ;
end if;
end validate_comm ;
----------------------------------------------------------------------------Procedure update_comm( p_empno emp.empno%type ,
p_comm emp.comm%type )
is
begin
if validate_comm ( p_comm ) then
update emp set comm =p_comm where empno =
p_empno ;
else
dbms_output.put_line ( 'invalid comm ');
end if;
end update_comm ;
-----------------------------------------------------------------------------function calc_tax (p_empno emp.empno%type , p_tax
number ) return number
is
v_t number ( 9,2) ;
begin
select ( sal * p_tax / 100 ) into v_t from emp where
empno = p_empno ;
return v_t ;
end calc_tax ;
-------------------------------------------------------------------------------End emp_pk ;

________________________________________________________________
______________________________________________________
2)Calling Package Constructs
2.1) using execute command
package_name.subprogram_name ;
set serveroutput on
execute emp_pk.update_comm ( 7900 , 6000 );
2.2) from PLSQL Block
begin
emp_pk.update_comm ( 7788 , 6000 );
end ;
2.3) calling package functions from select statement
select empno, ename, sal, emp_pk.calc_tax ( empno , 10 )
from emp ;
3)Bodiless Packages
Example
Create or replace package global_var
is
v_mty constant number(9,2) := 1.06 ;
v_pi constant number(4,2) := 3.14 ;
end;
calling bodiless packages
begin
dbms_output.put_line ( 2 * global_var.v_pi * 10 );
end;
///////////////////////////////PART II////////////////////////////////
Package overloading
create or replace package add_dept_pk
is
procedure add_dept (p_deptno number);
procedure add_dept (p_deptno number,p_dname varchar2 );

procedure add_dept (p_deptno number , p_dname varchar2 ,


p_loc varchar2 );
end;
/
create or replace package body add_dept_pk
is
--------------------------------------------------------------------procedure add_dept (p_deptno number )
is
begin
insert into dept ( deptno , dname , loc )
values (p_deptno , 'unknown' , 'unknown' );
end;
-------------------------------------------------------------------procedure add_dept (p_deptno number , p_dname varchar2 )
is
begin
insert into dept ( deptno , dname )
values (p_deptno , p_dname );
end;
-----------------------------------------------------------------------

procedure add_dept (p_deptno number , p_dname varchar2 ,


p_loc varchar2 )
is
begin
insert into dept ( deptno , dname , loc )
values (p_deptno , p_dname , p_loc );
end;
end add_dept_pk ;
/
show error
________________________________________________________________
__________________________________________________

create or replace package add_dept_pk


is
procedure add_dept (p_deptno number ) ;
procedure add_dept (p_deptno number , p_dname varchar2 );
procedure add_dept (p_deptno number , p_dname number);
end;
/
create or replace package body add_dept_pk
is
--------------------------------------------------------------------procedure add_dept (p_deptno number )
is
begin
insert into dept ( deptno , dname )
values (p_deptno , 'unknown');
end;
-------------------------------------------------------------------procedure add_dept (p_deptno number , p_dname varchar2 )
is
begin
insert into dept ( deptno , dname )
values (p_deptno , p_dname );
end;
----------------------------------------------------------------------procedure add_dept (p_deptno number , p_dname number )
is
begin
insert into dept ( deptno )
values (p_deptno);
dbms_output.put_line(p_dname);
end;
end add_dept_pk ;
/
show error
execute add_dept_pk.add_dept(51) ;
execute add_dept_pk.add_dept(52,'HR2') ;
execute add_dept_pk.add_dept(53,'Acc' , 'Alex') ;

select * from dept;


________________________________________________________________
__________________________________________________
forward declarations
Create or replace package emp_pk
is
procedure update_comm (p_empno emp.empno%type ,
p_comm emp.comm%type ) ;
function calc_tax (p_empno emp.empno%type , p_tax
number ) return number ;
End ;
/
Create or replace package body emp_pk
is
----------------------------------------------------------------------------------

Procedure update_comm ( p_empno emp.empno%type ,


p_comm emp.comm%type )
is
begin
if validate_comm ( p_comm ) then
update emp set comm =p_comm where empno =
p_empno ;
else
dbms_output.put_line ( 'invalid comm ');
end if;
end update_comm ;
-----------------------------------------------------------------------------function validate_comm ( p_comm emp.comm%type) return
boolean
is
v_maxsal number(6) ;
begin
select max(sal) into v_maxsal from emp ;

if p_comm >= v_maxsal then


return false ;
else
return true ;
end if;
end validate_comm ;
----------------------------------------------------------------------------function calc_tax (p_empno emp.empno%type , p_tax
number ) return number
is
v_t number ( 9,2) ;
begin
select ( sal * p_tax / 100 ) into v_t from emp where
empno = p_empno ;
return v_t ;
end calc_tax ;
End emp_pk ;
--------------------------------------------------------------------------------

one time only :


create or replace package emp_pk
is
v_maxsal number (9) ;
procedure update_comm ( p_empno number , p_comm
number );
end;
/
create or replace package body emp_pk
is
---------------------------------------------------------------------procedure update_comm ( p_empno number , p_comm
number )
is
begin
if p_comm <=v_maxsal then

update emp set comm = p_comm where empno =


p_empno ;
else
dbms_output.put_line ( 'invalid comm');
end if;
end update_comm ;
---------------------------------------------------------------------begin
select max(sal) into v_maxsal from emp;
end emp_pk ;
/
show error
execute emp_pk.update_comm ( 7900 , 6000 );
________________________________________________________________
______________________________
4)removing packages
drop package package_name ;
drop package body package_name ;
drop package emp_pk ;
/////////////////////////////////END////////////////////////////////////////////////////////
////////////////////////////////////////////