Monday, October 25, 2010

Cursor For Loop

Consider a PL/SQL code to display the empno, ename of employees of department name pr with CURSOR FOR Loop statement.

Solution :-

declare

cursor c1 is select empno,ename,deptnm from emp where deptnm='pr';

rec c1%ROWTYPE;

begin

for rec in c1 loop

dbms_output.put_line(rec.empno||'|'||rec.ename||'|'||rec.deptnm);

end loop;

end;

.

PL/SQL procedure successfully completed.

SQL> set serveroutput on

SQL> /

1|bina|pr

5|biju|pr

4|RHEA|pr

6|kamlesh|pr

10)

Consider a PL/SQL code to display the employee number and name of top 5 highest paid employees with CURSOR FOR LOOP statement.

declare

cursor c1 is select * from (select distinct basicsal,empno,ename from emp order by basicsal desc) where rownum <=5;

rec c1%rowtype;

begin

dbms_output.put_line('empno|ename|basicsal');

for r in c1 loop

dbms_output.put_line(r.empno||'|'||r.ename||'|'||r.basicsal);

end loop;

end;

.

/

SQL> /

empno|ename|basicsal

6|kamlesh|10000

1|bina|6000

5|biju|6000

4|RHEA|1000

No comments:

Post a Comment