Program Using Implicit Cursor
i)PL/SQL program using only an implicit cursor to select data from a table and print it.
DECLARE
v_employee_id employees.employee_id%TYPE;
v_first_name employees.first_name%TYPE;
v_last_name employees.last_name%TYPE;
BEGIN
FOR rec IN (SELECT employee_id, first_name, last_name FROM employees WHERE
department_id = 10) LOOP
v_employee_id := rec.employee_id;
v_first_name := rec.first_name;
v_last_name := rec.last_name;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id || ', Name: ' || v_first_name ||
' ' || v_last_name);
END LOOP;
END;
Output
Employee ID: 101, Name: John Doe
Employee ID: 102, Name: Jane Smith
Employee ID: 103, Name: Alice Johnson
Program Using Explicit Cursor
ii)PL/SQL program that uses only an explicit cursor to select data from a table and print it.
DECLARE
-- Declare the explicit cursor
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name FROM employees WHERE department_id =
10;
-- Declare variables to hold the cursor values
v_employee_id employees.employee_id%TYPE;
v_first_name employees.first_name%TYPE;
v_last_name employees.last_name%TYPE;
BEGIN
-- Open the cursor
OPEN emp_cursor;
-- Loop through the rows fetched by the cursor
LOOP
FETCH emp_cursor INTO v_employee_id, v_first_name, v_last_name;
EXIT WHEN emp_cursor%NOTFOUND;
-- Process each row
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id || ', Name: ' || v_first_name ||
' ' || v_last_name);
END LOOP;
-- Close the cursor
CLOSE emp_cursor;
END;
output:
Employee ID: 101, Name: John Doe
Employee ID: 102, Name: Jane Smith
Employee ID: 103, Name: Alice Johnson
Program using Trigger
-- Create the employees table
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
name VARCHAR2(50),
salary NUMBER,
department VARCHAR2(20)
);
-- Create a trigger to increase the salary by 10% before insertion
CREATE OR REPLACE TRIGGER update_salary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
:new.salary := :new.salary * 1.10; -- Increase salary by 10%
END;
/
-- Insert a new employee; the trigger will automatically update the salary
INSERT INTO employees (employee_id, name, salary, department)
VALUES (1, 'John Smith', 50000, 'Sales');
INSERT INTO employees (employee_id, name, salary, department)
VALUES (2, 'Jane Doe', 60000, 'Marketing');
-- Display the result from the employees table
SELECT * FROM employees;
Output
EMPLOYEE_ID | NAME | SALARY | DEPARTMENT
------------|-------------|----------|------------
1 | John Smith | 55000.00 | Sales
2 | Jane Doe | 66000.00 | Marketing
Program to design procedure using In and Out parameters
-- Create the employees table
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
name VARCHAR2(50),
salary NUMBER,
department VARCHAR2(20)
);
-- Insert example data into the employees table
INSERT INTO employees (employee_id, name, salary, department) VALUES (1, 'John Smith',
50000, 'Sales');
INSERT INTO employees (employee_id, name, salary, department) VALUES (2, 'Jane Doe',
60000, 'Sales');
INSERT INTO employees (employee_id, name, salary, department) VALUES (3, 'Alice Brown',
55000, 'Sales');
-- Create the procedure to calculate total salary
CREATE OR REPLACE PROCEDURE get_total_salary (
p_department IN VARCHAR2, -- IN parameter: the department name
p_total_salary OUT NUMBER -- OUT parameter: the total salary for the department
) AS
BEGIN
SELECT SUM(salary) INTO p_total_salary
FROM employees
WHERE department = p_department;
IF p_total_salary IS NULL THEN
p_total_salary := 0;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
p_total_salary := 0;
END;
/
-- Call the procedure and display the result
DECLARE
v_department VARCHAR2(20) := 'Sales'; -- Example department
v_total_salary NUMBER; -- Variable to hold the total salary
BEGIN
get_total_salary(p_department => v_department,
p_total_salary => v_total_salary);
DBMS_OUTPUT.PUT_LINE('Total salary for ' || v_department || ' department is: ' ||
v_total_salary);
END;
/
Output :
Total salary for Sales department is: 165000