1

更新を実行しようとするこのような更新ステートメントがあります。私の SPROC では、2 つのパラメーター (@TransactAutoID または @ReceiptNo) のいずれか、または両方を渡します。私がやりたいことは、 @TransactAutoID が空の場合、代わりに @ReceiptNo を2番目の選択肢として使用することです。私の理解では、「OR」演算子は引き続きこれを実行するため、より良い、またはより効率的な記述方法はありますか:

OR  (@ReceiptNo IS NOT NULL AND [AutoID] = (SELECT Max(AutoID)  
                                                FROM   [Transact]
                                                WHERE  [ReceiptNo] = @ReceiptNo)

@TransactAutoID が null でない場合でも。ありがとう。

 UPDATE  [Transact]
    SET     [VoidBy]     = @VoidedBy,
            [VoidOn]     = Getdate(),
            [VoidReason] = @VoidedReason
    WHERE   (@TransactAutoID IS NOT NULL 
              AND [AutoID] = @TransactAutoID)
        OR  (@ReceiptNo IS NOT NULL AND [AutoID] = (SELECT Max(AutoID)  
                                                    FROM   [Transact]
                                                    WHERE  [ReceiptNo] = @ReceiptNo)
4

1 に答える 1

1

Just add @TransactAutoID IS NULL in your second statement - and possibly the opposite in the first statement - depending on your logic (e.g. if you pass both, are you expecting it to do something differnt or fail or what?)

e.g.

UPDATE  [Transact] 
SET     [VoidBy]     = @VoidedBy, 
        [VoidOn]     = Getdate(), 
        [VoidReason] = @VoidedReason 
WHERE   (@TransactAutoID IS NOT NULL AND @ReceiptNo IS NULL
          AND [AutoID] = @TransactAutoID) 
    OR  (@TransactAutoID IS NULL AND @ReceiptNo IS NOT NULL AND [AutoID] = (SELECT Max(AutoID)   
                                                FROM   [Transact] 
                                                WHERE  [ReceiptNo] = @ReceiptNo)
于 2012-05-16T13:16:49.260 に答える