Showing posts with label For Loop. Show all posts
Showing posts with label For Loop. Show all posts

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;

Loops

Declare
v_Ctr Binary_Integer := 1;

Begin
While v_Ctr < 5 Loop
Do Something
v_Ctr := v_Ctr + 1;
End Loop;


Declare
v_Ctr Binary_Integer := 1;
v_Max Binary_Integer := &Max_Value;

Begin
Loop
Do Something
v_Ctr := v_Ctr + 1;
Exit When v_Ctr > v_Max;
End Loop;


/* v_Ctr Is Implicitly Declared As Binary_Integer. */
/* Can be a variable */
Begin
For v_Ctr In 1..Least(CtrA, CtrB) Loop
Do Something
End Loop;