Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Column Aliases in Explicit FOR LOOP Cursors

Ramuplsql10Aug 14 2014 — edited Aug 15 2014

Hi friends

    In the below block(forloop) i am not getting the countsal values but when i execute the query it gives the count.By this(COUNTSAL) the below if condition fails .i don't  know why it fails.

But when i use with simple loop it gives the correct result.

FOR LOOP:

CL SCR;

SET SERVEROUTPUT ON;

DECLARE

  CURSOR C1 IS SELECT EMPNO,ENAME,COUNT(SAL) COUNTSAL FROM EMP GROUP BY EMPNO,ENAME;

  EMP_REC C1%ROWTYPE;

BEGIN

  FOR IND IN C1

    EXIT WHEN C1%NOTFOUND;

  DBMS_OUTPUT.PUT_LINE(IND.EMPNO);

  DBMS_OUTPUT.PUT_LINE('SALCOUNT    :'||EMP_REC.COUNTSAL);

  IF EMP_REC.COUNTSAL > 0 THEN

       DBMS_OUTPUT.PUT_LINE('NAMES'||EMP_REC.ENAME);

  END IF;

  END LOOP;

END;

simple loop;(getting correct output)

CL SCR;

SET SERVEROUTPUT ON;

DECLARE

  CURSOR C1

  IS

  SELECT EMPNO,ENAME,COUNT(SAL) COUNTSAL FROM EMP GROUP BY EMPNO,ENAME;

  EMP_REC C1%ROWTYPE;

BEGIN

  OPEN C1;

  LOOP

  FETCH C1 INTO EMP_REC;

  EXIT WHEN C1%NOTFOUND;

  IF EMP_REC.COUNTSAL > 0 THEN

  DBMS_OUTPUT.PUT_LINE('NAMES    :'||EMP_REC.ENAME);

  END IF;

  END LOOP;

END;

Please suggest me.

This post has been answered by Solomon Yakobson on Aug 14 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 12 2014
Added on Aug 14 2014
3 comments
1,899 views