IF (SELECT Count(*) FROM Folios (NOLOCK)
where ISNULL(ReferenceNumber, 0) > 0 and ISNULL(HotelCustomerNumber, 0) NOT IN (
  select customerNumber from CUSTOMER with (NOLOCK))) > 0
BEGIN
  UPDATE FOLIOS SET HotelCustomerNumber = (SELECT TOP 1 C.CustomerNumber FROM ReservationMain RM with (NOLOCK) 
    inner join Customer C with (NOLOCK) ON C.HotelNumber = RM.HOTELNUMBER
    WHERE RM.REFERENCENUMBER = ReferenceNumber)
  where ISNULL(ReferenceNumber, 0) > 0 and ISNULL(HotelCustomerNumber, 0) NOT IN (
    select customerNumber from CUSTOMER with (NOLOCK))
  DECLARE @Customer INT
  SELECT TOP 1 @Customer = ISNULL(CustomerNumber, 0) FROM Customer WITH (NOLOCK) 
  WHERE CustomerTypeNumber = 7 AND ISNULL(MARKASDELETED, 0)= 0 AND (LocalHotel=1) 
  IF @Customer > 0
    UPDATE FOLIOS SET HotelCustomerNumber = @Customer
    WHERE ISNULL(ReferenceNumber, 0) = 0 and ISNULL(HotelCustomerNumber, 0) NOT IN (
      select customerNumber from CUSTOMER with (NOLOCK))
END

