IF (EXISTS (select proc_name from sysprocedure where LCase(proc_name)=LCase('CalcOccupiedOnlineRooms'))) THEN Drop function CalcOccupiedOnlineRooms END IF GO create function CalcOccupiedOnlineRooms(in @Class integer) returns integer begin declare @result integer; declare @startDate date; set @startDate = bvrData1; set @result = 0; for F as Curs scroll cursor for select ReserveNo, FromDate, ToDate, RoomClass, Broi from v_OccupiedOnlineRooms where RoomClass = @Class do if (select DateIntersection(FromDate,ToDate,bvrData1,bvrData2)) > 0 then set @result = @result + Broi; end if; end for; /* while @startDate < bvrData2 loop end loop; */ return @result; end; GO IF (EXISTS (select Table_name from SysTable where LCase(Table_name)=LCase('v_OccupiedOnlineRooms'))) THEN Drop View v_OccupiedOnlineRooms END IF GO create view v_OccupiedOnlineRooms as select R.ReserveNo, FromDateDt as FromDate, ToDate = (FromDate + NumDays), RP.RoomClass, broi = count(ToDate) from Reserve as R join ReservePlan as RP on R.ReserveNo = RP.ReserveNo and KodAdmin = -1 and R.Status = 1 and RP.Status = 1 and R.DateReserve >= DateToFloat(now(*)) group by R.ReserveNo, FromDate, ToDate, RoomClass order by R.ReserveNo