Tuesday, 2 April 2013

Cursor With SubQuery

Ex: 1:-


DECLARE
  V_DEPTNO DEPT.DEPTNO%TYPE;
 V_DNAME DEPT.DNAME%TYPE;
 V_STAFF NUMBER(6);
 CURSOR STAFFCOUNTCURSOR IS
    SELECT T1.DEPTNO,T1.DNAME,T2.STAFF FROM DEPT T1,
     (SELECT DEPTNO,COUNT(*) STAFF FROM CHEMP GROUP BY DEPTNO) T2
       WHERE T1.DEPTNO=T2.DEPTNO AND T2.STAFF>=5;
BEGIN
  OPEN STAFFCOUNTCURSOR ;
  LOOP
    FETCH STAFFCOUNTCURSOR INTO V_DEPTNO,V_DNAME,V_STAFF;
    EXIT WHEN STAFFCOUNTCURSOR%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(V_DNAME||' '||V_DEPTNO||' '||V_STAFF);
END LOOP;
CLOSE STAFFCOUNTCURSOR;
END;
/

Ur's
AmarAlam

0 comments:

Post a Comment