0% found this document useful (0 votes)
2 views

Program Cursor

Uploaded by

pathades585
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program Cursor

Uploaded by

pathades585
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program Cursor

DECLARE
-- Implicit Cursor Example
emp_name VARCHAR2(50);

-- Explicit Cursor Example


CURSOR emp_cursor IS
​ SELECT employee_id, first_name, last_name FROM employees;

-- Cursor with Parameter Example


CURSOR dept_cursor(department_id IN NUMBER) IS
​ SELECT employee_id, first_name FROM employees WHERE department_id =
dept_cursor.department_id;

v_employee_id employees.employee_id%TYPE;
v_first_name employees.first_name%TYPE;
v_last_name employees.last_name%TYPE;
v_dept_emp_id employees.employee_id%TYPE;
v_dept_first_name employees.first_name%TYPE;

BEGIN
-- Implicit Cursor
SELECT first_name INTO emp_name FROM employees WHERE employee_id = 101;
DBMS_OUTPUT.PUT_LINE('Implicit Cursor Example - Employee Name: ' ||
emp_name);

-- Explicit Cursor
OPEN emp_cursor;
LOOP
​ FETCH emp_cursor INTO v_employee_id, v_first_name, v_last_name;
​ EXIT WHEN emp_cursor%NOTFOUND;
​ DBMS_OUTPUT.PUT_LINE('Explicit Cursor - ID: ' || v_employee_id || ', Name: '
|| v_first_name || ' ' || v_last_name);
END LOOP;
CLOSE emp_cursor;
-- Cursor with Parameters
OPEN dept_cursor(10); -- Assuming department_id 10 exists
LOOP
​ FETCH dept_cursor INTO v_dept_emp_id, v_dept_first_name;
​ EXIT WHEN dept_cursor%NOTFOUND;
​ DBMS_OUTPUT.PUT_LINE('Cursor with Parameters - ID: ' || v_dept_emp_id || ',
Name: ' || v_dept_first_name);
END LOOP;
CLOSE dept_cursor;
END;
/

You might also like