我有一个Instead of Insert
触发器,如下所示:
ALTER TRIGGER [dbo].[trgBeforeInsertVehicleFeed] ON [dbo].[VehicleFeed]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
insert into VehicleFeed(
FeedOrDealerId
, Name
, Vin
, Stock
, VehicleType
, StatusId
, IsManualEntry
, IsActive
)
select
I.FeedOrDealerId
, I.Name
, I.Vin
, I.Stock
, I.VehicleType
, I.StatusId
, I.IsManualEntry
, I.IsActive
from inserted I
select distinct FeedOrDealerId from inserted
Update VehicleFeed
Set
IsActive= 0
, StatusId = 11
Where
Stock Not In (select stock from inserted)
And FeedOrDealerId = (select Distinct FeedOrDealerId from inserted)
And IsManualEntry = (select Distinct IsManualEntry from inserted)
And IsManualEntry = 0
END
其中有两个陈述。第一个是insert
,另一个是update
。
因此,此触发器在insert
上正常运行。但问题是在更新任何记录时也会触发此触发器。当我更新任何记录时,它的状态变为11,如触发器中所述,此状态仅用于此触发器。