Procedures in PL/SQL

Procedures in PL/SQL (Procedural Language/Structured Query Language) are a way to encapsulate a set of SQL and PL/SQL statements that perform a specific task. They are stored in the database and can be executed as needed, providing a modular and reusable approach to database programming.

Here’s an overview of creating and using procedures in PL/SQL:

Creating a Procedure

To create a procedure, you use the CREATE PROCEDURE statement followed by the procedure's name, parameters (if any), and the body of the procedure, which contains the executable code. Here’s a basic syntax:

CREATE [OR REPLACE] PROCEDURE procedure_name 
   [ (parameter_name [IN | OUT | IN OUT] datatype [, ...]) ]
IS
   -- Declaration of variables
BEGIN
   -- Executable statements
EXCEPTION
   -- Exception handling
END procedure_name;

Example

Here’s a simple example of a PL/SQL procedure that inserts a new employee record into an employees table:

CREATE OR REPLACE PROCEDURE add_employee (
    p_emp_id IN employees.emp_id%TYPE,
    p_first_name IN employees.first_name%TYPE,
    p_last_name IN employees.last_name%TYPE,
    p_email IN employees.email%TYPE
)
IS
BEGIN
    INSERT INTO employees (emp_id, first_name, last_name, email)
    VALUES (p_emp_id, p_first_name, p_last_name, p_email);
    
    COMMIT;
EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        RAISE;
END add_employee;


Executing a Procedure


To execute a procedure, you use the EXECUTE statement or call it from another PL/SQL block:

EXECUTE add_employee(101, 'John', 'Doe', 'john.doe@example.com');

Or within an anonymous PL/SQL block:

BEGIN
    add_employee(102, 'Jane', 'Smith', 'jane.smith@example.com');
END;

Parameters in Procedures
  • IN: The default parameter mode. The parameter is used to pass values into the procedure.
  • OUT: The parameter is used to return values from the procedure.
  • IN OUT: The parameter can pass initial values into the procedure and return updated values.
Exception Handling

Exception handling in procedures allows you to manage errors that occur during the execution of the procedure. You can define an EXCEPTION block to catch and handle exceptions:

EXCEPTION
    WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_LINE('No data found.');
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');


Benefits of Using Procedures
Modularity: Procedures allow you to break down complex operations into smaller, manageable units.
Reusability: Once a procedure is created, it can be reused in multiple applications or scripts.
Security: Procedures can help control access to data and operations, ensuring that only authorized users can perform specific tasks.
Performance: Stored procedures can improve performance by reducing the amount of data transferred between the database and the application.

By using procedures in PL/SQL, you can create robust, maintainable, and efficient database applications.

Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

Cursors in PL/SQL