SlideShare a Scribd company logo
SQL Procedures & Functions
Procedures
A procedure is a named PL/SQL block which performs one or more specific task. This is similar to a
procedure in other programming languages. A procedure has a header and a body.
The header consists of the name of the procedure and the parameters or variables passed to the
procedure.
The body consists or declaration section, execution section and exception section similar to a general
PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.
We can pass parameters to procedures in three ways :
Parameters Description
IN type These types of parameters are used to send values to stored procedures.
OUT type These types of parameters are used to get values from stored procedures. This is
similar to a return type in functions.
IN OUT type These types of parameters are used to send values and get values from stored
procedures.
A procedure may or may not return any value.
Syntax:
CREATE [OR REPLACE] PROCEDURE procedure_name (<Argument> {IN, OUT, IN
OUT}<Datatype>,…)
IS
Declaration section<variable, constant> ;
BEGIN
Execution section
EXCEPTION
Exception section
END
IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL
Blocks. The code between IS and BEGIN forms the Declaration section.
The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE together the
procedure is created if no other procedure with the same name exists or the existing procedure is
replaced with the current code.
How to execute a Procedure?
There are two ways to execute a procedure :
● From the SQL prompt : EXECUTE [or EXEC] procedure_name;
● Within another procedure – simply use the procedure name : procedure_name;
Example:
create table named emp have two column id and salary with number datatype.
CREATE OR REPLACE PROCEDURE p1(id IN NUMBER, sal IN NUMBER)
AS
BEGIN
INSERT INTO emp VALUES(id, sal);
DBMD_OUTPUT.PUT_LINE('VALUE INSERTED.');
END;
/
Output:
Functions:
A function is a named PL/SQL Block which is similar to a procedure. The major difference between a
procedure and a function is, a function must always return a value, but a procedure may or may not
return a value.
Syntax:
CREATE [OR REPLACE] FUNCTION function_name [parameters]
RETURN return_datatype; {IS, AS}
Declaration_section <variable,constant> ;
BEGIN
Execution_section
Return return_variable;
EXCEPTION
exception section
Return return_variable;
END;
RETURN TYPE: The header section defines the return type of the function. The return datatype can be any
of the oracle datatype like varchar, number etc.
The execution and exception section both should return a value which is of the datatype defined in the
header section.
How to execute a Function?
A function can be executed in the following ways.
● As a part of a SELECT statement : SELECT emp_details_func FROM dual;
● In a PL/SQL Statements like, : dbms_output.put_line(emp_details_func);
This line displays the value returned by the function .
Example:
create or replace function getsal (no IN number) return number
is
sal number(5);
begin
select salary into sal from emp where id=no;
return sal;
end;
/
Output:
In the example we are retrieving the ‘salary’ of employee with id 2 to variable ‘sal’.
The return type of the function is number.
Destroying procedure and function :
Syntax:
DROP PROCEDURE/FUNCTION PROCEDURE/FUNCTION_NAME;
Procedures VS Functions:
● A function MUST return a value
● A procedure cannot return a value
● Procedures and functions can both return data in OUT and IN OUT parameters
● The return statement in a function returns control to the calling program and returns the results
of the function
● The return statement of a procedure returns control to the calling program and cannot return a
value
● Functions can be called from SQL, procedure cannot
● Functions are considered expressions, procedure are not

More Related Content

Similar to SQL Procedures & Functions (20)

PPTX
Oracle: Procedures
DataminingTools Inc
 
PPTX
Oracle: Procedures
oracle content
 
PPT
DAC training-batch -2020(PLSQL)
RajKumarSingh213
 
PPTX
Unit 3
Abha Damani
 
PPTX
9. DBMS Experiment Laboratory PresentationPPT
TheVerse1
 
PPTX
Function
Sherin Fathima
 
PPT
plsql les02
sasa_eldoby
 
PPTX
PLSQL.pptx
git21is061t
 
PPTX
Sql Functions And Procedures
DataminingTools Inc
 
PPTX
MS SQL SERVER: Sql Functions And Procedures
sqlserver content
 
PPTX
MS SQLSERVER:Sql Functions And Procedures
sqlserver content
 
PPTX
PL_SQL_1.pptx fvbxcfbhxdfgh .
RAMIROENRIQUERAMALLO
 
PPS
Procedures/functions of rdbms
jain.pralabh
 
PPTX
Unit 3(rdbms)
Jay Patel
 
PPTX
Unit 3(rdbms)
Jay Patel
 
PPTX
TWO.pptx
SasidharGanugapenta
 
PPT
plsql les01
sasa_eldoby
 
PPTX
Plsql guide 2
Vinay Kumar
 
PPTX
Lecture 3.2_Subprogrammm - Function.pptx
pproychd
 
PPTX
5. stored procedure and functions
Amrit Kaur
 
Oracle: Procedures
DataminingTools Inc
 
Oracle: Procedures
oracle content
 
DAC training-batch -2020(PLSQL)
RajKumarSingh213
 
Unit 3
Abha Damani
 
9. DBMS Experiment Laboratory PresentationPPT
TheVerse1
 
Function
Sherin Fathima
 
