SlideShare a Scribd company logo
Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Science
MDS, University Ajmer, Rajasthan, India
1
 Procedures are named PL/SQL blocks.
 Created/owned by a particular schema
 Privilege to execute a specific procedure can
be granted to or revoked from application
users in order to control data access.
 Requires CREATE PROCEDURE (to create in
your schema) or CREATE ANY PROCEDURE
privilege (to create in other schemas).
2
CREATE [OR REPLACE] PROCEDURE <procedure_name>
(<parameter1_name> <mode> <data type>,
<parameter2_name> <mode> <data type>, ...) {AS|IS}
<Variable declarations>
BEGIN
Executable statements
[EXCEPTION
Exception handlers]
END <optional procedure name>;
 Unique procedure name is required.
 OR REPLACE clause facilitates testing.
 Parameters are optional – enclosed in parentheses
when used.
 AS or IS keyword is used – both work identically.
 Procedure variables are declared prior to the BEGIN
keyword.
 DECLARE keyword is NOT used in named
procedure.
3
 To Compile/Load a procedure use either the “@” symbol
or the START SQL command to compile the file. The
<SQL filename> parameter is the .sql file that contains
the procedure to be compiled.
SQL>@<SQL filename>
SQL>start <SQL filename>
 Filename does not need to be the same as the procedure
name. The .sql file only contains the procedure code.
 Compiled procedure is stored in the database, not the
.sql file.
 Use SHOW ERRORS command if the procedure does not
compile without errors. Use EXECUTE to run procedure.
SQL> show errors;
SQL> EXECUTE Insert_Employee
4
 Both procedures and functions can take parameters.
 Values passed as parameters to a procedure as
arguments in a calling statement are termed actual
parameters.
 The parameters in a procedure declaration are called
formal parameters.
 The values stored in actual parameters are values
passed to the formal parameters – the formal
parameters are like placeholders to store the
incoming values.
 When a procedure completes, the actual parameters
are assigned the values of the formal parameters.
 A formal parameter can have one of three possible
modes: (1) IN, (2), OUT, or (3) IN OUT.
5
 IN – this parameter type is passed to a procedure as a
read-only value that cannot be changed within the
procedure – this is the default mode.
 OUT – this parameter type is write-only, and can only
appear on the left side of an assignment statement in
the procedure – it is assigned an initial value of NULL.
 IN OUT – this parameter type combines both IN and
OUT; a parameter of this mode is passed to a
procedure, and its value can be changed within the
procedure.
 If a procedure raises an exception, the formal
parameter values are not copied back to their
corresponding actual parameters.
6
 Procedures do not allow specifying a
constraint on the parameter data type.
 Example: the following CREATE PROCEDURE
statement is not allowed because of the
specification that constrains the v_Variable
parameter to NUMBER(2). Instead use the
general data type of NUMBER.
/* Invalid constraint on parameter. */
CREATE OR REPLACE PROCEDURE proSample
(v_Variable NUMBER(2), ...)
/* Valid parameter. */
CREATE OR REPLACE PROCEDURE proSample
(v_Variable NUMBER, ...)
7
CREATE OR REPLACE PROCEDURE DisplaySalary IS
-- create local variable with required constraint
temp_Salary NUMBER(10,2);
BEGIN
SELECT Salary INTO temp_Salary FROM Employee
WHERE EmployeeID = '01885';
IF temp_Salary > 15000 THEN
DBMS_OUTPUT.PUT_LINE ('Salary > 15,000.');
ELSE
DBMS_OUTPUT.PUT_LINE ('Salary < 15,000.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Employee not found.');
END DisplaySalary;
/
8
SQL> exec DisplaySalary
Salary > 15,000.
PL/SQL procedure successfully completed.
9
CREATE OR REPLACE PROCEDURE DisplaySalary2(p_EmployeeID
IN CHAR, p_Salary OUT NUMBER) IS
v_Salary NUMBER(10,2);
BEGIN
SELECT Salary INTO v_Salary FROM Employee
WHERE EmployeeID = p_EmployeeID;
IF v_Salary > 15000 THEN
DBMS_OUTPUT.PUT_LINE ('Salary > 15,000.');
ELSE
DBMS_OUTPUT.PUT_LINE ('Salary <= 15,000.');
END IF;
p_Salary := v_Salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Employee not found.');
END DisplaySalary2;
10
DECLARE
v_SalaryOutput NUMBER := 0;
BEGIN
-- call the procedure
DisplaySalary2('01885', v_SalaryOutput);
-- display value of salary after the call
DBMS_OUTPUT.PUT_LINE ('Actual salary: '
||TO_CHAR(v_SalaryOutput));
END;
/
Salary > 15,000.
Actual salary: 16250
PL/SQL procedure successfully completed.
11
 The SQL statement to drop a procedure
