DECLARE @SystemDate VARCHAR(8)

select @SystemDate = SYSTEMDATE FROM System (nolock)

select r.referencenumber, r.guestNumber,status, statusrl, rl.LINENUMBER
INTo #Tmp
FROM Reservations r (nolock)
INNER JOIN RESERVATIONROOMLINEXTAB rlx (nolock) on r.ReferenceNumber = rlx.ReferenceNumber and r.GuestNumber =  rlx.GuestNumber and r.ReferenceNumber > 0 and r.GuestNumber = 0
INNER JOIN ReservationRoomline rl (nolock) on rlx.LINENUMBER = rl.LINENUMBER
WHERE Status > ISNULL(Statusrl, -1)
and rl.DepartureDate < @Systemdate

UPDATE ReservationRoomline
SET StatusRL = #tmp.Status
FROM ReservationRoomline rl
INNER JOIN #tmp ON rl.LINENUMBER = #tmp.LINENUMBER

Drop table #Tmp