Stored Procedures

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

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')
 /

Select * from employee; (View the table)


Creating sequence
Sequence is used to Enter some Numbers(Sequence
order) to the tables, always it will give the next value for
the sequence. We can determine the minimum and
maximum limit of the sequence.

CREATE SEQUENCE sequence_name


MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;
Example
CREATE SEQUENCE sup
MINVALUE 1
MAXVALUE 99
START WITH 1
INCREMENT BY 1
CACHE 20;
Creating sequence for the above program

create sequence student_sequence;


Dropping a sequence (After the usage)
 drop sequence sequence_name;
Creating Procedure
 CREATE OR REPLACE PROCEDURE AddNewEmployee (
 p_FirstName employee.first_name%TYPE,
 p_LastName employee.last_name%TYPE,
 p_Salary employee.salary%TYPE) AS
 BEGIN
 INSERT INTO employee (ID, first_name, last_name,salary)
 VALUES (student_sequence.nextval, p_FirstName,p_LastName,p_Salary);
 COMMIT;
 END;
 /
 Should be mention before procedure
 Create sequence student_sequence;
 begin
 AddNewEmployee(‘hh’,’jjkk’,34000)
 End;
Calling a procedure
DECLARE
 v_NewFirstName employee.first_name%TYPE := 'Margaret';
 v_NewLastName employee.last_name%TYPE := 'Mason';
 v_NewSalary employee.salary%TYPE := 2000;
 BEGIN
 -- Add Margaret Mason to the database.
 AddNewEmployee(v_NewFirstName, v_NewLastName,
v_NewSalary);
 END;
 /
Example 3
 CREATE OR REPLACE PROCEDURE update_employee_salary(
 p_factor IN NUMBER
 ) AS
 BEGIN
 UPDATE employee
 SET salary = salary * p_factor;
 COMMIT;
 EXCEPTION
 WHEN OTHERS THEN
 ROLLBACK;
 END update_employee_salary;
 /
Calling a procedure (Simple call)

CALL update_employee_salary(1.5);
DBMS_output.put_line
 set SERVEROUTPUT ON
 Set echo on
 (To use DBMS_output.put_line )

 CREATE OR REPLACE PROCEDURE emp_change_s (i_emp_id


IN VARCHAR2) AS
 BEGIN
 UPDATE employee set City = 'New' WHERE id = i_emp_id;

 DBMS_OUTPUT.PUT_LINE ('updated ');
 END emp_change_s;
 /
Calling the Procedure
call emp_change_s('01');
Simple Example DBMS_output.put_line
 create or replace procedure p_hello
 is
 v_string varchar2(256):='Hello, World!';
 begin
 dbms_output.put_line(v_string);
 end;
 /
Calling
begin
p_hello;
 end;
 /
Current Record Access
 CREATE TABLE customer
 (customer_id NUMBER(7),
 customer_name VARCHAR2(50),
 phone VARCHAR2(15),
 address VARCHAR2(400),
 city VARCHAR2(35),
 state VARCHAR2(30),
 country VARCHAR2(30),
 zip_code VARCHAR2(10),
 credit_rating VARCHAR2(9),
 sales_rep_id NUMBER(7),
 region_id NUMBER(7),
 comments VARCHAR2(255),
 preferred_customer VARCHAR2(1) DEFAULT 'N' NOT NULL,
 shipping_method VARCHAR2(1) DEFAULT 'M' NOT NULL);
Insert Rows
INSERT INTO customer VALUES (201, 'Jane', '111-1111',
'7 AVE','SAO', NULL, 'BRAZIL', NULL,
'EXCELLENT',12, 2, 'A', 'N', 'M');
INSERT INTO customer VALUES (207, 'Cat', '777-
7777', '6 STREET','LAGOS', NULL, 'NIGERIA', NULL,
'GOOD', NULL, 3, NULL,'N', 'M');
Procedure with Cursor
 CREATE OR REPLACE PROCEDURE display_customers
 AUTHID CURRENT_USER IS
 CURSOR cur_cust IS
 SELECT customer_id, customer_name
 FROM customer;
 BEGIN
 FOR cur_cust_rec IN cur_cust LOOP
 DBMS_OUTPUT.PUT_LINE('Customer Id: ' ||
cur_cust_rec.customer_id || CHR(9) || ' Customer Name: ' ||
cur_cust_rec.customer_name);
 END LOOP;
 END;
 /
Calling
Begin
Display_customers;
End;
/
Return from Procedure
create or replace procedure p_print (i_string in
VARCHAR2,i_replace in VARCHAR2 := 'new') is
 begin
 if i_string is null then
return;
 end if;
 DBMS_OUTPUT.put_line(replace(i_string,'<in>',
i_replace));
 end;
 /
Calling and declaring
declare
 v VARCHAR2(50):= 'I just printed my <in> line!';
 begin
 p_print (v,'first');
 p_print (v,'second');
 p_print (v);
 end;
 /

You might also like