-1
我有交易表和交易歷史表。我已經寫了更新觸發器,它將維護歷史記錄表中的更新記錄,它不會允許複製,但如何更新ISActive列,當它將狀態更新爲Y或N時.Record正在維護,但如何更新對同一記錄所做的更改複製插入到歷史表後,我需要如何再次更新
ALTER TRIGGER [dbo].[trg_Transaction]
ON [dbo].[Transaction_Details]
FOR UPDATE
AS
Declare @ApplicationNo varchar(20);
Declare @PaymentID varchar(20);
Declare @CreatedBy varchar(20)
DECLARE @CreatedDate DATETIME ;
DECLARE @UpdatedDate DATETIME ;
DECLARE @UpdatedBy VARCHAR(20) ;
DECLARE @IsActive CHAR(2)
Select @ApplicationNo = I.ApplicationNo from INSERTED I
Select @PaymentID = I.PaymentID from INSERTED I
Select @CreatedDate = CAST(I.CreatedDate AS DATETIME) from INSERTED I
Select @UpdatedDate = CAST(I.UpdatedDate AS DATETIME)from INSERTED I
Select @UpdatedBy = I.UpdatedBy from INSERTED I
Select @IsActive = I.IsActive from INSERTED I
INSERT INTO [BPS].[dbo].[Transaction_Details_History]
([ApplicationNo]
,[PaymentID]
,[CreatedBy]
,[UpdatedDate]
,[UpdatedBy]
,[IsActive])
SELECT @ApplicationNo,
@PaymentID,
@CreatedBy,
@UpdatedDate,
@UpdatedBy,
@IsActive
FROM INSERTED I
WHERE NOT EXISTS
(SELECT
[ApplicationNo]
,[PaymentID]
,[CreatedBy]
,[UpdatedDate]
,[UpdatedBy]
,[IsActive]
FROM [Transaction_Details_History]
WHERE @ApplicationNo = [ApplicationNo]
)
GO
只是做一個更新 –
你的邏輯並不清楚,但有一點是清楚的是,你的觸發不正確寫入,它被設計只用於更新單行上班,而在現實中的記憶居民表「插入」可以包含多行。你是什麼意思*「我怎樣才能更新對同一記錄沒有重複的變化」*? – GarethD
你可能想看看'merge'語句。 – jpw