is the straight-forward DROP PROCEDURE
<procedureName> command.
 This is a data definition language (DDL)
command, and so an implicit commit
executes prior to and immediately after
the command.
SQL> DROP PROCEDURE DisplaySalary2;
Procedure dropped.
12

More Related Content

What's hot (20)

DDL And DML
DDL And DMLDDL And DML
DDL And DML
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
Triggers
TriggersTriggers
Triggers
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
 
Database programming
Database programmingDatabase programming
Database programming
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Retrieving data using the sql select statement
Retrieving data using the sql select statementRetrieving data using the sql select statement
Retrieving data using the sql select statement
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Oracle Database Sequence
Oracle Database SequenceOracle Database Sequence
Oracle Database Sequence
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
4. plsql
4. plsql4. plsql
4. plsql
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
PLSQL
PLSQLPLSQL
PLSQL
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 

Similar to pl/sql Procedure

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 - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Thuan Nguyen
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14Syed Asrarali
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQLPooja Dixit
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Thuan Nguyen
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!Anas Mohammed
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedureftz 420
 

Similar to pl/sql Procedure (20)

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
 
Module04
Module04Module04
Module04
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQL
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 

More from Pooja Dixit

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptxPooja Dixit
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptxPooja Dixit
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptxPooja Dixit
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptxPooja Dixit
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxPooja Dixit
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptxPooja Dixit
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptxPooja Dixit
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxPooja Dixit
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptxPooja Dixit
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptxPooja Dixit
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptxPooja Dixit
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptxPooja Dixit
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptxPooja Dixit
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithmPooja Dixit
 

More from Pooja Dixit (20)

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptx
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptx
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptx
 
K-Map.pptx
K-Map.pptxK-Map.pptx
K-Map.pptx
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptx
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptx
 
Gray Code.pptx
Gray Code.pptxGray Code.pptx
Gray Code.pptx
 
Flip Flop.pptx
Flip Flop.pptxFlip Flop.pptx
Flip Flop.pptx
 
Encoder.pptx
Encoder.pptxEncoder.pptx
Encoder.pptx
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptx
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptx
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptx
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptx
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptx
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptx
 
Decoders
DecodersDecoders
Decoders
 
Three Address code
Three Address code Three Address code
Three Address code
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 

Recently uploaded

plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsparmarsneha2
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativePeter Windle
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfjoachimlavalley1
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismDeeptiGupta154
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfYibeltalNibretu
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 

Recently uploaded (20)

plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 

