Experiment No:7 Date: 1.1 Aim:: Department of Computer Engineering, KBTCOE

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Department of Computer Engineering, KBTCOE

Experiment No:7 Date:

1.1 Aim: PL/SQL Stored Procedure and Stored Function. Write a Stored Procedure namely
proc_Grade for the categorization of student.if marks scored by students in examination is
<=1500 and marks>=990 then student will be placed in distinction category if marks scored
are between 989 and900 category is first class, if marks 899 and 825 category is Higher
Second Class Write a PL/SQL block for using procedure created with above requirement.

1.2 Objective:
 To study syntax and implementation of PL/SQL stored procedure.
 To Study syntax and implementation of PL/SQL stored function

1.3 Hardware used: --

1.4 Software used:


 Latest version of 64 Bit Ubuntu OS
 Oracle 11g.
1.5 Theory:
PL/SQL Stored Procedure and Function:-

Procedure: A procedure is a subprogram that performs a specific action or task. A procedure


has two parts.
 The procedure specification: The procedure specification specifies the procedure name
and the parameters it accepts. It is not necessary to create a procedure that accepts parameters.
 The procedure body: The procedure body contains the declarative section without
DECLARE keyword, the executable section and an exception section.

1. Creating a Procedure: - To create a procedure from the database.


Syntax:-
Create [or replace] PROCEDURE procedure_name [(argument1 [IN / OUT / IN OUT] type),
(argument2 [IN / OUT / IN OUT] type),….]
IS/AS
Procedure_body
Where
Procedure_name: – is the name of the procedure to be created
Argument:- is the name of the procedure parameter
Type:- Is the data type of the associated parameter
Procedure_body:-Is a PL/SQL block that makes up the code of the procedure.

Page 1 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE

IN: - This is default mode. The value of the actual parameter is passed into the procedure.
Inside the procedure the formal parameter is considered read only.

OUT: - Any value the actual parameter has when the procedure is called ignored. Inside the
procedure, the formal parameters are considered as write only. IN OUT: - This mode is
combination of IN and OUT
 CREATE PROCEDURE instructs the compiler to create new procedure. Keyword 'OR
REPLACE' instructs the compile to replace the existing procedure (if any) with the current
one.
 Procedure name should be unique.
 Keyword 'IS' will be used, when the procedure is nested into some other blocks. If the
procedure is standalone then 'AS' will be used. Other than this coding standard, both have the
same meaning.

 Example1: Creating Procedure and calling it using EXEC

In this example, we are going to create a procedure that takes the name as input and prints the
welcome message as output. We are going to use EXEC command to call procedure.
CREATE OR REPLACE PROCEDURE welcome_msg (p_name IN VARCHAR2)
IS
BEGIN
dbms_output.put_line (‘Welcome '|| p_name);
END;
/
EXEC welcome_msg (‘Students’);
Code Explanation:
 Code line 1: Creating the procedure with name 'welcome_msg' and with one
parameter 'p_name' of 'IN' type.
 Code line 4: Printing the welcome message by concatenating the input name.
 Procedure is compiled successfully.
 Code line 7: Calling the procedure using EXEC command with the parameter
'Students'. Procedure is executed, and the message is printed out as "Welcome Students ".

2. Deleting Procedure:-To remove a procedure from the database. Syntax:-


Drop procedure <procedure_name>;
Function:-
Page 2 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE

A function is a subprogram, which is used to compute values. It is similar to a procedure;


function also takes arguments and can be in different modes. Function also can be stored in
the database. It is a PL/SQL block consisting of declarative, executable and exception
section. Function is a standalone PL/SQL subprogram. Like PL/SQL procedure, functions
have a unique name by which it can be referred. These are stored as PL/SQL database objects

Difference between procedure and function is that the procedure call is a PL/SQL statement
by itself, while a function call is called as a part of an expression. A function can return more
than one value using OUT parameter. A function can be called using positional or named
notation.

Below are some of the characteristics of functions.

 Functions are a standalone block that is mainly used for calculation purpose.
 Function use RETURN keyword to return the value, and the datatype of this is
defined at the time of creation.
 A Function should either return a value or raise the exception, i.e. return is mandatory
in functions.
 Function with no DML statements can be directly called in SELECT query whereas
the function with DML operation can only be called from other PL/SQL blocks.
 It can have nested blocks, or it can be defined and nested inside the other blocks or
packages.
 It contains declaration part (optional), execution part, exception handling part
(optional).
 The values can be passed into the function or fetched from the procedure through the
parameters.
 These parameters should be included in the calling statement.
 Function can also return the value through OUT parameters other than using
RETURN.
 Since it will always return the value, in calling statement it always accompanies with
assignment operator to populate the variables.

1. Creating a Function: - To create the subprogram from the database.


Syntax:-
Create [or replace] FUNCTION function_name

Page 3 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE

[(argument1 [IN / OUT / IN OUT] type),


(argument2 [IN / OUT / IN OUT] type), ….]
Return return_type IS / AS
Function_body
Where
Function_name: – is the name of the function to be created
Argument: - is the name of the function parameter
Type: - Is the data type of the associated parameter
Function_body:-Is a PL/SQL block containing code for the function.
IN:-This is default mode. The value of the actual parameter is passed into the procedure.

Inside the procedure the formal parameter is considered read only. OUT:-Any value the actual
parameter has when the procedure is called ignored. Inside the procedure, the formal
parameters are considered as write only.

INOUT:-this mode is combination of IN and OUT

Example:

1. We have to find the total strength of students using functions present


in different sections in a school

// first lets create a table


create table section(s_id int, s_name varchar(20), strength int );

// Inserting values into table


insert into section values(1, 'computer science', 20);
insert into section values(2, 'portal', 45);
insert into section values(3, 'geeksforgeeks', 60);

// Defining function
create or replace function totalStrength

// Defining return type


return integer
as
total integer:=0;

begin

// calculating the sum and storing it in total


select sum(strength) into total from section;
return total;

Page 4 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE

// closing function
end totalStrength;

set serveroutput on;

declare
answer integer;

begin
answer:=totalstrength();
dbms_output.put_line('Total strength of students is ' || answer);
end;
Output:

Total strength of students is 125

2. Now, let’s take an example to demonstrate Declaring, Defining and Invoking a


simple PL/SQLfunction which will compute and return the reverse of a number.

set serveroutput on;


declare

a int;
c int;
n int;
rev int:=0;
r int;

// Defining function
function reverse_it( x IN int)
return int
as
z int;

// function code
begin
n := x;

while (n > 0)
loop
r := mod(n, 10);
rev := (rev * 10) + r;
n := trunc(n / 10);
end loop;

z := rev;
return z;

end ;

BEGIN
a := 123456789;
Page 5 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE

c := reverse_it(a);
dbms_output.put_line('the reverse of number is ' || c);

END;

Output:

The reverse of number is 987654321

2. Deleting a Function: - To remove the subprogram from the database. Syntax:-


Drop function<function_name>;

Signature of Staff with Date

1.6 Questions
1. What is stored procedure? Explain syntax.
2. How function is different from stored procedure? Explain with example.
3. Explain use of stored procedure and stored function in database.

Page 6 of 6 Exp.No.4

You might also like