SlideShare a Scribd company logo
1 of 18
Procedures
and Functions
in PL/SQL
Subprogram
A subprogram is a program unit/module that performs a
particular task. These subprograms are combined to form
larger programs. This is basically called the 'Modular
design'.
A subprogram can be created −
• At the schema level: standalone subprogram
• Inside a package: packaged subprogram
• Inside a PL/SQL block: PL/SQL subprogram
Types of Subprograms
PL/SQL provides two kinds of subprograms −
1. Procedures − These subprograms do not return a
value directly; mainly used to perform an action.
2. Functions − These subprograms return a single
value; mainly used to compute and return a value.
Parts of a PL/SQL Subprogram
Each PL/SQL subprogram has a name, and like
anonymous PL/SQL blocks, the named blocks will
also have the following three parts −
1. Declarative Part - Optional
2. Executable Part - Mandatory
3. Exception-handling - Optional
Procedure
Procedures are simply a named block, that executes certain
task.
It never returns values.
It can be executed from within another procedure, function, or
trigger.
It can also be executed directly from the SQL prompt using
EXECUTE command.
It is completely portable among platforms in which Oracle is
executed.
Syntax of Procedure
CREATE [OR REPLACE] PROCEDURE procedure_name
[ ( parameter1 [ IN | OUT | IN OUT ] data_type [ , ... ] ) ]
IS | AS
BEGIN
< procedure_body >
END;
Example of Procedure
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
SQL>Procedure created.
Executing a Procedure
Using the EXECUTE keyword
SQL>EXECUTE greetings;
Calling the name of the procedure from a PL/SQL block
BEGIN
greetings;
END;
Result:-
Hello World!
PL/SQL procedure successfully completed.
Deleting a Procedure
Syntax for deleting a procedure is −
DROP PROCEDURE procedure-name;
Example-
DROP PROCEDURE greetings;
Parameter Modes in PL/SQL
Subprograms
Three types of parameter modes are applicable in
PL/SQL that change the behavior or nature of the
parameters:-
1. IN
2. OUT
3. IN OUT
IN Mode
 It is the default mode of parameter passing.
 It is used for sending input values to the procedure from the caller.
 Parameters are passed by reference.
 It is a read-only parameter.
 Example,
CREATE OR REPLACE PROCEDURE Square ( num IN NUMBER )
AS
BEGIN
dbms_output.put_line(‘Square = ‘ || ( num * num ) );
END;
 Execution,
EXECUTE Square(5);
Square = 25
PL/SQL procedure successfully completed.
OUT Mode
 It is used for sending output values from the procedure to the caller.
 Parameters are passed by value.
 Inside the subprogram, it acts like an uninitialized variable.
 The actual parameter must be variable.
 Example,
CREATE OR REPLACE PROCEDURE Square ( num IN NUMBER , res OUT NUMBER)
AS
BEGIN
res := num * num;
END;
 Execution,
DECLARE
temp NUMBER;
BEGIN
Square ( 5 , temp );
dbms_output.put_line ( 'Square = ' || temp );
END;
 Result,
Square = 25
Statement processed.
IN OUT Mode
 It is used for both sending input values to the procedure from the caller and
sending output values from the procedure to the caller.
 Parameters are passed by value.
 Inside the subprogram, it acts like an initialized variable.
 The actual parameter must be variable.
 Example,
CREATE OR REPLACE PROCEDURE Square ( num IN OUT NUMBER )
AS
BEGIN
num := num * num;
END;
 Execution,
DECLARE
temp NUMBER;
BEGIN
temp := 5;
Square ( temp );
dbms_output.put_line ( 'Square = ' || temp );
END;
 Result,
Square = 25
Statement processed.
Function
A function is same as a procedure except that it
returns a value.
It is also called a stored function, or a user function
or a user-defined function.
It can be used as part of a SQL expression.
Syntax of Function
CREATE [OR REPLACE] FUNCTION function_name
[ ( parameter1 [ IN | OUT | IN OUT ] data_type [ , ... ] ) ]
RETURN return_type
IS | AS
BEGIN
< function_body >
END;
Example of Function
CREATE OR REPLACE FUNCTION Square ( num IN
NUMBER )
RETURN NUMBER
IS
BEGIN
RETURN ( num * num );
END;
SQL>Function created.
Executing a Function
 Using the EXECUTE keyword
Example,
VARIABLE res NUMBER
EXECUTE :res := Square(5);
PRINT res;
Result:- 25
 Calling the name of the function from a PL/SQL block
Example,
DECLARE
temp NUMBER;
BEGIN
temp := 5;
dbms_output.put_line ( 'Square = ' || Square ( temp ));
END;
Result:-
Square = 25
Statement processed.
 Using the SELECT statement
Example,
SELECT Square(5) FROM DUAL;
Result:- 25
Thank You

More Related Content

What's hot

pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Curso Básico de Pl Sql Oracle
Curso Básico de Pl Sql OracleCurso Básico de Pl Sql Oracle
Curso Básico de Pl Sql Oracleluisguil
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts Bharat Kalia
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEDrkhanchanaR
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQLPooja Dixit
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggersrehaniltifat
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functionsfarwa waqar
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group FunctionsSalman Memon
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...rehaniltifat
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functionsAmrit Kaur
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQLPooja Dixit
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Achmad Solichin
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 

What's hot (20)

pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
 
