/*

  Fix any missing Referential Integrity Constraints that were left out or
  turned out to be missing during development and production.

  Also fix any illigal data that might have existed before new referential Integrity 
  constraints were applied.

  BE CAREFULL! BE VERY CAREFULL!

*/

-- BA/20181024: Fix illigal ReservationAddress.Link
UPDATE ReservationAddress
SET Link = NULL
WHERE (Link IS NOT NULL) AND (NOT EXISTS(SELECT TOP 1 * FROM Customer WITH (NOLOCK) WHERE CustomerNumber = Link))


-- BA/20181024: Fix illigal ReservationAddress.ContactPersonCustomerNumber
UPDATE ReservationAddress
SET ContactPersonCustomerNumber = NULL
WHERE (ContactPersonCustomerNumber IS NOT NULL) AND (NOT EXISTS(SELECT TOP 1 * FROM Customer WITH (NOLOCK) WHERE CustomerNumber = ContactPersonCustomerNumber))

