嗨,我读了两天,但找不到解决方案,这就是为什么我很高兴我正在尝试触发从最后一条记录中插入数据最高的插入表,而没有重复。我有目标表:
CREATE TABLE [dbo].[exported] ([Repair_Date] [datetime] NULL,
[idKards] [int] NULL, [Position] [nvarchar](32) NULL,
[error] [int] NULL)
并触发:
Create TRIGGER [dbo].[tr2] ON [dbo].[mes] AFTER INSERT
AS
INSERT INTO dbo.[exported]
([idKard],[Repair_Date],[Position],[Error],[name],[model_name],[parent_number])
SELECT c.idKards,i.[Repair_Date],i.[Position],i.error,s.name, mo.Model_Name, pn.Parent_Number FROM inserted i
left outer join dbo.test t on i.idTest=t.idTest
left outer join dbo.Kards k on t.idKards=k.idKards
where [Repair_Error] in (300,400)
and K.idKards > (select max(idKards) from dbo.exported)
dbo.exported 中的示例数据 - 在此表中,所有值都被删除,除了最后一个:
Repair_Date idKards Position error
2012-10-10 00:03:25 91996 IC4303 4
当数据从另一个表(下面的示例数据)插入到表 dbo.mes(触发器打开)时:
Repair_Date idTest Position error
2012-10-10 00:00:58 91996 C524 1
2012-10-10 00:00:56 91996 C522 1
2012-10-10 00:00:54 91996 C537 1
2012-10-10 04:34:31 95694 P1104 1
2012-10-10 06:48:33 97405 P1104 1
2012-10-10 01:31:17 93088 P1104 1
2012-10-10 01:34:04 92747 P1104 1
2012-10-10 12:49:22 102773 P1104 1
2012-10-10 14:19:03 102773 P1104 4
2012-10-15 16:27:24 149693 P1104 1
触发器应将 dbo.exported 上的最后一个 idTest 与插入的数据进行比较,并将不重复的数据添加到表 dbo.exported。但它添加了所有:
Repair_Date idTest 位置错误 2012-10-10 00:03:25 91996 IC4303 4 2012-10-10 00:00:58 91996 C524 1 2012-10-10 00:00:56 91996 C522 1 2012-10-10 00:00:54 91996 C537 1 2012-10-10 04:34:31 95694 P1104 1 2012-10-10 06:48:33 97405 P1104 1 2012-10-10 01:31:17 93088 P1104 1 2012-10-10 01:34:04 92747 P1104 1 2012-10-10 12:49:22 102773 P1104 1 2012-10-10 14:19:03 102773 P1104 4 2012-10-15 16:27:24 149693 P1104 1