插入触发器在出错时终止

时间:2014-09-17 07:21:35

标签: sql-server-2008

我有一个After Insert触发器,它应该首先插入,然后插入另一个表。但是当在另一个表中插入数据时发生任何错误时,所有事务都会终止。我想在基表中插入第一个数据,无论触发器是否成功触发。

这是我的代码:

CREATE TRIGGER [dbo].[Insert_Teacher_Active_LC]
   ON  [dbo].[Teacher_Profile]
   AFTER INSERT,UPDATE
AS 
BEGIN
    MERGE [ROSC].[dbo].[Active_LC] as d
    USING (SELECT 
              DistrictID, upazilaID, LCID, LCVisitYr, Trimister,
              CASE WHEN (TcrPres = 1 AND TcrMtchLCProf = 1) 
                   THEN 1 ELSE 0 
              END AS TcrRpls
           FROM INSERTED) AS s ON s.DistrictID = d.DistrictID 
                                AND s.upazilaID = d.upazilaID 
                                AND s.LCID = d.LCID 
                                AND s.LCVisitYr = d.EduYr

    WHEN MATCHED THEN
       UPDATE 
         SET Trimister = s.Trimister, Tcr_Replace = s.TcrRpls

    WHEN NOT MATCHED THEN
        INSERT(DistrictID, UpazilaID, LCID, EduYr, Trimister, Tcr_Replace)
        VALUES(DistrictID, UpazilaID, LCID, LCVisitYr, Trimister, TcrRpls);

    -- Insert statements for trigger here
END

0 个答案:

没有答案