如何运行触发器分隔表

时间:2013-07-16 05:08:22

标签: sql-server-2008 tsql triggers

我有一个触发器,如下所示:

ALTER TRIGGER [dbo].[areftblSalesDoc] ON [dbo].[tblSalesDoc]
AFTER INSERT
AS
Begin Try
    INSERT INTO ArefSms
        (DSIDName, SalesID, CustName, CustCellPhone, Fish, qt, ProdName)
    SELECT
        td.DSName, i.SalesID, tc.CustName, tc.CustCellPhone, i.Fish, NULL, NULL
        FROM inserted i
        left join tblDistributionStationDesc td on i.DSID = td.DSID
        left join tblCustomerDesc tc on i.CustomerID = tc.CustID

End Try
Begin Catch

End catch

当触发器无法保存任何内容且错误tblsalesdoc无法保存时。但我不希望这样。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果您想在发生错误时跳过触发器代码,可以尝试使用SAVE TRANSACTION声明

ALTER TRIGGER [dbo].[areftblSalesDoc] ON [dbo].[tblSalesDoc]
AFTER INSERT
AS

SAVE TRANSACTION BeforeInsertInTrigger;

Begin Try
    INSERT INTO ArefSms
        (DSIDName, SalesID, CustName, CustCellPhone, Fish, qt, ProdName)
    SELECT
        td.DSName, i.SalesID, tc.CustName, tc.CustCellPhone, i.Fish, NULL, NULL
        FROM inserted i
        left join tblDistributionStationDesc td on i.DSID = td.DSID
        left join tblCustomerDesc tc on i.CustomerID = tc.CustID

End Try
Begin Catch
   ROLLBACK TRANSACTION BeforeInsertInTrigger;
   RETURN
End catch