table 上的条件触发器

Conditional trigger on a table

我无法创建简单的条件触发器来将数据插入新的 table。下面是触发器及其输出:

CREATE TRIGGER [dbo].[test_trigger] 
ON [dbo].[Export_Data]
FOR INSERT
AS
BEGIN
    INSERT INTO test (Terminal_ID, EmpIdentification, SwipeDateTime) 
        SELECT  
            DeviceId, UserId, LogDate 
        FROM 
            Inserted
END

输出如下:

我的要求是只需要 Terminal_ID = 32 & 33 的日志。

谢谢

只需在 Inserted table 的 select 语句中添加一个 where 子句。如果您将 Export_Data.DeviceId 插入 test.Terminal_ID,那么您的过滤器应该是 where Inserted.DeviceId in (32,33):

CREATE  TRIGGER [dbo].[test_trigger] on [dbo].[Export_Data]
for insert
as
begin
    insert into test(Terminal_ID,EmpIdentification,SwipeDateTime) 
    select DeviceId,UserId,LogDate from Inserted
    WHERE DeviceId in (32, 33)
end