Sample cursors in PL/SQL
DECLARE
CURSOR emp_cur IS SELECT empno,sal,deptno
FROM empkris;
v_sal empkris.sal%TYPE;
v_empno empkris.empno%TYPE;
v_deptno empkris.deptno%TYPE;
v_ename empkris.ename%TYPE;
BEGIN
IF emp_cur%ISOPEN THEN
CLOSE emp_cur;
end if;
OPEN emp_cur;
LOOP
FETCH emp_cur INTO v_empno,v_sal,v_deptno;
EXIT when emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( 'Employee number is : ' || v_empno || ' his salary is :'|| v_sal || ' and he works in dept no :'|| v_deptno);
END LOOP;
CLOSE emp_cur;
END;
/
DECLARE
CURSOR emp_cur IS SELECT empno,sal,deptno
FROM empkris;
v_sal empkris.sal%TYPE;
v_empno empkris.empno%TYPE;
v_deptno empkris.deptno%TYPE;
v_ename empkris.ename%TYPE;
BEGIN
For c in emp_cur
LOOP
v_empno:=c.empno;
v_deptno:=c.deptno;
v_sal:=c.sal;
FETCH emp_cur INTO v_empno,v_sal,v_deptno;
DBMS_OUTPUT.PUT_LINE( 'Employee number is : ' || v_empno || ' his salary is :'|| v_sal || ' and he works in dept no :'|| v_deptno);
END LOOP;
END;
/
DECLARE
v_sal empkris.sal%TYPE;
v_empno empkris.empno%TYPE;
v_deptno empkris.deptno%TYPE;
v_ename empkris.ename%TYPE;
v_empnon empkris.empno%TYPE:=999;
CURSOR c_employee is select empno,deptno from empkris where empno=v_empnon;
BEGIN
OPEN c_employee;
LOOP
FETCH c_employee INTO v_empno,v_deptno;
EXIT WHEN c_employee%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( 'Employee number is : ' || v_empno || ' and he works in dept no :'|| v_deptno);
END LOOP;
END;
/
CURSOR emp_cur IS SELECT empno,sal,deptno
FROM empkris;
v_sal empkris.sal%TYPE;
v_empno empkris.empno%TYPE;
v_deptno empkris.deptno%TYPE;
v_ename empkris.ename%TYPE;
BEGIN
IF emp_cur%ISOPEN THEN
CLOSE emp_cur;
end if;
OPEN emp_cur;
LOOP
FETCH emp_cur INTO v_empno,v_sal,v_deptno;
EXIT when emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( 'Employee number is : ' || v_empno || ' his salary is :'|| v_sal || ' and he works in dept no :'|| v_deptno);
END LOOP;
CLOSE emp_cur;
END;
/
DECLARE
CURSOR emp_cur IS SELECT empno,sal,deptno
FROM empkris;
v_sal empkris.sal%TYPE;
v_empno empkris.empno%TYPE;
v_deptno empkris.deptno%TYPE;
v_ename empkris.ename%TYPE;
BEGIN
For c in emp_cur
LOOP
v_empno:=c.empno;
v_deptno:=c.deptno;
v_sal:=c.sal;
FETCH emp_cur INTO v_empno,v_sal,v_deptno;
DBMS_OUTPUT.PUT_LINE( 'Employee number is : ' || v_empno || ' his salary is :'|| v_sal || ' and he works in dept no :'|| v_deptno);
END LOOP;
END;
/
DECLARE
v_sal empkris.sal%TYPE;
v_empno empkris.empno%TYPE;
v_deptno empkris.deptno%TYPE;
v_ename empkris.ename%TYPE;
v_empnon empkris.empno%TYPE:=999;
CURSOR c_employee is select empno,deptno from empkris where empno=v_empnon;
BEGIN
OPEN c_employee;
LOOP
FETCH c_employee INTO v_empno,v_deptno;
EXIT WHEN c_employee%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( 'Employee number is : ' || v_empno || ' and he works in dept no :'|| v_deptno);
END LOOP;
END;
/