SQL中的更新触发器需要清理

时间:2015-11-11 20:46:58

标签: sql sql-server

这是我正在处理的网站上某些更新触发器的格式。实际上,Insert和Delete使用与下面类似的格式(重复性)。这似乎是很多重复。有没有办法我可以清理它而不必在每个字段上单独插入到我的Audit表中(如下所示)?

USE [DatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[update_iep_serv] 
ON  [dbo].[iep_serv] 
FOR UPDATE
AS 
BEGIN
SET NOCOUNT ON;

DECLARE @iepservpk [int]
DECLARE @iepfk [int]
DECLARE @arcfk[int]
DECLARE @start [date]
DECLARE @term [date] 
DECLARE @thr_code [char] (3)
DECLARE @thr_type [char] (3)
DECLARE @cotreat[char] (2)
DECLARE @frequency [smallint]
DECLARE @duration [numeric] (4,2)
DECLARE @period [char] (2)
DECLARE @therapist [int] 
DECLARE @empfk [int]
DECLARE @tablelock [varchar] (50)

DECLARE @iepservpk_n [int]
DECLARE @iepfk_n [int]
DECLARE @arcfk_n [int]
DECLARE @start_n [date]
DECLARE @term_n [date] 
DECLARE @thr_code_n [char] (3)
DECLARE @thr_type_n [char] (3)
DECLARE @cotreat_n[char] (2)
DECLARE @frequency_n [smallint]
DECLARE @duration_n [numeric] (4,2)
DECLARE @period_n [char] (2)
DECLARE @therapist_n [int] 
DECLARE @empfk_n [int]
DECLARE @tablelock_n [varchar] (50)

 select 
 @iepservpk=iepservpk,
 @iepfk = iepfk, 
 @arcfk = arcfk,
 @start = start,
 @term=term,
 @thr_code=thr_code,
 @thr_type =thr_type,
 @cotreat=cotreat,
 @frequency=frequency, 
 @duration=duration,
 @period = period,
 @therapist = therapist,
 @empfk = empfk,
 @tablelock = tablelock
 from deleted

 select 
 @iepservpk_n=iepservpk,
 @iepfk_n = iepfk, 
 @arcfk_n = arcfk,
 @start_n = start,
 @term_n=term,
 @thr_code_n=thr_code,
 @thr_type_n =thr_type,
 @cotreat_n=cotreat,
 @frequency_n=frequency, 
 @duration_n=duration,
 @period_n = period,
 @therapist_n = therapist,
 @empfk_n = empfk,
 @tablelock_n= tablelock
 from inserted 


-- Insert statements for trigger here

IF @iepservpk != @iepservpk_n OR (@iepservpk is null AND @iepservpk_n is not null) OR (@iepservpk is not null AND @iepservpk_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'iepservpk',@iepservpk,@iepservpk_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END


IF @iepfk != @iepfk_n OR (@iepfk is null AND @iepfk_n is not null) OR (@iepfk is not null AND @iepfk_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'iepfk',@iepfk,@iepfk_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @arcfk != @arcfk_n OR (@arcfk is null AND @arcfk_n is not null) OR (@arcfk is not null AND @arcfk_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'arcfk',@arcfk,@arcfk_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @start != @start_n OR (@start is null AND @start_n is not null) OR (@start is not null AND @start_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'start',@start,@start_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @term != @term_n OR (@term is null AND @term_n is not null) OR (@term is not null AND @term_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'term',@term,@term_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @thr_code != @thr_code_n OR (@thr_code is null AND @thr_code_n is not null) OR (@thr_code is not null AND @thr_code_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'thr_code',@thr_code,@thr_code_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @thr_type != @thr_type_n OR (@thr_type is null AND @thr_type_n is not null) OR (@thr_type is not null AND @thr_type_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'thr_type',@thr_type,@thr_type_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @cotreat != @cotreat_n OR (@thr_type is null AND @cotreat_n is not null) OR (@cotreat is not null AND @cotreat_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'cotreat',@cotreat,@cotreat_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @frequency != @frequency_n OR (@frequency is null AND @frequency_n is not null) OR (@frequency is not null AND @frequency_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'frequency',@frequency,@frequency_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @duration != @duration_n OR (@duration is null AND @duration_n is not null) OR (@duration is not null AND @duration_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'duration',@duration,@duration_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @period != @period_n OR (@period is null AND @period_n is not null) OR (@period is not null AND @period_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'period',@period,@period_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @therapist != @therapist_n OR (@therapist is null AND @therapist_n is not null) OR (@therapist is not null AND @therapist_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'therapist',@therapist,@therapist_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @empfk != @empfk_n OR (@empfk is null AND @empfk_n is not null) OR (@empfk is not null AND @empfk_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'empfk',@empfk,@empfk_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END

IF @tablelock != @tablelock_n OR (@tablelock is null AND @tablelock_n is not null) OR (@tablelock is not null AND @tablelock_n is null)
BEGIN
    INSERT INTO [Audit].[dbo].[AUDITACT]
       ([databaseName]
       ,[tableName]
       ,[tablefk]
       ,[fieldname]
       ,[old]
       ,[new]
       ,[userfk]
       ,[username]
       ,[action]
       ,[entrytime])
    VALUES
        ('Support','IEP_SERV',@iepservpk,'tablelock',@tablelock,@tablelock_n,@empfk,@tablelock_n,'UPDATE',GETDATE())
END
END

1 个答案:

答案 0 :(得分:1)

嗯,这并不像我想象的那么简单,因为如果多个条件为真,你需要做多次插入。所以你仍然需要多个插入(而不是CASE语句),但你可以使用JOIN而不是变量和IF块进行插入:

--This inserts all the rows where ColumnNameA was updated
INSERT INTO AuditTable (Col1, Col2, FieldNameCol)
SELECT Col1, Col2, 'ColumnNameA'
FROM Inserted i
INNER JOIN Deleted d
  ON i.ColumnNameA<>d.ColumnNameA

--Then repeat the above for each other ColumnName you want to check