CREATE PROCEDURE dbo.PostTranTranXtab @AOriginalTransactionNumber INT, @ANewTransactionNumber INT AS
SET NOCOUNT ON
/*
  Created by Kent Johansen, March 2018
  Update TransactionTransactionXtab
*/

IF ISNULL(@ANewTransactionNumber, 0) > 0
BEGIN
  DECLARE @FKTranNr1 INT
  DECLARE @FKTranNr2 INT
  DECLARE @RelationType INT

  SELECT @FKTranNr1 = FK_TransactionNumber1, @FKTranNr2 = FK_TransactionNumber2, @RelationType = RelationType
  FROM TransactionTransactionXtab WITH (NOLOCK)
  WHERE FK_TransactionNumber1 = @AOriginalTransactionNumber OR FK_TransactionNumber2 = @AOriginalTransactionNumber

  IF @FKTranNr1 = @AOriginalTransactionNumber
    INSERT INTO TransactionTransactionXTab (FK_TransactionNumber1, FK_TransactionNumber2, RelationType) VALUES (@ANewTransactionNumber, @FKTranNr2, @RelationType)
  ELSE IF @FKTranNr2 = @AOriginalTransactionNumber
    INSERT INTO TransactionTransactionXTab (FK_TransactionNumber1, FK_TransactionNumber2, RelationType) VALUES (@FKTranNr1, @ANewTransactionNumber, @RelationType)
END