我的触发器有问题,它适用于单行更新,但对于多次更新,它会给出错误,即子查询返回一个以上的值。如何处理。
GO
ALTER TRIGGER [dbo].[OnpaymentUpdate]
ON [dbo].[paymentData]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @customerID NCHAR(50), @lastpaymentDate DATETIME, @stat nchar(50), @month int;
SET @customerID= (SELECT customerID FROM inserted)
SET @stat= (SELECT stat FROM inserted) --table inserted contains inserted rows (or new updated rows)
set @lastpaymentDate = (SELECT MAX(paymentDate) FROM paymentReceipt where customerID=@customerID)
SET @month= (SELECT DATEDIFF(MONTH, @lastpaymentDate,GETDATE()))
DECLARE @balance BIGINT
SET @balance =
(
SELECT (totalprice-(paidAmount+concession))
FROM paymentData
WHERE customerID = @customerID
)
Declare @paid int
SET @paid =
(
SELECT paidAmount
FROM paymentData
WHERE customerID = @customerID
)
UPDATE PaymentData
SET balanceAmount = @balance ,
lastpaymentDate=@lastpaymentDate
WHERE customerID = @customerID
if (@month >=2 and @stat!='Cancel' and @stat!='Refund' And @stat!='Refunded' and @stat!='Transfered' and @stat!='Transfer')
Begin
IF (@month <2 and @stat='Defaulter')
SET @stat='Regular'
IF (@balance<=0 and @paid >0)
SET @stat='Payment Completed'
else
SET @stat='Defaulter'
End
else
Begin
if @stat='Refund'
Set @stat='Refunded'
if @stat='Cancled'
Set @stat='Cancel'
if @stat='Transfer'
Set @stat='Transfered'
End
UPDATE PaymentData
SET stat =@stat
WHERE customerID = @customerID
END