更新上次修改日期列

时间:2016-07-12 12:19:10

标签: triggers sql-server-2012 constraints last-modified modified-date

我有一张这样的表:

id  name    modified
11  John    2016-07-12 15:49:45
22  Abraham 2016-07-12 15:52:03

我需要更新修改过的'跟踪行的上次修改日期的列。我使用触发器完成了这个操作,但是已经读过触发器会消耗性能。有没有办法使用约束来做到这一点?

1 个答案:

答案 0 :(得分:0)

可以在UPDATE子句中使用DEFAULT约束和DEFAULT关键字。请参阅以下示例:

 Sub CompareAndDelete()
'This code will compare the rows of each sheet and delete any old alerts that have already been emailed out
'   it will then call SaveFile IF new alerts have been found

Dim row1() As Variant, row2() As Variant, newRow As Variant
Dim coll As Collection
Dim i As Long
Dim maxCol1 As Integer
Dim maxCol2 As Integer

'Find max number of columns for old and new alert
With ActiveSheet
    maxCol1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
    maxCol2 = .Cells(2, .Columns.Count).End(xlToLeft).Column
End With

'Redimensionalize arrays
ReDim row1(0 To (maxCol1 - 1))
ReDim row2(0 To (maxCol2 - 1))

'Assign row1/row2 string values into arrays
For r = 0 To (maxCol1 - 1)
   row1(r) = Cells(1, r + 1).Value
Next

For s = 0 To (maxCol2 - 1)
    row2(s) = Cells(2, s + 1).Value
Next

ReDim newRow(LBound(row1) To Abs(UBound(row2) - UBound(row1)) - 1)

'Create a collection to load all row1/row2 values into
Set coll = New Collection

'Empty Collection for each run through
Set coll = Nothing

'Set collection to New before using
Set coll = New Collection



For i = LBound(row1) To (UBound(row1))
    coll.Add row1(i), row1(i)
Next i

For i = LBound(row2) To (UBound(row2))
    On Error Resume Next
    coll.Add row2(i), row2(i)
    If Err.Number <> 0 Then
        coll.Remove row2(i)
    End If
    On Error GoTo 0
Next i

'Copy Row 2 and Paste it to Row 1

ActiveWorkbook.ActiveSheet.Rows(2).Copy
Range("A1").Select
ActiveSheet.Paste

'Now values are stored in collection, delete row 2
'Rows(2).EntireRow.ClearContents

'Paste only the new alerts onto a new worksheet that is designated for new   alerts
For i = LBound(newRow) To UBound(newRow)
    newRow(i) = coll(i + 1) 'Collections are 1-based
    'Debug.Print newRow(i)
    ActiveWorkbook.Sheets("Sheet" & index + 4).Select
    ActiveWorkbook.Sheets("Sheet" & index + 4).Cells(1, i + 1).Value =   newRow(i)

Next i


'if NEW alerts have been found, call SaveFile
If IsEmpty(ActiveWorkbook.Sheets("Sheet" & index + 4).Cells(1, 1)) = False     Then
         Call SaveFile
End If

End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~