1

我有一个这样的更新语句,它试图执行更新。在我的 SPROC 中,我要么传入 2 个参数之一(@TransactAutoID 或 @ReceiptNo),要么同时传入这两个参数。我想要做的是如果@TransactAutoID 为空,那么我将使用@ReceiptNo 作为第二选择来代替它。是否有更好或更有效的编写方式,因为据我了解,“或”运算符仍将继续执行此操作:

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

即使@TransactAutoID 不为空。谢谢。

 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

只需在您的第二个语句中添加 @TransactAutoID IS NULL - 并且可能在第一个语句中添加相反的 - 取决于您的逻辑(例如,如果您同时通过两者,您是否希望它做一些不同的事情或失败或什么?)

例如

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 回答