IF EXISTS (SELECT 1
           FROM HOTELSETUP WITH (NOLOCK)
           WHERE SEARCHKEY = 'AutoConfirmations' AND KEYVALUE = 1)
BEGIN

  UPDATE RESERVATIONMAIN
  SET AUTOCONFIRMED = 1 
  WHERE 
    AUTOCONFIRMED = 0 AND

    REFERENCENUMBER NOT IN 
   (SELECT REFERENCENUMBER 
    FROM LOCKEDRESERVATIONS (NOLOCK)) AND 
  
    NOT EXISTS 
   (SELECT 1 
    FROM RESERVATIONFOLLOWUPXTAB WITH (NOLOCK) JOIN FOLLOWUP WITH (NOLOCK) ON 
      FOLLOWUP.FOLLOWUPNUMBER = RESERVATIONFOLLOWUPXTAB.FOLLOWUPNUMBER AND
	  FOLLOWUP.CREATEDDATE > DATEADD(DAY, -3, CURRENT_TIMESTAMP) AND
	  FOLLOWUP.SUBTYPE = 1 AND
	  RESERVATIONFOLLOWUPXTAB.REFERENCENUMBER = RESERVATIONMAIN.REFERENCENUMBER)


  UPDATE RESERVATIONS
  SET AUTOCONFIRMED = 1 
  WHERE 
    AUTOCONFIRMED = 0 AND

    REFERENCENUMBER NOT IN 
   (SELECT REFERENCENUMBER 
    FROM LOCKEDRESERVATIONS (NOLOCK)) AND 
  
    NOT EXISTS 
   (SELECT 1 
    FROM RESERVATIONFOLLOWUPXTAB WITH (NOLOCK) JOIN FOLLOWUP WITH (NOLOCK) ON 
      FOLLOWUP.FOLLOWUPNUMBER = RESERVATIONFOLLOWUPXTAB.FOLLOWUPNUMBER AND
	  FOLLOWUP.CREATEDDATE > DATEADD(DAY, -3, CURRENT_TIMESTAMP) AND
	  FOLLOWUP.SubType = 1 AND
	  RESERVATIONFOLLOWUPXTAB.REFERENCENUMBER = RESERVATIONS.REFERENCENUMBER)

END



