Lecture Notes Unit5 chapter17 Stored procedures and functions

Murugan146644 251 views 12 slides Aug 07, 2024
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Description:
Welcome to the comprehensive guide on Relational Database Management System (RDBMS) concepts, tailored for final year B.Sc. Computer Science students affiliated with Alagappa University. This document covers fundamental principles and advanced topics in RDBMS, offering a structured appr...


Slide Content

RDBMS -Unit V
Chapter 17
Stored Procedures and Functions
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
AlagappaGovernment Arts College, Karaikudi.
(Affiliated by AlagappaUniversity)
Mailid: [email protected]
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO

Stored Procedure -Definition
➢AstoredprocedureisagroupofSQLandPL/SQL
commandsthatexecutecertaintasks.
Procedure Trigger
The user must call a
procedure either from a
program or manually.
A trigger is automatically
executed when a trigger event
occurs.

TRIGGER -Definition

Creating a Stored Procedure
➢Astoredprocedurehastwoparts:aspecification
sectionandtheprocedurebody.TheCREATE
PROCEDURE commandisresponsibleforthe
creationofprocedures.
➢Syntax:
CREATE [OR REPLACE] PROCEDURE [schema.]
procedure
[ (argument [IN | OUT | IN OUT] datatype
[, argument [IN | OUT | IN OUT] datatype] ...)]
{IS|AS} {pl/sql_subprogram_body| external_body}

Creating a Stored Procedure
Arguments:
OR REPLACE
➢Re-createstheprocedureifitalreadyexists.
schema
➢Theschemathatwillcontaintheprocedure.Ifschemais
omitted,Oraclecreatestheprocedureinthecurrentschema.
procedure
➢The name of the procedure to be created.
argument
➢Thenameofaprocedureargument.Iftheproceduredoesnot
acceptarguments,youcanomittheparenthesesafteritsname.
IN
➢Specifies that a value is determined for the argument when the
procedure is called.

Creating a Stored Procedure
OUT
➢Specifiesthattheprocedurereturnsavalueforthisargumentin
itscallingenvironmentaftertheexecution.
IN OUT
➢Specifiesthatavaluemustbedeterminedfortheargumentwhen
theprocedureiscalledandthattheprocedurereturnsavaluein
itscallingenvironmentaftertheexecution.
datatype
➢An argument can be any data type supported by PL/SQL.
IS pl/sql_subprogram_body
➢The procedure definition. Procedure definitions are written in
PL/SQL.

Creating a Stored Procedure –Example Program No Argument
1.Create table emp2 (name varchar2(25), salary number(7), eno
number(3));
2.Insert few records.
3.View the records
4. Type the following program proc1.sql (This procedure
increases the employees’ salaries 10 percent)
create or replace procedure increase1
is
begin
update system.emp2 set salary=salary*1.1;
end;
/
5. Run the program (@ filepath)
6. Execute the procedure by the following statement
exec increase1
7. Verify the modified output (salary field) in the emp2 database
with step 3. (select * from emp2;)

Creating a Stored Procedure –Example Program with Arguments
➢Thisprocedureincreasestheemployees’salariesbutthistime
thepayraiseisonlyforthoseinaparticularemployeenumber
andtheincreaseisadifferentpercentage.
➢CREATE PROCEDURE increase2 (venoIN emp2.eno%type,
percent number) IS
BEGIN
update emp2
set salary=salary*(1+percent/100)
where eno=veno;
END;
/
➢Execute the procedure by the following statement
exec increase2(105,20)
➢Here 105 is the employee number and 20 is the percentage to be
increase.

Deleting a Stored Procedure
➢To delete a stored procedure, use the DROP PROCEDURE
command:
➢Syntax :
DROP PROCEDURE name
➢Example:
SQL> drop procedure increase1;
Procedure dropped.

Function
➢AFunctionisagroupofSQLandPL/SQLcommandsthat
executecertaintasks.
➢DifferencebetweenProcedureandFunction
Procedure Function
It does not return a valueIt returns a value
The procedure invoked
by the procedure name.
The function invoked by
the function name with
selectstatement.

Creating a Function
➢TheCREATEFUNCTIONcommandcreatesafunctionasan
isolatedschemaobject.Youcanalsocreateafunctionaspartof
apackage.
Syntax:
CREATE[ORREPLACE]FUNCTION[schema.]function
[(argument[IN|OUT|INOUT]datatype
[,argument[IN|OUT|INOUT]datatype]...)]
RETURNdatatype{IS|AS}pl/sql_subprogram_body

Creating a Function –Example Program for counting number of records
1.Createtheempltablewiththefieldasname,salaryanddno
(departmentnumber)andinsertfewrecords.
2.Typethefollowingprogram
createorreplacefunctioncountemp(vdnoinempl.dno%type)
returnnumberis
emptotnumber;
begin
selectcount(*)intoemptotfromemplwheredno=vdno;
returnemptot;
end;
/
3.Runtheprogram
4.Executethefunctionbyusingthefollowingstatement
selectcountemp(101)fromdual