You are on page 1of 2

Packages : Usage : It is used for grouping all the related functions and procedures togethe r... Adv.

: * Provides easy maintainance.. * Code will remain secured.. steps to work with Packages : step 1 : create the package specification where the package spec. is used for specifying the prototypes of all the functio ns and procedures that will be used in the Package.. syntax : create package <package_name> is --global variable declarations and intializations.. function function_name( [arg_info] ) return <datatype>;..... procedure prototype(s); end; step 2 : create the package body... Usage : It is used for specifying the definitions for all the functions and proc edures used in the pack. spec. create package calculator is function addnum ( a number, b number) return number; function subnum ( a number,b number ) return number; procedure display ( a number, b number, res number ); end; create or replace package body calculator is function addnum ( a number, b number ) return number is begin return a+b; end addnum; function subnum ( a number, b number ) return number is begin return a-b; end subnum; procedure display ( a number, b number, res number ) is

begin dbms_output.put_line( 'Result of ' || a || ' and ' || b || ' = ' || res ); end display; end; To Execute the functions or procedures present in the package : package_name.member_name select calculator.addnum( 10,20) as "Addition" from dual; select empno,ename,sal,comm, calculator.addnum( sal, comm ) as "Total Salary" from emp where job='SALESMAN' Triggers : It is a set of plsql instructions which will be executed implictly ba sed on some events.. Usage : * To provide additional restrictions what the Oracle constraints cant achieve.. * To automate the tasks.. Note : Triggers should be written to perform any task which is already provided by oracle.. * It is advisable in triggers to omit dbms_output statements Restrictions : * Commit statement should not be used inside a trigger * Trigger's should not refer to any of the columns which it cant see incase if i t refers then it will result in an error called mutating trigger error.. select to_char( sysdate, 'hh24:mi:ss' ) from dual create or replace trigger restrict_time_trg before insert on emp begin if to_char(sysdate,'hh24') < 9 or to_char( sysdate,'hh24' ) > 14 then raise_application_error( -20001,'Insertions are not allowed in this timings..' ) ; end if; end;

You might also like