創建一個觸發器,它會創建一個新表Emp_audit並添加新的元組到它時,任何變化表僱員製作
create trigger my_trigger on Employees
AFTER INSERT, UPDATE, DELETE
AS
DECLARE @What varchar(30);
DECLARE @Who varchar(30);
DECLARE @for int;
DECLARE @At time;
DECLARE @COUNTI int;
DECLARE @COUNTD int;
select @COUNTI = COUNT(*) from inserted;
select @COUNTD = COUNT(*) from deleted;
set @Who = SYSTEM_USER;
set @At = CURRENT_TIMESTAMP;
if(@COUNTD = 0 and @COUNTI = 1)
begin
set @What = 'insert';
select @for = EmployeeID from inserted i;
end
else
begin
if(@COUNTD = 1 and @COUNTI = 0)
begin
set @What = 'delete';
select @for = EmployeeID from deleted i;
end
else
begin
set @What = 'update';
select @for = EmployeeID from inserted i;
end
end
INSERT INTO EMP_Audit Values (@What, @Who, @for, @At);
您的查詢並不一定表示上次表進行了更新。您需要使用第三方日誌讀取器工具(和事務日誌)來確定歷史數據。這是一個持續的需求嗎? –
哎馬丁·我想我們應該能夠發現裏面SQL這樣的信息withoud由於SQL提供了每個數據的基礎上 –
日誌文件使用的是第三方,但它不記錄格式。你可以使用'sys.fn_dblog'來玩玩,但第三方工具供應商已經花費了很多工時來做這件事。如果這是一個持續的需求,你還沒有回答? –