title: while loop to insert data into table in sql
body: <pre><code> declare @temp int,
  @temp1 int,
  @temp2 int,
  @temp3 int,
  @temp4 int,
  @temp5 int,
  @modid int,
  @supid int,
  @sid varchar(50)
  begin tran
  select * from StudentSupervisor;
  
  select @temp = count(*) from Students s where s.IsLockedOut = '0' and s.IsGraduated = '0'; 
  select @temp1 = count(*)  from staffs st where st.IsLockedOut ='0';
  
  
  
  set @temp5 = round(@temp/@temp1,0);
  
  WHILE (select count(*) from students s where s.IsLockedOut ='0' and s.StudentId not in (select ss.StudentId from StudentSupervisor ss where s.StudentId = ss.StudentId and ss.IsApproved = 1)) != 0
  
  BEGIN
  
  
  select top 1 @sid = s.studentid from students s, StudentSupervisor ss where s.IsLockedOut ='0' and s.StudentId not in (select s.StudentId where s.StudentId = ss.StudentId and ss.IsApproved = 1);
  
  
  select top 1 @supid = st.Staffid, @modid = st.moderatorid from Staffs st where st.IsLockedOut =0 and Quota &lt;=@temp5;
  
          insert into StudentSupervisor 
          (StudentId,SupervisorId,ModeratorId,IsApproved)
      values
          (@sid,@supid,@modid,1)
          update Staffs set quota +=1 where staffs.StaffID = @supid;
  
  END
  
  
  select * from StudentSupervisor;
  
  ROLLBACK  tran
  </code></pre>
  
  <p>Hi all, i am quite stuck on this logic and i did search for solution but i get nothing after overnight work, let me clear with my situation now, first i would like to take count of student that not in studentsupervisor table or in studentsupervisor table but isapproved !=1, then i take the count of staff that quota is not more than total student / total staff,after that i would like to pump student together with staff into studentsupervisor table while they still available.. please let me know what wrong with my dynamic query, thanks</p>