DECLARE @PK_CashBox INTEGER

SELECT TOP 1 @PK_CashBox = PK_CashBox FROM CashBox 
WHERE ISNULL(CashRegisterMarking, '') <> ''
AND ISNULL(MarkAsDeleted, 0) = 0

IF ISNULL(@PK_CashBox, 0) > 0
BEGIN
  UPDATE TransactionsRelation SET FK_Cashbox = @PK_CashBox WHERE FK_TransactionNumber IN (
    SELECT t.TransactionNumber FROM Transactions t WITH (NOLOCK) 
    INNER JOIN TransactionsRelation tr WITH (NOLOCK) ON tr.FK_TransactionNumber = t.TransactionNumber
    INNER JOIN CashBox cb WITH (NOLOCK) ON cb.PK_CashBox = tr.FK_CashBox
      AND ISNULL(cb.CashRegisterMarking, '') = ''
    WHERE t.Payment IN (3,4,6,15) --[ptCash, ptCreditCard, ptCurrency, ptGiftCertificateRedeem]
  )
END