SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[mysqltrig]
ON [dbo].[mytable]
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT OFF;
SAVE TRANSACTION appInsert;
BEGIN TRY
INSERT INTO [dbo].[mynewtable] ( [myfield1],[myfield2] )
SELECTi.fieldA, i.fieldB
FROM inserted i;
END TRY
BEGIN CATCH
DECLARE @myErrorNumber int, @myErrorSeverity int, @myErrorState int, @myErrorLine int,
@myErrorProcedure varchar(4000), @myErrorMessage varchar(4000)
SELECT
@myErrorNumber = ERROR_NUMBER(),
@myErrorSeverity = ERROR_SEVERITY(),
@myErrorState = ERROR_STATE(),
@myErrorProcedure = ERROR_PROCEDURE(),
@myErrorLine = ERROR_LINE(),
@myErrorMessage = ERROR_MESSAGE();
ROLLBACK TRANSACTION appInsert;
INSERT INTO myerrortable ( myerrcode, myerrmsg, myerrdt )
SELECT CAST(@myErrorNumber as VARCHAR),
CAST(@myErrorMessage as VARCHAR),
SYSDATETIME()
END CATCH
END