更新表时触发了INSTEAD OF INSERT触发器

时间:2015-11-02 07:16:13

标签: sql-server triggers

我有一个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,如触发器中所述,此状态仅用于此触发器。

0 个答案:

没有答案