0% found this document useful (0 votes)
5 views

RDBMS_Unit6

The document provides an overview of procedures and functions in PL/SQL, detailing their structure, syntax, and how to create and execute them. It explains the differences between procedures and functions, the various parameter modes (IN, OUT, IN OUT), and includes examples of creating and calling procedures and functions. Additionally, it covers how to delete procedures and the execution of PL/SQL functions.

Uploaded by

aavishkaarclub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

RDBMS_Unit6

The document provides an overview of procedures and functions in PL/SQL, detailing their structure, syntax, and how to create and execute them. It explains the differences between procedures and functions, the various parameter modes (IN, OUT, IN OUT), and includes examples of creating and calling procedures and functions. Additionally, it covers how to delete procedures and the execution of PL/SQL functions.

Uploaded by

aavishkaarclub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

10/7/2019

Procedures and functions


Unit 6

Prepared by Shimi Biju 1

 A procedure is a named PL/SQL block which performs one


or more specific task.
 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 of 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.

Prepared by Shimi Biju 2

1
10/7/2019

General Syntax to create a procedure is:

CREATE [OR REPLACE] PROCEDURE proc_name [list of


parameters]
IS
Declaration section
BEGIN
Execution section
EXCEPTION
Exception section
END;

Prepared by Shimi Biju 3

 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.
Prepared by Shimi Biju 4

2
10/7/2019

Example

 The following example creates a simple procedure that


displays the string 'Hello World!' on the screen when
executed.

 CREATE OR REPLACE PROCEDURE greetings


 IS
 BEGIN
 dbms_output.put_line('Hello World!');
 END;

Prepared by Shimi Biju 5

execute a procedure

 There are two ways to execute a procedure.


1) From the SQL prompt.
EXECUTE [or EXEC] procedure_name;
2) Within another procedure – simply use the procedure
name.
procedure_name;

Prepared by Shimi Biju 6

3
10/7/2019

Executing a Standalone Procedure

 The procedure can be called from another PL/SQL block −

 BEGIN
 greetings;
 END;

Prepared by Shimi Biju 7

example
 The below example creates a procedure ‘employer_details’ which gives the details of the
employee.

CREATE OR REPLACE PROCEDURE employer_details


IS
CURSOR emp_cur IS
SELECT first_name, last_name, salary FROM emp_tbl;
emp_rec emp_cur%rowtype;
BEGIN
FOR emp_rec in emp_cur
LOOP
dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name || ' ' ||emp_cur.salary);
END LOOP;
END;

Prepared by Shimi Biju 8

4
10/7/2019

Deleting a Standalone Procedure

 A standalone procedure is deleted with the DROP


PROCEDURE statement. Syntax for deleting a procedure is

 DROP PROCEDURE procedure-name;

 greetings procedure can be deleted by using the following


statement −

 DROP PROCEDURE greetings;

Prepared by Shimi Biju 9

Procedures: Passing Parameters

 We can pass parameters to procedures in three ways.


1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters
 A procedure may or may not return any value.

Prepared by Shimi Biju 10

5
10/7/2019

Parameter Modes in PL/SQL Subprograms


 IN
 An IN parameter lets you pass a value to the subprogram.
 It is a read-only parameter.
 Inside the subprogram, an IN parameter acts like a constant.
 It cannot be assigned a value.
 You can pass a constant, literal, initialized variable, or
expression as an IN parameter.
 You can also initialize it to a default value; however, in that
case, it is omitted from the subprogram call.
 It is the default mode of parameter passing.
 Parameters are passed by reference.

Prepared by Shimi Biju 11

Parameter Modes in PL/SQL Subprograms

 OUT
 An OUT parameter returns a value to the calling program.
Inside the subprogram, an OUT parameter acts like a
variable.
 You can change its value and reference the value after
assigning it.
 The actual parameter must be variable and it is passed by
value.

Prepared by Shimi Biju 12

6
10/7/2019

Parameter Modes in PL/SQL Subprograms

 IN OUT
 An IN OUT parameter passes an initial value to a
subprogram and returns an updated value to the caller.
 It can be assigned a value and the value can be read.
 The actual parameter corresponding to an IN OUT formal
parameter must be a variable, not a constant or an
expression.
 Formal parameter must be assigned a value. Actual
parameter is passed by value.

Prepared by Shimi Biju 13

 Create PROCEDURE findMin(x IN number, y IN number, z


OUT number) IS
 BEGIN
 IF x < y THEN
 z:= x;
 ELSE
 z:= y;
 END IF;
 END;

Prepared by Shimi Biju 14

7
10/7/2019

 DECLARE
 a number;
 b number;
 c number;
 BEGIN
 a:= 23;
 b:= 45;
 findMin(a, b, c);
 dbms_output.put_line(' Minimum of (23, 45) : ' || c);
 END;
Prepared by Shimi Biju 15

 DECLARE
 a number;
 PROCEDURE squareNum(x IN OUT number) IS
 BEGIN
 x := x * x;
 END;
 BEGIN
 a:= 23;
 squareNum(a);
 dbms_output.put_line(' Square of (23): ' || a);
 END;
