DECLARE @PK_ReportAutomationId INT
DECLARE @CustomerNumber INT

DECLARE db_cur CURSOR FOR
  SELECT PK_ReportAutomationId
  FROM ReportAutomation WITH (NOLOCK)
  WHERE PK_ReportAutomationId NOT IN (
    SELECT FK_ReportAutomationId
    FROM ReportAutomationCustomerXTab WITH (NOLOCK)
  )
OPEN db_cur

FETCH NEXT FROM db_cur INTO @PK_ReportAutomationId

WHILE @@FETCH_STATUS = 0
BEGIN
  DECLARE db_cur_Cust CURSOR FOR 
    SELECT CustomerNumber
    FROM Customer WITH (NOLOCK)
    WHERE CustomerTypeNumber = 7
    AND (HotelNumber > 0 OR LocalHotel = 1)
    AND ISNULL(Customer.MarkAsDeleted, 0) = 0
  OPEN db_cur_cust

  FETCH NEXT FROM db_cur_cust INTO @CustomerNumber
  WHILE @@FETCH_STATUS = 0
  BEGIN
    INSERT INTO ReportAutomationCustomerXTab (FK_ReportAutomationId, FK_CustomerNumber) VALUES 
      (@PK_ReportAutomationId, @CustomerNumber)
    FETCH NEXT FROM db_cur_cust INTO @CustomerNumber
  END

  CLOSE db_cur_cust
  DEALLOCATE db_cur_cust

  FETCH NEXT FROM db_cur INTO @PK_ReportAutomationId
END

CLOSE db_cur
DEALLOCATE db_cur