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

pl/sql Procedure

  • 1.
    Prof. Neeraj Bhargava PoojaDixit Department of Computer Science School of Engineering & System Science MDS, University Ajmer, Rajasthan, India 1
  • 2.
     Procedures arenamed 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/Loada 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 proceduresand 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 donot 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 REPLACEPROCEDURE 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 REPLACEPROCEDURE 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 SQLstatement 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