-- Original case: CR011425
-- Moved from \Post\Once to \Post: CR015943
SET NOCOUNT ON 
DECLARE @ReferenceNumber INT
DECLARE @CreatedBy INT

DECLARE db_cur CURSOR FOR
  SELECT DISTINCT Reservations.ReferenceNumber, f.CreatedBy
  FROM Reservations WITH (NOLOCK)
  INNER JOIN ReservationMain WITH (NOLOCK) ON ReservationMain.ReferenceNumber = Reservations.ReferenceNumber
  INNER JOIN ReservationFollowupXtab rfx WITH (NOLOCK) ON rfx.ReferenceNumber = ReservationMain.ReferenceNumber
  INNER JOIN Followup f WITH (NOLOCK) ON rfx.FollowupNumber = f.FollowupNumber 
    AND SubType = 1
  WHERE ISNULL(ReservationMain.StaffId, 0) = 0
  AND Reservations.ReferenceNumber > 0
OPEN db_cur

FETCH NEXT FROM db_cur INTO @ReferenceNumber, @CreatedBy

WHILE @@FETCH_STATUS = 0
BEGIN
  UPDATE ReservationMain SET StaffId = @CreatedBy WHERE ReferenceNumber = @ReferenceNumber
  FETCH NEXT FROM db_cur INTO @ReferenceNumber, @CreatedBy
END

CLOSE db_cur
DEALLOCATE db_cur
