Stored Procedures
Stored Procedures
Stored Procedures
S.Naji
[email protected]
Stored procedures
A stored procedure is a named set of PL/SQL statements
designed to perform an action. Stored procedures are
stored inside the database. They define a programming
interface for the database rather than allowing the client
application to interact with database objects directly.
Stored procedures are typically used for data validation
or to encapsulate large, complex processing instructions
that combine several SQL queries.
Stored functions have a single return value parameter.
Unlike functions, procedures may or may not return
values.
Procedure format
CREATE [OR REPLACE] PROCEDURE
procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
Simple Procedure
CREATE PROCEDURE ins
IS
begin
insert into users values(1,'a');
end;
/
Calling the procedure (pl/sql)
begin
ins;
Ins;
end;
/
Example 2
create table Employee(
ID VARCHAR2(4 BYTE) NOT NULL,
First_Name VARCHAR2(10 BYTE),
Last_Name VARCHAR2(10 BYTE),
Start_Date DATE,
End_Date DATE,
Salary Number(8,2),
City VARCHAR2(10 BYTE),
Description VARCHAR2(15 BYTE)
)
/
Inserting Data
insert into Employee(ID, First_Name, Last_Name,
Start_Date, End_Date, Salary, City, Description)
values ('01','Jason',
'Martin',to_date('19960725','YYYYMMDD'),
to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto',
'Programmer')
/
CALL update_employee_salary(1.5);
DBMS_output.put_line
set SERVEROUTPUT ON
Set echo on
(To use DBMS_output.put_line )