plsql les02
sasa_eldoby
 
PLSQL.pptx
git21is061t
 
Sql Functions And Procedures
DataminingTools Inc
 
MS SQL SERVER: Sql Functions And Procedures
sqlserver content
 
MS SQLSERVER:Sql Functions And Procedures
sqlserver content
 
PL_SQL_1.pptx fvbxcfbhxdfgh .
RAMIROENRIQUERAMALLO
 
Procedures/functions of rdbms
jain.pralabh
 
Unit 3(rdbms)
Jay Patel
 
Unit 3(rdbms)
Jay Patel
 
plsql les01
sasa_eldoby
 
Plsql guide 2
Vinay Kumar
 
Lecture 3.2_Subprogrammm - Function.pptx
pproychd
 
5. stored procedure and functions
Amrit Kaur
 

More from JeevananthamArumugam (10)

PDF
Advanced Encryption Standard - AES
JeevananthamArumugam
 
PDF
Classical Encryption Techniques
JeevananthamArumugam
 
PDF
Data Encryption Standard
JeevananthamArumugam
 
PDF
Square and Multiply - Shortcut Method
JeevananthamArumugam
 
PDF
Chinese Reminder Theorem
JeevananthamArumugam
 
PDF
Euler's Theorem
JeevananthamArumugam
 
PDF
NCC | General Subject | Public speaking
JeevananthamArumugam
 
PDF
NCC | General Subject | Communication Skills | Year II
JeevananthamArumugam
 
PDF
CNS - Chapter1
JeevananthamArumugam
 
PDF
IoT - Scope and Opportunities
JeevananthamArumugam
 
Advanced Encryption Standard - AES
JeevananthamArumugam
 
Classical Encryption Techniques
JeevananthamArumugam
 
Data Encryption Standard
JeevananthamArumugam
 
Square and Multiply - Shortcut Method
JeevananthamArumugam
 
Chinese Reminder Theorem
JeevananthamArumugam
 
Euler's Theorem
JeevananthamArumugam
 
NCC | General Subject | Public speaking
JeevananthamArumugam
 
NCC | General Subject | Communication Skills | Year II
JeevananthamArumugam
 
CNS - Chapter1
JeevananthamArumugam
 
IoT - Scope and Opportunities
JeevananthamArumugam
 
Ad

Recently uploaded (20)

PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PPTX
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
digestive system for Pharm d I year HAP
rekhapositivity
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Ad

SQL Procedures & Functions

  • 1. SQL Procedures & Functions Procedures A procedure is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage. We can pass parameters to procedures in three ways : Parameters Description IN type These types of parameters are used to send values to stored procedures. OUT type These types of parameters are used to get values from stored procedures. This is similar to a return type in functions. IN OUT type These types of parameters are used to send values and get values from stored procedures. A procedure may or may not return any value.
  • 2. Syntax: CREATE [OR REPLACE] PROCEDURE procedure_name (<Argument> {IN, OUT, IN OUT}<Datatype>,…) IS Declaration section<variable, constant> ; BEGIN Execution section EXCEPTION Exception section END IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section. The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE together the procedure is created if no other procedure with the same name exists or the existing procedure is replaced with the current code. How to execute a Procedure? There are two ways to execute a procedure : ● From the SQL prompt : EXECUTE [or EXEC] procedure_name; ● Within another procedure – simply use the procedure name : procedure_name; Example: create table named emp have two column id and salary with number datatype. CREATE OR REPLACE PROCEDURE p1(id IN NUMBER, sal IN NUMBER) AS BEGIN INSERT INTO emp VALUES(id, sal); DBMD_OUTPUT.PUT_LINE('VALUE INSERTED.'); END; / Output:
  • 3. Functions: A function is a named PL/SQL Block which is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value. Syntax: CREATE [OR REPLACE] FUNCTION function_name [parameters] RETURN return_datatype; {IS, AS} Declaration_section <variable,constant> ; BEGIN Execution_section Return return_variable; EXCEPTION exception section Return return_variable; END; RETURN TYPE: The header section defines the return type of the function. The return datatype can be any of the oracle datatype like varchar, number etc. The execution and exception section both should return a value which is of the datatype defined in the header section. How to execute a Function? A function can be executed in the following ways. ● As a part of a SELECT statement : SELECT emp_details_func FROM dual; ● In a PL/SQL Statements like, : dbms_output.put_line(emp_details_func); This line displays the value returned by the function .
  • 4. Example: create or replace function getsal (no IN number) return number is sal number(5); begin select salary into sal from emp where id=no; return sal; end; / Output: In the example we are retrieving the ‘salary’ of employee with id 2 to variable ‘sal’. The return type of the function is number. Destroying procedure and function : Syntax: DROP PROCEDURE/FUNCTION PROCEDURE/FUNCTION_NAME; Procedures VS Functions: ● A function MUST return a value ● A procedure cannot return a value ● Procedures and functions can both return data in OUT and IN OUT parameters ● The return statement in a function returns control to the calling program and returns the results of the function ● The return statement of a procedure returns control to the calling program and cannot return a value ● Functions can be called from SQL, procedure cannot ● Functions are considered expressions, procedure are not