Tuesday, March 3, 2009

For Loop to Read Records in Cursor

--+----------------------------------------------------
--| Declaration Section - Define Department Stats
--+----------------------------------------------------
Declare

Cursor c_DeptStats Is
Select DName
, Count(*) EmpCnt
From Dept d, Emp e
Where d.Deptno = e.Deptno
Group By DName
Order By DName;
--+----------------------------------------------------
--| Execution: Loop to read records in cursor
--+----------------------------------------------------
Begin
For v_Rec In c_DeptStats Loop
---------------------------------------------------
If v_Rec.EmpCnt > 4 Then
Dbms_Output.Put_Line (v_Rec.DName ||' Has ' ||
v_Rec.EmpCnt||' Employees.');
End If;
---------------------------------------------------
End Loop;
End;
/

Embedded Select in For Loop

For v_Ctr in (Select DName, DeptNo From Dept)
Loop
...executable statements...
End Loop;

No comments:

Post a Comment