Stored Procedure and Functions
What are Strored Procedures?
• Stored Procedure are named PL/SQL blocks.
• Stored Procedure is a logical group of SQL and
PL/SQL statements that perform some specific
action
• Stored in database as database objects.
• Are compiled ONLY at the time of their creation
or modification
Advantages of Stored Procedures
• Improve database performance
– Reducing information sent over network
• Memory Allocation
– Only single copy of stored procedure is loaded
into memory for execution by multiple users.
• Productivity
– Redundant code can be avoided
Advantages of Stored Procedures
• Integrity
– Common set of procedures reduces committing
coding errors
• Memory Allocation
– Only single copy of stored procedure is loaded into
memory for execution by multiple users.
• Productivity
– Redundant code can be avoided
Anonymous Pl/SQL vs Stored
Procedures
• Anonymous PL/SQL is unnamed PL/SQL block
whereas Stored Procedure is named block.
• Anonymous PL/SQL subsequent reuse is not
possible.
• Anonymous PL/SQL compiled each time
submitted to Oracle whereas Stored
procedure is stored as database object.s
How to create Stored Procedures
CREATE [OR REPLACE] PROCEDURE procname
(parameter1 MODE datatype [,parameter2 MODE datatype...])
AS
[declare]
....
BEGIN
statement(s);
END [procname];
MODES of Parameters
• IN Mode
– Default
– Pass values to the procedure being called
– IN parameters acts like constant and cannot
assigned value.
MODES of Parameters
• OUT Mode
– Returns values to caller
– OUT parameters is uninitialized and cannot be
assigned to another variable.
• IN OUT Mode
– Pass initial value to procedure and return updated
values
– Can be initialised or assigned to other
STORED FUNCTIONS
What are Stored Functions
• Store Function is same as Stored Procedure except that
– FUNCTION return value Procedure not
CREATE OR REPLACE FUNCTION functionname
(parameter MODE datatype....)
RETURN returntype
AS
declare
BEGIN
statements;
END functionname;
Obtaining Procedure Created by You
• To see names of procedure
SELECT * FROM user_objects
WHERE object_type=‘PROCEDURE’
• To view source code of procedure
SELECT Text FROM user_source
WHERE name=‘procedurename’;
DROPPING
DROP PROCEDURE procedurename;
DROP FUNCTION functionname;
PACKAGES
What are Packages
• Packages are the schema objects that groups logically
related PL/SQL objects.
• Compiled and stored in database.
• What goes in PL/SQL?
– Types
– Variables and Constants
– Cursors
– Procedure and Function
– Exception declaration
Advantages of Packages
• Modularity
– Encapsulate logically related types
• Information Hiding by declaring it as public or
private
Parts of Package
• Specification
– Declare types, constants, cursors, exception etc.
– List package resources available to resources
– Its a public declaration visible to apps
– Compulsory
• Body
– Implements or define cursors or subprograms
– Private declaration hidden from apps
– Optional
CREATE OR REPLACE PACKAGE pkgname
IS
-- all declaration statement
END pkgnmae
CREATE OR REPLACE PACKAGE BODY pkgname
IS
-- all definition

5. stored procedure and functions

  • 1.
  • 2.
    What are StroredProcedures? • Stored Procedure are named PL/SQL blocks. • Stored Procedure is a logical group of SQL and PL/SQL statements that perform some specific action • Stored in database as database objects. • Are compiled ONLY at the time of their creation or modification
  • 3.
    Advantages of StoredProcedures • Improve database performance – Reducing information sent over network • Memory Allocation – Only single copy of stored procedure is loaded into memory for execution by multiple users. • Productivity – Redundant code can be avoided
  • 4.
    Advantages of StoredProcedures • Integrity – Common set of procedures reduces committing coding errors • Memory Allocation – Only single copy of stored procedure is loaded into memory for execution by multiple users. • Productivity – Redundant code can be avoided
  • 5.
    Anonymous Pl/SQL vsStored Procedures • Anonymous PL/SQL is unnamed PL/SQL block whereas Stored Procedure is named block. • Anonymous PL/SQL subsequent reuse is not possible. • Anonymous PL/SQL compiled each time submitted to Oracle whereas Stored procedure is stored as database object.s
  • 6.
    How to createStored Procedures CREATE [OR REPLACE] PROCEDURE procname (parameter1 MODE datatype [,parameter2 MODE datatype...]) AS [declare] .... BEGIN statement(s); END [procname];
  • 7.
    MODES of Parameters •IN Mode – Default – Pass values to the procedure being called – IN parameters acts like constant and cannot assigned value.
  • 8.
    MODES of Parameters •OUT Mode – Returns values to caller – OUT parameters is uninitialized and cannot be assigned to another variable. • IN OUT Mode – Pass initial value to procedure and return updated values – Can be initialised or assigned to other
  • 9.
  • 10.
    What are StoredFunctions • Store Function is same as Stored Procedure except that – FUNCTION return value Procedure not CREATE OR REPLACE FUNCTION functionname (parameter MODE datatype....) RETURN returntype AS declare BEGIN statements; END functionname;
  • 11.
    Obtaining Procedure Createdby You • To see names of procedure SELECT * FROM user_objects WHERE object_type=‘PROCEDURE’ • To view source code of procedure SELECT Text FROM user_source WHERE name=‘procedurename’;
  • 12.
  • 13.
  • 14.
    What are Packages •Packages are the schema objects that groups logically related PL/SQL objects. • Compiled and stored in database. • What goes in PL/SQL? – Types – Variables and Constants – Cursors – Procedure and Function – Exception declaration
  • 15.
    Advantages of Packages •Modularity – Encapsulate logically related types • Information Hiding by declaring it as public or private
  • 16.
    Parts of Package •Specification – Declare types, constants, cursors, exception etc. – List package resources available to resources – Its a public declaration visible to apps – Compulsory • Body – Implements or define cursors or subprograms – Private declaration hidden from apps – Optional
  • 17.
    CREATE OR REPLACEPACKAGE pkgname IS -- all declaration statement END pkgnmae CREATE OR REPLACE PACKAGE BODY pkgname IS -- all definition