0

トリガーに問題があります。単一行の更新では機能しますが、複数の更新では、サブクエリが複数の値を返すというエラーが発生します。これを処理する方法。

    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
4

2 に答える 2

0

プロセスで複数の CustomerID が追加される可能性はありますか? この行は問題です:

   SET @customerID= (SELECT customerID FROM inserted)  
   SET @stat= (SELECT stat FROM inserted) --table inserted contains inserted 

customerID と stat がすべての行で一貫していることが保証されている場合は、次のように MAX で修正できます。

   SET @customerID= (SELECT MAX(customerID) FROM inserted)  
   SET @stat= (SELECT MAX(stat) FROM inserted) --table inserted contains inserted 

ただし、これらのアイテムが挿入されたすべての行で一貫していることが保証されていない場合、問題が発生します。その場合は、値をカーソルで移動するためのカーソルが必要になります。

また、これに変更します。

    SET @balance = 
            (
                SELECT SUM( (totalprice-(paidAmount+concession)) ) 
                FROM paymentData
                WHERE customerID = @customerID

            )
             Declare @paid int
             SET @paid = 
            (
                SELECT SUM(paidAmount) 
                FROM paymentData
                WHERE customerID = @customerID
            )
于 2013-06-04T12:19:38.483 に答える