Wednesday, April 5, 2017

SQL Trigger Example For Update and Delete

USE [ONE_TAX]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('[FIN_TAX].[OPL_OPINION_LOG_H_TRG]','TR') IS NOT NULL
DROP TRIGGER [FIN_TAX].[OPL_OPINION_LOG_H_TRG]
GO

CREATE TRIGGER [FIN_TAX].[OPL_OPINION_LOG_H_TRG]
ON [FIN_TAX].[OPL_OPINION_LOG]
AFTER UPDATE, DELETE
AS
BEGIN
INSERT INTO FIN_TAX.OPL_OPINION_LOG_H
(
OPL_SK,
DOCUMENT_ID,
TPL_PROJ_ID,
OPL_FINAL_VERSION_FLG_ID,
OPL_FIN48_FLG_ID,
OPL_INT_EXT_FLG_ID,
OPL_OPINION_DT,
OPL_FIRM_CONTACT_NAME,
OPL_AIG_CONTACT_ID,
OPL_ENTITY_REQUEST_ID,
OPL_ISSUE_DESC,
OPL_EXT_SHARE_FLG_ID,
OPL_COMPLETE_FLG_ID,
OPL_REVIEWER_NAME,
OPL_REVIEW_DT,
OPL_SUBMIT_DT,
OPL_UPLOAD_USERID,
OPL_FOREIGN_TEXT,
OPL_UPD_USERID,
OPL_UPD_TS,
OPL_ASSOC_TAX_PL_FLG_ID,
OPL_ISSUE_DISCUSS_FLG_ID,
OPL_DATE_VALID_FLG_ID,
OPL_SIGNIF_CHG_FLG_ID,
OPL_TAX_TREAT_TRANS_FLG_ID,
OPL_PRESENT_COMM_FLG_ID,
OPL_LITIGATE_FLG_ID,
OPL_FIRM_ISSED_OPINION_TEXT,
OPL_APPROVAL_STATUS_ID,
OPL_DOC_ACCESS_FLG_ID,
OPL_LEGAL_OPINION_FLG_ID,
OPL_AUTH_CONTRARY_FLG_ID,
HIST_ACTION_TYPE
)
SELECT
*,CASE WHEN EXISTS (SELECT * FROM inserted)THEN 'U' ELSE 'D' END
FROM deleted;
END;

GO


No comments:

Post a Comment