Stored Procedures: Implementation of Business Rules Within Database Why? Improves Security, Efficiency, Integrity
Stored Procedures: Implementation of Business Rules Within Database Why? Improves Security, Efficiency, Integrity
Why?
Improves security, efficiency, integrity
PL/SQL
Oracle programming language that allows
SQL statements.
[non –standard language]
Can combine data manipulation and query
processing within a block of procedural
code.
Oracle Stored procedures
These are PL/SQL blocks stored in the
database
they are database objects, similar to tables
can be accessed by client applications and
session users.
[Microsoft SQL server has its own language
as do other DB vendors]
Benefits of stored procedures
Performance
All code remains in the database
only parameters are passed in calls
Security
– stored procedures are written for all update
methods to tables ensuring appropriate access
to and management of data changes.
– Allows users with low access permissions to
use stored procedures from library
Types of stored procedures
Triggers
linked to specific changes occurring in database
they will be executed as a result of database values
functions [as in other languages allow a value
to be obtained and returned]
** must return a value when called
procedures can send or return values
– can execute a variety of statements
PL/SQL Block Structure
Create or replace procedure procedure_name
or procedure_name(parameter list)
IS
DECLARE [only required in a nested block]
declarations: variables, cursors, user-defined
exceptions
BEGIN mandatory
executable SQL & PL/SQL statements
EXCEPTION optional
exception handling
Is
v_dummytext varchar2(25) ;
Begin
End:
/
Format of a trigger
create or replace trigger BTrigCapEmp
before insert or update of JOB,ENAME
on EMP
for each row
Declare
begin
:new.JOB := upper(:new.JOB);
:new.ENAME := upper(:new.ENAME);
-- changes values to upppercase
end;
/
Format of function
Create or replace function fname[(parameters)]
Return datatype
Is
Declare variables / datatypes
Begin
….executable statements
Return returnValue;
Exception
End;
/