E9 Procedures Functions (2)
E9 Procedures Functions (2)
Procedures in PL/SQL
A PL/SQL procedure is a reusable block of code that contains a specific set of
actions or logic.
The procedure contains two parts:
1. Procedure Header
The procedure header includes the procedure name and optional parameter
list.
It is the first part of the procedure and specifies the name and parameters
2. Procedure Body
The procedure body contains the executable statements that implement the
specific business logic.
It can include declarative statements, executable statements, and exception-
handling statements
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
PL/SQL Function
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand a
procedure may or may not return a value. Except this, all the other things of PL/SQL procedure
are true for PL/SQL function too.
o RETURN clause specifies that data type you are going to return from the function.
o Function_body contains the executable part.
o The AS keyword is used instead of the IS keyword for creating a standalone function.
1. DECLARE
2. n3 number(2);
3. BEGIN
4. n3 := adder(11,22);
5. dbms_output.put_line('Addition is: ' || n3);
6. END;
7. /
Output:
Addition is: 33
Statement processed.
0.05 seconds
1. DECLARE
2. a number;
3. b number;
4. c number;
5. FUNCTION findMax(x IN number, y IN number)
6. RETURN number
7. IS
8. z number;
9. BEGIN
10. IF x > y THEN
11. z:= x;
12. ELSE
13. Z:= y;
14. END IF;
15.
16. RETURN z;
17. END;
18. BEGIN
19. a:= 23;
20. b:= 45;
21.
22. c := findMax(a, b);
23. dbms_output.put_line(' Maximum of (23,45): ' || c);
24. END;
25. /
Output:
Maximum of (23,45): 45
Statement processed.
0.02 seconds