exp 8 dbms
exp 8 dbms
A PL/SQL procedure is a reusable unit that encapsulates the specific business logic of the
application. Technically speaking, a PL/SQL procedure is a named block stored as a schema
object in the Oracle Database.
IS
[declaration statements]
BEGIN
[execution statements]
EXCEPTION
[exception handler]
END [procedure_name ];
A procedure begins with a header that specifies its name and an optional parameter list.
Each parameter can be in either IN, OUT, or INOUT mode. The parameter mode specifies
whether a parameter can be read from or written to.
IN
An IN parameter is read-only. You can reference an IN parameter inside a procedure, but you
cannot change its value. Oracle uses IN as the default mode. It means that if you don’t
specify the mode for a parameter explicitly, Oracle will use the IN mode.
OUT
An OUT parameter is writable. Typically, you set a returned value for the OUT parameter
and return it to the calling program. Note that a procedure ignores the value that you supply
for an OUT parameter.
INOUT
An INOUT parameter is both readable and writable. The procedure can be read and modified.
Note that OR REPLACE option allows you to overwrite the current procedure with the new
code.
PL/SQL procedure body
Similar to an anonymous block, the procedure body has three parts. The executable part is
mandatory whereas the declarative and exception-handling parts are optional. The executable
part must contain at least one executable statement.
1) Declarative part
In this part, you can declare variables, constants, cursors, etc. Unlike an anonymous block, a
declaration part of a procedure does not start with the DECLARE keyword.
2) Executable part
This part contains one or more statements that implement specific business logic. It might
contain only a NULL statement.
3) Exception-handling part
This part contains the code that handles exceptions.
name VARCHAR2(50),
marks NUMBER
);
IS
BEGIN
EXCEPTION
p_name := 'Not Found'; -- Handling exception if roll number does not exist
END Get_Student_Name;
DECLARE
BEGIN
Get_Student_Name(v_rollno, v_name);
Scenario:
Create a procedure that increments a student's marks and returns the updated value.
IS
BEGIN
p_marks := p_marks + 5;
-- Updating the Student table
UPDATE Student1
END Update_Marks;
DECLARE
BEGIN
Update_Marks(v_rollno, v_marks);
-- Displaying the updated marks
END;
DECLARE
BEGIN
Update_Marks(v_rollno, v_marks);