내 방아쇠에 문제가 생겼다. 단일 행 업데이트에서 작동하지만 여러 업데이트의 경우 하위 쿼리가 하나 이상의 값을 반환한다는 오류가 발생합니다. 이것을 어떻게 처리 할 것인가.SQL 트리거, 둘 이상의 값을 반환하는 하위 쿼리입니다.
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 [email protected])
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 ,
[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 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 [email protected]
WHERE customerID = @customerID
END
당신의 질문에 단서가 있는데, '삽입 된 표에는 삽입 된 행 (또는 새로운 업데이트 된 행)이 들어 있습니다.'라고 쓰여져 * rows *라고 쓰여지고 * 단 하나의 * 값을 선택하려고 시도하는 것입니다. 그것을 스칼라 변수에 할당하십시오. –
이것은 아마도 집합 기반 연산으로 다시 쓰거나 계산 된 열 및/또는 인덱싱 된 뷰로 대체 할 수 있습니다. 그러나 먼저 테이블 구조에 대한 아이디어가 필요합니다. 예를 들어, 특정'customerID'에 대한 PaymentData의 한 행? –
마침내 (지금은) IF (@month> = 2 및 ... begin ... if (@month <2 and ...) - 두 번째' if 문이 항상 만족 될 때 –