-- Brief description of the organization: XYZ Corporation is a technology company that specializes in software development.

-- Tables
CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(50),
    department_id INT,
    salary INT
);

-- Update, Insert, and Delete
UPDATE employees
SET salary = 60000
WHERE employee_id = 1;

INSERT INTO employees (employee_id, employee_name, department_id, salary)
VALUES (2, 'John Doe', 2, 50000);

DELETE FROM employees
WHERE employee_id = 2;

-- Three different loops
DECLARE
    i INT := 1;
BEGIN
    WHILE i <= 10 LOOP
        DBMS_OUTPUT.PUT_LINE('Loop iteration: ' || i);
        i := i + 1;
    END LOOP;
    
    FOR j IN 1..5 LOOP
        DBMS_OUTPUT.PUT_LINE('For loop iteration: ' || j);
    END LOOP;
    
    FOR k IN (SELECT * FROM employees) LOOP
        DBMS_OUTPUT.PUT_LINE('Employee name: ' || k.employee_name);
    END LOOP;
END;

-- If statement
DECLARE
    salary employees.salary%TYPE;
BEGIN
    SELECT salary INTO salary
    FROM employees
    WHERE employee_id = 1;
    
    IF salary > 50000 THEN
        DBMS_OUTPUT.PUT_LINE('High salary');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Low salary');
    END IF;
END;

-- Cursors
DECLARE
    CURSOR c_emp IS
    SELECT * FROM employees;
    
    emp_rec employees%ROWTYPE;
BEGIN
    OPEN c_emp;
    
    LOOP
        FETCH c_emp INTO emp_rec;
        EXIT WHEN c_emp%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_rec.employee_name);
    END LOOP;
    
    CLOSE c_emp;
END;

-- Trigger
CREATE OR REPLACE TRIGGER salary_trigger
BEFORE UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
    IF :NEW.salary < 50000 THEN
        RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be less than 50000');
    END IF;
END;

-- Exceptions
BEGIN
    RAISE_APPLICATION_ERROR(-20002, 'Explicitly raised exception');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Exception occurred: ' || SQLERRM);
END;

BEGIN
    SELECT salary INTO employees.salary
    FROM employees
    WHERE employee_id = 100;
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_LINE('No data found');
END;

-- Procedure
CREATE OR REPLACE PROCEDURE update_salary (emp_id IN INT, new_salary IN INT)
AS
BEGIN
    UPDATE employees
    SET salary = new_salary
    WHERE employee_id = emp_id;
END;

-- Function
CREATE OR REPLACE FUNCTION get_employee_name (emp_id IN INT)
RETURN VARCHAR2
AS
    emp_name VARCHAR2(50);
BEGIN
    SELECT employee_name INTO emp_name
    FROM employees
    WHERE employee_id = emp_id;
    
    RETURN emp_name;
END;

-- Package
CREATE OR REPLACE PACKAGE employee_package
AS
    PROCEDURE update_salary (emp_id IN INT, new_salary IN INT);
    FUNCTION get_employee_name (emp_id IN INT) RETURN VARCHAR2;
END employee_package;
/ 
by

PL/SQL Online Compiler

Write, Run & Share PL/SQL code online using OneCompiler's Oracle PL/SQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for Oracle PL/SQL running on latest version 23c (23.3.0.0). Getting started with the OneCompiler's Oracle PL/SQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'PL/SQL' and start writing code to learn and test online without worrying about tedious process of installation.

About PL/SQL

PL/SQL is procedural extension for SQL created by Oracle. It is by default embedded into the Oracle Database. PL/SQL program units are compiled and stored inside the Oracle Database which results in optimal execution times as the PL/SQL and SQL run within the same server process.

Syntax help

Following is the syntax structure for the PL/SQL code blocks

DECLARE 
   <declarations section> 
BEGIN 
   <executable command(s)>
EXCEPTION 
   <exception handling> 
END;

Example

DECLARE 
   message  varchar2(100):= 'Hello, World!'; 
BEGIN 
   dbms_output.put_line(message); 
END; 
/

Named procedures

CREATE OR REPLACE FUNCTION 
hello_user
   (user_name IN VARCHAR2) 
    RETURN VARCHAR2
IS
BEGIN
   RETURN 'Hello ' || user_name;
END hello_user;
/

BEGIN
   dbms_output.put_line(hello_user('Peter'));
END;
/

Exception handling

BEGIN
  DBMS_OUTPUT.put_line (1/0);
EXCEPTION
  WHEN OTHERS
  THEN
    DBMS_OUTPUT.put_line ('error is: ' || SQLERRM);
END;