我有大约25个表,每个表都有3个触发器(插入,更新,删除)来生成用户操作的日志。每次在UI中进行编辑时,所有这些表都会更新,尽管它们的值在UI中没有更改。
在这种情况下,我无法检查列是否更新,我需要检查以前更新的值是否与新更新的值不同,然后我可以插入到我的日志表中。此外,我需要将所有更新的表值作为以逗号分隔的字符串插入日志表。
有没有有效的方法来处理这个问题?
ALTER TRIGGER [dbo].[Activity_Update]
ON [dbo].[Activity]
AFTER UPDATE
AS
BEGIN
DECLARE @Names VARCHAR(8000)
SET @result = ' '
DECLARE @id INT;
SELECT @id = i.ID FROM inserted i;
IF UPDATE(deptNotes)
BEGIN
DECLARE @OldValue NVARCHAR(MAX)
SELECT TOP 1 @OldValue = deptNotes
FROM Activity
WHERE id = @id
ORDER BY Timestamp DESC
IF (@OldValue != (SELECT i.deptNotes FROM inserted i))
BEGIN
IF ((SELECT i.deptNotes FROM inserted i) != ' ')
SELECT @result = @result + ',' + 'Modified dept. Notes'
ELSE
SELECT @result = @result + ',' + 'Removed dept. Notes'
END
END
IF UPDATE(deptActivityID)
BEGIN
DECLARE @OldValue1 NVARCHAR(MAX)
SELECT TOP 1 @OldValue1 = deptActivityID
FROM Activity
WHERE id = @id
ORDER BY Timestamp DESC
IF (@OldValue1 != (SELECT i.deptActivityID FROM inserted i))
BEGIN
SELECT @result = @result + ',' + 'Changed dept. Activity '
END
END
IF UPDATE(SubmissionDate)
BEGIN
declare @OldValue2 nvarchar(max)
select Top 1 @OldValue2 = submissiondate from [Activity] where id=@id Order by Timestamp DESC
If (@OldValue2 != (select i.submissiondate from inserted i))
BEGIN
Select @result = @result + ',' + 'Changed application date - ' + '"' + (select cast(i.submissiondate as nvarchar(500)) from inserted i)+ '"'
END
END
INSERT [Activity]
(
[deptActivityID],
[deptNotes],
[SubmissionDate],
[Username],
[Operation],
[Comment]
)
SELECT
v.deptActivityID,
v.deptNotes,
v.SubmissionDate,
v.[LastEditBy],
'update',
@result
FROM inserted v
END
GO
答案 0 :(得分:4)
您可以使用num = ""
while not isinstance(num, int):
num = input("Please, enter a number\n>>> ")
try:
num = int(num)
except ValueError:
pass
# proceed with your code...
表来查找旧值的内容以及新值的deleted
。另请注意,INSERT,UPDATE和DELETE可能会影响多行,因此您应该触发它。
下面的触发器代码将为您提供deptNotes中任何更改的行列表。
inserted
如果您的deptNotes可能包含NULL值,则需要使用ISNULL()来处理