PACKAGES IN ORACLE

PACKAGE:
                        A package is named collection of variables,cursors,types,procedures and functions.

  • package does not accepts parameters.
  • can not be invoked.
  • can not be nested.
package having two parts

                          package specification
                          package body

package specification: we are defining global data and also declare subprograms

package body: here we are implementing subprograms

syntax: package specification

create or replace package ( package_name )
is
global_variable declaration;
cursor declaration;
procedure declaration;
function declaration;
types declaration;
end (package_name);
/

package body:

create or replace package body (package_name)
is
procedure implementation;
function implementation;
end (package_name);
/
----------------------------------------------------------------------

invoking packaged subprograms:

exec package_name.procedure_name(actual parameters);

select package_name.function_name(actual parameters) from dual;


examples:
-------------------------------------------------------------------------------------
------package specification

create or replace package pkg1
is
   procedure p1;
   procedure p2;
end pkg1;


---- package body

create or replace package body pkg1
is
procedure p1
is
    dbms_output.put_line('procedure p1');
end p1;
procedure p2
is
  dbms_output.put_line('procedure p2');
end p2;
end pkg1;
/

exec pkg1.p1;
output is: procedure p1


exec pkg1.p2;
output is: procedure p2

--------------------------------------------------------------------

global variable declaration :

  in plsql we are defining global variables in  package specification only

example:

create or replace package pkg2
is
--global variable declaration
   g number(3):=500;
   procedure p3;
   function f1(a number) return number;
end pkg2;
/

--package body
create or replace package body pkg2
is
   procedure p3
   is
        z number(3);
   begin
        z :=g/2;
       dbms_output.put_line(z);
   end p3;
   function f1 (a number)
   return number
   is
       n number(5):=0;
  begin
       n:=g*a;
       return n;
   end f1;
end pkg2;
/

----exec procedure p3
exec pkg2.p3;
output:250

---exec function f1 is
select pkg2.f1(4) from dual;


output:2000.
----------------------------------------------------------------------------------




Comments

Popular posts from this blog

PRAGMA SERIALLY_REUSABLE

COLLECTION METHODS

CURSOR PARAMETERS