DECLARE @ArrLineNumber INT
DECLARE @WareNumber INT
DECLARE @FK_RecurringPatternId INT
DECLARE @FK_WareLinkNumber INT
DECLARE @HasRule BIT
DECLARE TmpCursor CURSOR FOR
SELECT DISTINCT WareNumber FROM ArrangementLines WITH (NOLOCK) WHERE ISNULL(ArrangementLines.WareHasRule, 0) = 0
OPEN TmpCursor
FETCH NEXT FROM TmpCursor INTO @WareNumber
WHILE (@@FETCH_STATUS <> -1) 
BEGIN
  SELECT @HasRule = 0
  SELECT @FK_RecurringPatternId = 0
  SELECT @FK_WareLinkNumber = 0
  SELECT TOP 1 @FK_RecurringPatternId = FK_RecurringPatternId 
  FROM RecurringPatternWareXTab WITH (NOLOCK)
  INNER JOIN RecurringPattern WITH (NOLOCK) ON RecurringPatternWareXTab.FK_RecurringPatternId = RecurringPattern.Id
  WHERE RecurringPatternWareXTab.FK_WareNumber = @WareNumber


  SELECT TOP 1 @FK_WareLinkNumber = WareLinkNumber
  FROM Ware WITH (NOLOCK)
  INNER JOIN WareLinkGroup with (nolock) on Ware.WareLinkNumber = WareLinkGroup.Id AND ISNULL(WareLinkGroup.FK_RecurringPatternId,0) > 0
  WHERE Ware.WareNumber = @WareNumber

  IF (@FK_RecurringPatternId > 0) 
    SELECT @HasRule = 1  
  IF (@FK_WareLinkNumber > 0)
    SELECT @HasRule = 1

  IF @HasRule = 1
  BEGIN
    UPDATE ArrangementLines
	SET WareHasRule = 1
	WHERE WareNumber = @WareNumber
  END
  
  FETCH NEXT FROM TmpCursor INTO @WareNumber
END
CLOSE TmpCursor
DEALLOCATE TmpCursor
