Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Just Updating the Missing Records in the Exisiting table.

I try the Nested While Loop whereas the inner loop works well
but the Outer loop doesn't works out for me. I checked lot of times, for me it seems correctly. Where I did Mistake please Sort it out friends .

Thanks in Advance

[edit]Manually indented code - OriginalGriff[/edit]

What I have tried:

SQL
Alter Procedure spExemptedLeaveSettings
As
Begin
    Declare @iLeaveID int = 1 
    Declare @iSMID int = 1133
    Declare @cSex varchar
    Set @cSex = (Select cSex from StaffMaster where iSMID = @iSMID) 
    While(@iSMID <= 1662)
    Begin  
        While(@iLeaveID <= 15)
        Begin 
            if(@cSex = 'M')
            begin
                Insert into StaffLeaveSettings Values (@iSMID,@iLeaveID,0,'E',1,0,'I',Null,Null,Null,Null,Null,Null,Null,Null)
                Update StaffLeaveSettings Set iRefreshYear = 5 where iLeaveID = 4 and iSMID = @iSMID
                Select cSex from StaffMaster where iSMID = @iSMID
                Delete StaffLeaveSettings Where iLeaveID IN (2,5,7,10,11) and iSMID = @iSMID
            end
            else if(@cSex = 'F') 
            begin	
                Insert into StaffLeaveSettings Values (@iSMID,@iLeaveID,0,'E',1,0,'I',Null,Null,Null,Null,Null,Null,Null,Null)
                Update StaffLeaveSettings Set iRefreshYear = 5 where iLeaveID = 4 and iSMID = @iSMID
                Select cSex from StaffMaster where iSMID = @iSMID
                Delete StaffLeaveSettings Where iLeaveID IN (2,7,8,10,11) and iSMID = @iSMID
            end
            Set @iLeaveID = @iLeaveID + 1
        End
        Set @iSMID = @iSMID + 1
    End
End
Posted
Updated 12-Jul-16 23:59pm
v3
Comments
Suvendu Shekhar Giri 13-Jul-16 5:47am    
How you knew that outer loop is not working?
Mohammed Asarudeen R 13-Jul-16 5:50am    
Yes!
abinash panda 13-Jul-16 5:57am    
It looks like your both the loops are working. but after the first pass of outer loop when it comes for the second iteration , @iLeaveID has already reached the max limit so its not entering the inner loop anymore hence it looks like your outer loop is not working more than once but it is looping each time failing to enter the inner loop. Hope it helps. My suggestion you should reinitialize the value of @iLeaveID some where before the inner loop and in the outer loop. i.e. set @iLeaveID = 1;
after begin.

While(@iSMID <= 1662)
Begin
While(@iLeaveID <= 15)

1 solution

  Declare @cSex varchar
    Set @cSex = (Select cSex from StaffMaster where iSMID = @iSMID) 
    While(@iSMID <= 1662)
    Begin  
set @iLeaveID = 1;
        While(@iLeaveID <= 15)
SQL




Try the above code.
 
Share this answer
 
Comments
Mohammed Asarudeen R 13-Jul-16 6:04am    
Thanks Dude
abinash panda 13-Jul-16 6:31am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900