PL/SQL Explicit Cursor Example: Employees with Salary > 50,000

PL/SQL Program with Explicit Cursor: Employee Salary > 50,000

PL/SQL Explicit Cursor Example, PL/SQL Employee Salary Query, Oracle Cursor Program, SQL Cursor Tutorial, PL SQL Assignment, Cursor in Database, Employees Table Cursor

📌 Question

Write a PL/SQL program that uses an explicit cursor to retrieve and display the details of all employees from the EMPLOYEES table whose salary is greater than 50,000. The program should display the following information for each qualifying employee:

  • EMPLOYEE_ID
  • FIRST_NAME
  • LAST_NAME
  • SALARY

✅ Answer

PL/SQL Program Using Explicit Cursor

DECLARE
   CURSOR emp_cursor IS
      SELECT employee_id, first_name, last_name, salary
      FROM employees
      WHERE salary > 50000;

   v_emp_id    employees.employee_id%TYPE;
   v_fname     employees.first_name%TYPE;
   v_lname     employees.last_name%TYPE;
   v_salary    employees.salary%TYPE;

BEGIN
   OPEN emp_cursor;
   LOOP
      FETCH emp_cursor INTO v_emp_id, v_fname, v_lname, v_salary;
      EXIT WHEN emp_cursor%NOTFOUND;

      DBMS_OUTPUT.PUT_LINE('ID: ' || v_emp_id || 
                           ', Name: ' || v_fname || ' ' || v_lname ||
                           ', Salary: ' || v_salary);
   END LOOP;
   CLOSE emp_cursor;
END;
/
  

📖 Explanation

  • Explicit Cursor: Defined to fetch employees with salary greater than 50,000.
  • FETCH: Retrieves one row at a time into variables.
  • LOOP: Iterates until no more records are found.
  • DBMS_OUTPUT.PUT_LINE: Displays the result for each employee.

📌 Final Summary

  • This program demonstrates how to use an explicit cursor in PL/SQL.
  • It retrieves only those employees whose salary > 50,000.
  • The output displays Employee ID, First Name, Last Name, and Salary.
  • Useful for filtering and reporting in HR or payroll systems.

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন