Professional Documents
Culture Documents
Experiment No:7 Date: 1.1 Aim:: Department of Computer Engineering, KBTCOE
Experiment No:7 Date: 1.1 Aim:: Department of Computer Engineering, KBTCOE
Experiment No:7 Date: 1.1 Aim:: Department of Computer Engineering, KBTCOE
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
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.
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 ".
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.
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.
Page 3 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE
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.
Example:
// Defining function
create or replace function totalStrength
begin
Page 4 of 6 Exp.No.4
Department of Computer Engineering, KBTCOE
// closing function
end totalStrength;
declare
answer integer;
begin
answer:=totalstrength();
dbms_output.put_line('Total strength of students is ' || answer);
end;
Output:
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:
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