1
我得到这个触发器工作单行插入或更新,但是当我尝试一次更新多行时,它会给出错误的是,子查询返回多个值。用于处理多个更新行的SQL Server触发器。子查询返回多个值
例如
update paymentdata
set stat=1
触发代码是在这里
USE [AGP]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[OnpaymentUpdate]
ON [dbo].[paymentData]
AFTER UPDATE --operations you want trigger to fire on
AS
BEGIN
SET NOCOUNT ON;
DECLARE @customerID NCHAR(50), @lastpaymentDate DATETIME, @stat nchar(50), @month int;
SET @customerID= (SELECT customerID FROM inserted) --table inserted contains inserted rows (or new updated rows)
SET @stat= (SELECT stat FROM inserted) --table inserted contains inserted rows (or new updated rows)
set @lastpaymentDate = (SELECT MAX(paymentDate) FROM paymentReceipt where [email protected])
SET @month= (SELECT DATEDIFF(MONTH, @lastpaymentDate,GETDATE()))
DECLARE @balance BIGINT
SET @balance =
(
SELECT (totalprice-(paidAmount+concession))
FROM paymentData
WHERE customerID = @customerID
)
UPDATE PaymentData
SET balanceAmount = @balance ,
[email protected]
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)
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 [email protected]at
WHERE customerID = @customerID
END
在一次更新中将所有这些变量和ifs转换为case语句。 – 2013-05-01 14:23:35