CREATE FUNCTION dbo.getGuestNumberOfRoomLine(@LineNumber int,@Refno int)
RETURNS int
AS
BEGIN
 declare @return int
 declare @guestNumber int set @GuestNumber = null

 set @return = 0 
 if not exists (select LineNumber from reservationRoomLineXtab WITH (nolock) where linenumber=@LineNumber and GuestNumber = 0)
 begin
   select top 1 @GuestNumber=ReservationRoomLineXtab.GuestNumber from reservationRoomLineXtab WITH (nolock) inner join reservations WITH (nolock) on reservations.referencenumber = reservationRoomlinextab.referencenumber
   and reservationRoomLineXtab.LineNumber =@LineNumber and reservations.status= 5 and reservations.referencenumber=@Refno
   if isnull(@guestNumber,0) > 0
     set @Return = @GuestNumber
 end



 return @return
end

