0
일부 데이터가 Sales 테이블에 삽입 될 때마다 트리거를 생성합니다. 실행되면 Account 테이블의 구매 잔여 금액을 Sales 테이블의 판매 총액에서 뺍니다. 그러나 방아쇠는 잘못된 결과를 제공합니다. 코드는 다음과 같습니다.삽입을위한 트리거가 잘못된 결과를 나타냄
CREATE TRIGGER [dbo].[TRG_Conta_Insert]
ON [dbo].[Sale]
FOR INSERT, UPDATE
AS
BEGIN
declare @IDAccount bigint
declare @SaleTotalAmount money
declare @RemainingAmount money
select @IDAccount= Inserted.ID_Account, @SaleTotalAmount = Sale.Amount, @RemainingAmount = RemainingAmount from Inserted, Account
if (@IDAccount is not null) and (@RemainingAmount - @SaleTotalAmount >= 0)
begin transaction
update Account set RemainingAmount = @RemainingAmount - @SaleTotalAmount where Account.ID_Account = @IDAccount
commit
트리거가 RemainingAmount 열의 잘못된 결과로 계정 테이블을 업데이트하는 이유는 무엇입니까?
할당 쿼리는 교차 조인을 사용하며 트리거가 명령문마다 한 번씩 실행되지 않는다는 공통적 인 오류를 가정합니다. –
아론이 말하는 것은 스칼라 변수를 없애고 업데이트 문을 기반으로 설정하고 기본 키에 삽입 및 판매에 참여해야한다는 것입니다. –
삽입 및 업데이트 중에 동일한 계산을 수행하는 것이 이상하게 보입니다. –