Dung (Donny) Nguyen

Senior Software Engineer

Procedural Language/Structured Query Language (PL/SQL)

PL/SQL, short for Procedural Language/Structured Query Language, is Oracle Corporation’s procedural extension for SQL and the Oracle relational database. It combines the power of SQL with procedural constructs, allowing for more complex and powerful database operations. Here’s an overview of PL/SQL:

Key Features of PL/SQL:

  1. Block Structure:
    • PL/SQL programs are written in logical blocks, each block comprising three sections: declarative, executable, and exception-handling.
    • Example Block Structure:
      DECLARE
        -- Declarative section
        v_variable_name datatype;
      BEGIN
        -- Executable section
        v_variable_name := value;
      EXCEPTION
        -- Exception-handling section
        WHEN exception_name THEN
          -- Handle the exception
      END;
      
  2. Control Structures:
    • Loops: FOR, WHILE, LOOP statements to iterate over a block of statements.
    • Conditional Statements: IF, ELSIF, CASE statements to control the flow based on conditions.
    • Example:
      BEGIN
        FOR i IN 1..10 LOOP
          DBMS_OUTPUT.PUT_LINE('Iteration ' || i);
        END LOOP;
      END;
      
  3. Cursors:
    • Used to handle query results row by row.
    • Implicit Cursors: Automatically created by Oracle for single SQL statements.
    • Explicit Cursors: Defined and managed by the programmer for more complex query handling.
    • Example:
      DECLARE
        CURSOR c_emp IS SELECT empno, ename FROM emp;
      BEGIN
        FOR r_emp IN c_emp LOOP
          DBMS_OUTPUT.PUT_LINE(r_emp.empno || ' ' || r_emp.ename);
        END LOOP;
      END;
      
  4. Exception Handling:
    • Mechanism to handle runtime errors.
    • Built-in exceptions like NO_DATA_FOUND, TOO_MANY_ROWS, and user-defined exceptions.
    • Example:
      BEGIN
        SELECT ...;
      EXCEPTION
        WHEN NO_DATA_FOUND THEN
          DBMS_OUTPUT.PUT_LINE('No data found!');
      END;
      
  5. Procedures and Functions:
    • Procedures: Perform actions but do not return a value.
    • Functions: Perform actions and return a value.
    • Example:
      CREATE OR REPLACE PROCEDURE hello_world IS
      BEGIN
        DBMS_OUTPUT.PUT_LINE('Hello, world!');
      END;
      
      CREATE OR REPLACE FUNCTION add_numbers(a NUMBER, b NUMBER) RETURN NUMBER IS
      BEGIN
        RETURN a + b;
      END;
      
  6. Packages:
    • Group related procedures, functions, variables, and other PL/SQL constructs into a single unit.
    • Provide modularity and encapsulation.
    • Example:
      CREATE OR REPLACE PACKAGE my_package IS
        PROCEDURE proc1;
        FUNCTION func1 RETURN NUMBER;
      END my_package;
           
      CREATE OR REPLACE PACKAGE BODY my_package IS
        PROCEDURE proc1 IS
        BEGIN
          DBMS_OUTPUT.PUT_LINE('In proc1');
        END;
             
        FUNCTION func1 RETURN NUMBER IS
        BEGIN
          RETURN 1;
        END;
      END my_package;
      
  7. Triggers:
    • Automatically execute specified PL/SQL code in response to certain events on a particular table or view.
    • Example:
      CREATE OR REPLACE TRIGGER before_insert
      BEFORE INSERT ON employees
      FOR EACH ROW
      BEGIN
        :NEW.created_at := SYSDATE;
      END;
      

Advantages of PL/SQL:

PL/SQL is a powerful tool for database developers, enabling the creation of efficient and maintainable database applications.