Prepared by Shimi Biju 16

8
10/7/2019

Example1:

 Using IN and OUT parameter:

 Let’s create a procedure which gets the name of the employee


when the employee id is passed.

CREATE OR REPLACE PROCEDURE emp_name (id IN NUMBER,


emp_name OUT NUMBER)
IS
BEGIN
SELECT first_name INTO emp_name FROM emp_tbl WHERE empID = id;
END;

Prepared by Shimi Biju 17

 DECLARE
 empName varchar(20);
 CURSOR id_cur SELECT id FROM emp_ids;
 BEGIN
 FOR emp_rec in id_cur
 LOOP
 emp_name(emp_rec.id, empName);
 dbms_output.putline('The employee ' || empName || ' has id ' ||
emp-rec.id);
 END LOOP;
 END;
Prepared by Shimi Biju 18

9
10/7/2019

Using IN OUT parameter in procedures:

 CREATE OR REPLACE PROCEDURE emp_salary_increase


 (emp_id IN emptbl.empID%type, salary_inc IN OUT emptbl.salary%type)
 IS
 tmp_sal number;
 BEGIN
 SELECT salary INTO tmp_sal FROM emp_tbl WHERE empID = emp_id;
 IF tmp_sal between 10000 and 20000 THEN
salary_inout := tmp_sal * 1.2;
 ELSIF tmp_sal between 20000 and 30000 THEN
 salary_inout := tmp_sal * 1.3;
 ELSIF tmp_sal > 30000 THEN
 salary_inout := tmp_sal * 1.4;
 END IF;
 END;
Prepared by Shimi Biju 19

 The below PL/SQL block shows how to execute the above 'emp_salary_increase' procedure.

 DECLARE
 CURSOR updated_sal is
 SELECT empID,salary
 FROM emp_tbl;
 pre_sal number;
 BEGIN
 FOR emp_rec IN updated_sal LOOP
 pre_sal := emp_rec.salary;
 emp_salary_increase(emp_rec.empID, emp_rec.salary);
 dbms_output.put_line('The salary of ' || emp_rec.empID ||
 ' increased from '|| pre_sal || ' to '||emp_rec.salary);
 END LOOP;
 END;

Prepared by Shimi Biju 20

10
10/7/2019

PL/SQL Functions

 What is a Function in PL/SQL?


 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.

Prepared by Shimi Biju 21

General Syntax to create a function

CREATE [OR REPLACE] FUNCTION function_name


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];

Prepared by Shimi Biju 22

11
10/7/2019

 Where,

 function-name specifies the name of the function.


 [OR REPLACE] option allows the modification of an existing function.
 The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside
and OUT represents the parameter that will be used to return a value
outside of the procedure.
 The function must contain a return statement.
 The RETURN clause specifies the data type you are going to return
from the function.
 function-body contains the executable part.
 The AS keyword is used instead of the IS keyword for creating a
standalone function.

Prepared by Shimi Biju 23

 create or replace function sum(n1 in number, n2 in


number)
 return number
 is
 n3 number;
 begin
 n3 :=n1+n2;
 return n3;
 end;

Prepared by Shimi Biju 24

12
10/7/2019

program to call the function

 DECLARE
 n3 number;
 BEGIN
 n3 := sum(11,22);
 dbms_output.put_line(‘Sum is: ' || n3);
 END;

Prepared by Shimi Biju 25

Function to find the total number of customers

CREATE OR REPLACE FUNCTION totalCustomers


RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;

RETURN total;
END;
/
Prepared by Shimi Biju 26

13
10/7/2019

Calling function

 DECLARE
 c number(2);
 BEGIN
 c := totalCustomers();
 dbms_output.put_line('Total no. of Customers: ' || c);
 END;

Prepared by Shimi Biju 27

DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;

Prepared by Shimi Biju 28

14
10/7/2019

 DECLARE
 a number;
 b number;
 c number;
 FUNCTION findMax(x IN number, y IN number)
 RETURN number
 IS
 z number;
 BEGIN
 IF x > y THEN
 z:= x;
 ELSE
 Z:= y;
 END IF;
 RETURN z;
 END;
 BEGIN
 a:= 23;
 b:= 45;

Prepared cby:= findMax(a,
Shimi Biju b); 29

 dbms_output.put_line(' Maximum of (23,45): ' || c);


 END;

How to execute a PL/SQL Function?

A function can be executed in the following ways.

1) Since a function returns a value we can assign it to a variable.


employee_name := employer_details_func;
If ‘employee_name’ is of datatype varchar we can store the name
of the employee by assigning the return type of the function to it.
2) As a part of a SELECT statement
SELECT employer_details_func FROM dual;
3) In a PL/SQL Statements like,
dbms_output.put_line(employer_details_func);
This line displays the value returned by the function.

Prepared by Shimi Biju 30

15

You might also like