Wednesday, February 2, 2011

Explicit Cursor - User Defined Data type - CASE statement

SET SERVEROUTPUT ON
DECLARE
  --Cursor declaration
  CURSOR C_EMP is
      SELECT  INITCAP(ename),
      (CASE 
          WHEN sal + nvl(comm,0) > 4000 THEN 'High Earning'
          WHEN sal + nvl(comm,0) BETWEEN 2000 AND 3999 THEN 'Medium Earning'
          ELSE 'Low Earning'
        END) Earning
        FROM emp;
 
  --TYPE declaration
  TYPE emp_salary_info IS RECORD
  (name emp.ename%TYPE,
  earning VARCHAR2(20));
  --Variable declaration
  v_emp_sal_info emp_salary_info;
BEGIN
  -- open cursor
  OPEN C_EMP;
  LOOP
   -- fetch cursor
    FETCH C_EMP INTO v_emp_sal_info;
      EXIT WHEN C_EMP%NOTFOUND;
      IF C_EMP%ROWCOUNT = 1 THEN
        DBMS_OUTPUT.PUT_LINE( 'ENAME'||'---->'||'EARNING DETAIL' );
      END IF;
      DBMS_OUTPUT.PUT_LINE( v_emp_sal_info.name||'---->'||v_emp_sal_info.earning);
  END LOOP;
  --close cursor
  CLOSE C_EMP;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('No data found exception raised');
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Others Exception raised');
    IF C_EMP%ISOPEN THEN
      CLOSE C_EMP;
    END IF;
END;

anonymous block completed
ENAME---->EARNING DETAIL
Smith---->Low Earning
Allen---->Low Earning
Ward---->Low Earning
Jones---->Medium Earning
Martin---->Medium Earning
Blake---->Medium Earning
Clark---->Medium Earning
Scott---->Medium Earning
King---->High Earning
Turner---->Low Earning
Adams---->Low Earning
James---->Low Earning
Ford---->Medium Earning
Miller---->Low Earning

No comments:

Post a Comment

External Table

Oracle External Table External tables are defined as tables that do not resides in the database allows you to access data that is stor...