Stored procedures and functions are named PL/SQL blocks that are stored in a database. They improve performance by reducing network traffic and allowing shared memory usage. Stored procedures are created using the CREATE PROCEDURE statement and can accept parameters using modes like IN, OUT, and IN OUT. Stored functions are similar but return a value. Packages group related database objects like procedures, functions, types and provide modularity and information hiding.
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
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’;
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