Curso Básico de Pl Sql Oracle
Curso Básico de Pl Sql OracleCurso Básico de Pl Sql Oracle
Curso Básico de Pl Sql Oracle
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
Functions in c
Functions in cFunctions in c
Functions in c
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggers
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQL
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 

Similar to Procedure n functions

Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 
DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)RajKumarSingh213
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQLKailash N
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Proceduressqlserver content
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Proceduressqlserver content
 
Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPTSujayaBiju
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprogramsSimon Huang
 

Similar to Procedure n functions (20)

SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)DAC training-batch -2020(PLSQL)
DAC training-batch -2020(PLSQL)
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQL
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
Module04
Module04Module04
Module04
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Procedures
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Procedures
 
Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPT
 
PLSQL
PLSQLPLSQL
PLSQL
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprograms
 

Recently uploaded

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Recently uploaded (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

Procedure n functions

  • 2. Subprogram A subprogram is a program unit/module that performs a particular task. These subprograms are combined to form larger programs. This is basically called the 'Modular design'. A subprogram can be created − • At the schema level: standalone subprogram • Inside a package: packaged subprogram • Inside a PL/SQL block: PL/SQL subprogram
  • 3. Types of Subprograms PL/SQL provides two kinds of subprograms − 1. Procedures − These subprograms do not return a value directly; mainly used to perform an action. 2. Functions − These subprograms return a single value; mainly used to compute and return a value.
  • 4. Parts of a PL/SQL Subprogram Each PL/SQL subprogram has a name, and like anonymous PL/SQL blocks, the named blocks will also have the following three parts − 1. Declarative Part - Optional 2. Executable Part - Mandatory 3. Exception-handling - Optional
  • 5. Procedure Procedures are simply a named block, that executes certain task. It never returns values. It can be executed from within another procedure, function, or trigger. It can also be executed directly from the SQL prompt using EXECUTE command. It is completely portable among platforms in which Oracle is executed.
  • 6. Syntax of Procedure CREATE [OR REPLACE] PROCEDURE procedure_name [ ( parameter1 [ IN | OUT | IN OUT ] data_type [ , ... ] ) ] IS | AS BEGIN < procedure_body > END;
  • 7. Example of Procedure CREATE OR REPLACE PROCEDURE greetings AS BEGIN dbms_output.put_line('Hello World!'); END; / SQL>Procedure created.
  • 8. Executing a Procedure Using the EXECUTE keyword SQL>EXECUTE greetings; Calling the name of the procedure from a PL/SQL block BEGIN greetings; END; Result:- Hello World! PL/SQL procedure successfully completed.
  • 9. Deleting a Procedure Syntax for deleting a procedure is − DROP PROCEDURE procedure-name; Example- DROP PROCEDURE greetings;
  • 10. Parameter Modes in PL/SQL Subprograms Three types of parameter modes are applicable in PL/SQL that change the behavior or nature of the parameters:- 1. IN 2. OUT 3. IN OUT
  • 11. IN Mode  It is the default mode of parameter passing.  It is used for sending input values to the procedure from the caller.  Parameters are passed by reference.  It is a read-only parameter.  Example, CREATE OR REPLACE PROCEDURE Square ( num IN NUMBER ) AS BEGIN dbms_output.put_line(‘Square = ‘ || ( num * num ) ); END;  Execution, EXECUTE Square(5); Square = 25 PL/SQL procedure successfully completed.
  • 12. OUT Mode  It is used for sending output values from the procedure to the caller.  Parameters are passed by value.  Inside the subprogram, it acts like an uninitialized variable.  The actual parameter must be variable.  Example, CREATE OR REPLACE PROCEDURE Square ( num IN NUMBER , res OUT NUMBER) AS BEGIN res := num * num; END;  Execution, DECLARE temp NUMBER; BEGIN Square ( 5 , temp ); dbms_output.put_line ( 'Square = ' || temp ); END;  Result, Square = 25 Statement processed.
  • 13. IN OUT Mode  It is used for both sending input values to the procedure from the caller and sending output values from the procedure to the caller.  Parameters are passed by value.  Inside the subprogram, it acts like an initialized variable.  The actual parameter must be variable.  Example, CREATE OR REPLACE PROCEDURE Square ( num IN OUT NUMBER ) AS BEGIN num := num * num; END;  Execution, DECLARE temp NUMBER; BEGIN temp := 5; Square ( temp ); dbms_output.put_line ( 'Square = ' || temp ); END;  Result, Square = 25 Statement processed.
  • 14. Function A function is same as a procedure except that it returns a value. It is also called a stored function, or a user function or a user-defined function. It can be used as part of a SQL expression.
  • 15. Syntax of Function CREATE [OR REPLACE] FUNCTION function_name [ ( parameter1 [ IN | OUT | IN OUT ] data_type [ , ... ] ) ] RETURN return_type IS | AS BEGIN < function_body > END;
  • 16. Example of Function CREATE OR REPLACE FUNCTION Square ( num IN NUMBER ) RETURN NUMBER IS BEGIN RETURN ( num * num ); END; SQL>Function created.
  • 17. Executing a Function  Using the EXECUTE keyword Example, VARIABLE res NUMBER EXECUTE :res := Square(5); PRINT res; Result:- 25  Calling the name of the function from a PL/SQL block Example, DECLARE temp NUMBER; BEGIN temp := 5; dbms_output.put_line ( 'Square = ' || Square ( temp )); END; Result:- Square = 25 Statement processed.  Using the SELECT statement Example, SELECT Square(5) FROM DUAL; Result:- 25