IF (EXISTS(Select * from sys.sysprocedure where LCase(proc_name) = LCase('FillOweRentLodg')))
THEN Drop procedure FillOweRentLodg
END IF

GO

create procedure DBA.FillOweRentLodg()
begin
  set bvrSpravkaM=bvrSpravkaM1;
  set bvrSpravkaY=bvrSpravkaY1;
  delete from #OwePaidRentLodg;
  while((bvrSpravkaY < bvrSpravkaY2) or((bvrSpravkaY = bvrSpravkaY2) and(bvrSpravkaM <= bvrSpravkaM2))) loop
    insert into #OwePaidRentLodg(
      select GNo,Month,DaysInMonth,Year,Name,Uni,Kurs,Spec,FN,Address,City,Sex,PriceKind,Usluga,Cena,Kol,SumOwe,KolPaid,SumPaid,
        Razlika,RealDatePayed,BrStudenti,RName,CountryName,PayType,PayTypeStr
        from v_OwePaidLodgMonth);
    if(bvrSpravkaM >= 12) then
      set bvrSpravkaM=1;
      set bvrSpravkaY=bvrSpravkaY+1
    else
      set bvrSpravkaM=bvrSpravkaM+1
    end if
  end loop
end
//[07.04.2009] - Nixon - promeneno DaysInMonth - broj dni v meseca prez koito studenta e bil v obshtejitieto
