-2

我有这个触发器我想让它允许多行更新,目前它只处理单行更新。当我更新记录时,它说子查询返回超过 1 个值..

   GO
    ALTER TRIGGER [dbo].[OnReceiptUpdate]
    ON  [dbo].[paymentReceipt]
    AFTER UPDATE 
    AS 
    BEGIN   
    SET NOCOUNT ON;
    Declare   @correctdate VARCHAR(19);
   Declare   @receiptNo VARCHAR(50);
DECLARE @customerID NCHAR(50)
SET @customerID= (SELECT customerID FROM inserted)
 set @correctdate =  (SELECT CONVERT(VARCHAR(19),paymentDate,103) FROM inserted)   
  set @receiptNo =  (SELECT receiptNo FROM inserted)      
   BEGIN

   UPDATE Paymentreceipt
       SET paymentDate = @correctdate 
    WHERE customerID = @customerID and receiptNo=@receiptNo  
   END
 END
4

2 回答 2

2
Update p
Set p.paymentDate = CONVERT(VARCHAR(19),i.paymentDate,103)
From Paymentreceipt p
inner join inserted i
On p.customerID = i.customerID and p.receiptNo = i.receiptNo

我认为应该这样做。

PS 为什么 p.paymentdate 是一个字符串?那是求之不得。

于 2013-06-02T12:46:13.187 回答
1

最简单的方法是在触发器中使用更新语句,例如下面的语句。

UPDATE Paymentreceipt
SET paymentDate = CONVERT(VARCHAR(19),paymentDate,103)
FROM inserted
WHERE Inserted.receiptNo  = Paymentreceipt.receiptNo
AND Inserted.customerID  = Paymentreceipt.customerID  

请注意,我面前没有 SQL 服务器,因此语法可能不是 100% 正确,但这给了你大致的想法。

一般来说,我会尽量避免使用触发器,但如果您真的需要触发器,请使用它,但可以通过使用存储过程来解决这个问题。

于 2013-06-02T12:33:06.150 回答