pl/sql Procedure

  • 1. Prof. Neeraj Bhargava Pooja Dixit Department of Computer Science School of Engineering & System Science MDS, University Ajmer, Rajasthan, India 1
  • 2.  Procedures are named PL/SQL blocks.  Created/owned by a particular schema  Privilege to execute a specific procedure can be granted to or revoked from application users in order to control data access.  Requires CREATE PROCEDURE (to create in your schema) or CREATE ANY PROCEDURE privilege (to create in other schemas). 2
  • 3. CREATE [OR REPLACE] PROCEDURE <procedure_name> (<parameter1_name> <mode> <data type>, <parameter2_name> <mode> <data type>, ...) {AS|IS} <Variable declarations> BEGIN Executable statements [EXCEPTION Exception handlers] END <optional procedure name>;  Unique procedure name is required.  OR REPLACE clause facilitates testing.  Parameters are optional – enclosed in parentheses when used.  AS or IS keyword is used – both work identically.  Procedure variables are declared prior to the BEGIN keyword.  DECLARE keyword is NOT used in named procedure. 3
  • 4.  To Compile/Load a procedure use either the “@” symbol or the START SQL command to compile the file. The <SQL filename> parameter is the .sql file that contains the procedure to be compiled. SQL>@<SQL filename> SQL>start <SQL filename>  Filename does not need to be the same as the procedure name. The .sql file only contains the procedure code.  Compiled procedure is stored in the database, not the .sql file.  Use SHOW ERRORS command if the procedure does not compile without errors. Use EXECUTE to run procedure. SQL> show errors; SQL> EXECUTE Insert_Employee 4
  • 5.  Both procedures and functions can take parameters.  Values passed as parameters to a procedure as arguments in a calling statement are termed actual parameters.  The parameters in a procedure declaration are called formal parameters.  The values stored in actual parameters are values passed to the formal parameters – the formal parameters are like placeholders to store the incoming values.  When a procedure completes, the actual parameters are assigned the values of the formal parameters.  A formal parameter can have one of three possible modes: (1) IN, (2), OUT, or (3) IN OUT. 5
  • 6.  IN – this parameter type is passed to a procedure as a read-only value that cannot be changed within the procedure – this is the default mode.  OUT – this parameter type is write-only, and can only appear on the left side of an assignment statement in the procedure – it is assigned an initial value of NULL.  IN OUT – this parameter type combines both IN and OUT; a parameter of this mode is passed to a procedure, and its value can be changed within the procedure.  If a procedure raises an exception, the formal parameter values are not copied back to their corresponding actual parameters. 6
  • 7.  Procedures do not allow specifying a constraint on the parameter data type.  Example: the following CREATE PROCEDURE statement is not allowed because of the specification that constrains the v_Variable parameter to NUMBER(2). Instead use the general data type of NUMBER. /* Invalid constraint on parameter. */ CREATE OR REPLACE PROCEDURE proSample (v_Variable NUMBER(2), ...) /* Valid parameter. */ CREATE OR REPLACE PROCEDURE proSample (v_Variable NUMBER, ...) 7
  • 8. CREATE OR REPLACE PROCEDURE DisplaySalary IS -- create local variable with required constraint temp_Salary NUMBER(10,2); BEGIN SELECT Salary INTO temp_Salary FROM Employee WHERE EmployeeID = '01885'; IF temp_Salary > 15000 THEN DBMS_OUTPUT.PUT_LINE ('Salary > 15,000.'); ELSE DBMS_OUTPUT.PUT_LINE ('Salary < 15,000.'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('Employee not found.'); END DisplaySalary; / 8
  • 9. SQL> exec DisplaySalary Salary > 15,000. PL/SQL procedure successfully completed. 9
  • 10. CREATE OR REPLACE PROCEDURE DisplaySalary2(p_EmployeeID IN CHAR, p_Salary OUT NUMBER) IS v_Salary NUMBER(10,2); BEGIN SELECT Salary INTO v_Salary FROM Employee WHERE EmployeeID = p_EmployeeID; IF v_Salary > 15000 THEN DBMS_OUTPUT.PUT_LINE ('Salary > 15,000.'); ELSE DBMS_OUTPUT.PUT_LINE ('Salary <= 15,000.'); END IF; p_Salary := v_Salary; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('Employee not found.'); END DisplaySalary2; 10
  • 11. DECLARE v_SalaryOutput NUMBER := 0; BEGIN -- call the procedure DisplaySalary2('01885', v_SalaryOutput); -- display value of salary after the call DBMS_OUTPUT.PUT_LINE ('Actual salary: ' ||TO_CHAR(v_SalaryOutput)); END; / Salary > 15,000. Actual salary: 16250 PL/SQL procedure successfully completed. 11
  • 12.  The SQL statement to drop a procedure is the straight-forward DROP PROCEDURE <procedureName> command.  This is a data definition language (DDL) command, and so an implicit commit executes prior to and immediately after the command. SQL> DROP PROCEDURE DisplaySalary2; Procedure dropped. 12