CREATE PROCEDURE dbo.RecreateArrangementForProduct @AProductNumber INT AS
  --Created AV 2006 0926
  --Last Changed 2007 1701 support for dayproducts added
  --Last changed 2008 0121 dayproducts block: wrong sql.

  DECLARE @Refno INT
  DECLARE @DayProduct BIT
  SELECT @DayProduct = DayProduct FROM Products WITH (NOLOCK) WHERE ProductNumber = @AProductNumber
  DECLARE ReservationCur CURSOR FORWARD_ONLY FOR
  SELECT DISTINCT ReferenceNumber FROM Reservations WITH (NOLOCK) 
  WHERE GuestNumber= 0 AND Status < 5 AND ReferenceNumber > 0 
  AND 
    (
    @DayProduct <> 1
    AND
    (ReferenceNumber IN (SELECT ReferenceNumber FROM Reservations WITH (NOLOCK) WHERE GuestNumber > 0
    AND (ProductNumber = @AProductNumber OR ProductNumber2 = @AProductNumber OR ProductNumber3 = @AProductNumber OR ProductNumber4 = @AProductNumber)))
    OR
    (ReferenceNumber IN (SELECT ReferenceNumber FROM ReservationRoomLineXtab WITH (NOLOCK) WHERE GuestNumber=0 AND LineNumber IN (SELECT LineNumber FROM ReservationRoomLine WITH (NOLOCK)
    WHERE (ProductNumber = @AProductNumber OR ProductNumber2 = @AProductNumber OR ProductNumber3 = @AProductNumber OR ProductNumber4 = @AProductNumber))))
    )
  OR
    (
    @DayProduct = 1
    AND
    ReferenceNumber IN (SELECT ReferenceNumber FROM ReservationConferenceLine WITH (NOLOCK) WHERE  ProductNumber = @AProductNumber)
    )
  OPEN ReservationCur
  FETCH NEXT FROM ReservationCur INTO @Refno
  WHILE (@@FETCH_STATUS <> -1)
  BEGIN
    EXEC RecreateArrangement @Refno
    FETCH NEXT FROM ReservationCur INTO @Refno
  END
  CLOSE ReservationCur
  DEALLOCATE ReservationCur
