仅在实际更新数据时才存储信息的更新触发器?

时间:2018-10-30 22:57:43

标签: sql sql-server sql-server-2008 sql-server-2012 mssql-jdbc

CREATE TRIGGER AuditTrigger2 
ON authors
AFTER update
AS
    INSERT INTO audit (trackingUser, date_time)
    VALUES (SYSTEM_USER, getdate())
GO

我需要向其更新触发器添加什么,以排除实际上未更新任何内容的更新尝试?

我的审计表也有一个用于交易类型的列,但是我不确定如何从触发器中获取交易类型并将其插入到该列中。

1 个答案:

答案 0 :(得分:1)

str(without_response[impute_here,])

'data.frame':   16 obs. of  10 variables:
$ V1 : int  1057013 1096800 1183246 1184840 1193683 1197510
1241232 169356 432809 563649 ...
$ V2 : int  8 6 1 1 1 5 3 3 3 8 ...
$ V3 : int  4 6 1 1 1 1 1 1 1 8 ...
$ V4 : int  5 6 1 3 2 1 4 1 3 8 ...
$ V5 : int  1 9 1 1 1 1 1 1 1 1 ...
$ V6 : int  2 6 1 2 3 2 2 2 2 2 ...
$ V7 : chr  NA NA NA NA ...
$ V8 : int  7 7 2 2 1 3 3 3 2 6 ...
$ V9 : int  3 8 1 1 1 1 1 1 1 10 ...
$ V10: int  1 1 1 1 1 1 1 1 1 1 ...

summary(without_response[impute_here,])

      V1                V2              V3              V4       
Min.   :  61634   Min.   :1.000   Min.   :1.000   Min.   :1.000  
1st Qu.: 595517   1st Qu.:1.000   1st Qu.:1.000   1st Qu.:1.000  
Median :1057040   Median :3.000   Median :1.000   Median :2.500  
Mean   : 857578   Mean   :3.375   Mean   :2.438   Mean   :2.875  
3rd Qu.:1187051   3rd Qu.:5.000   3rd Qu.:4.000   3rd Qu.:4.250  
Max.   :1241232   Max.   :8.000   Max.   :8.000   Max.   :8.000  
       V5              V6             V7                  V8       
Min.   :1.000   Min.   :1.000   Length:16          Min.   :1.000  
1st Qu.:1.000   1st Qu.:2.000   Class :character   1st Qu.:2.000  
Median :1.000   Median :2.000   Mode  :character   Median :2.500  
Mean   :1.812   Mean   :2.438                      Mean   :3.125  
3rd Qu.:1.000   3rd Qu.:2.000                      3rd Qu.:3.250  
Max.   :9.000   Max.   :7.000                      Max.   :7.000  
      V9             V10   
Min.   : 1.00   Min.   :1  
1st Qu.: 1.00   1st Qu.:1  
Median : 1.00   Median :1  
Mean   : 2.75   Mean   :1  
3rd Qu.: 3.00   3rd Qu.:1  
Max.   :10.00   Max.   :1 

is.na(without_response[impute_here,])

      V1     V2      V3      V4      V5      V6      V7      V8      V9   V10
24  FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE  FALSE
41  FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
140 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
146 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
159 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
165 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
236 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
250 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
276 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
293 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
295 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
298 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
316 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
322 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
412 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE
618 FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   TRUE    FALSE    FALSE   FALSE

编辑:根据您的评论,您可能需要:

INSERT INTO audit (trackingUser, date_time)
  select SYSTEM_USER, getdate()
  from Inserted I
  inner join Deleted D on D.id = I.id /* Use your PK condition */
  where <some condition that compares fields in I & D>