實例效果: 實現表數據的增修刪時,記錄日誌。 1.“複製”現有表, 創建相應的_Log表; (註意點: 2.對現有表,創建Insert,Update,Delete的觸發器, 並將相應數據 記錄到對應的_Log表 相應代碼如下: ...
實例效果:
實現表數據的增修刪時,記錄日誌。
1.“複製”現有表,
創建相應的_Log表;
(註意點:
通過select union all 的方式,避免了IDENTITY 的“複製”,
即如果原表有 PK 如 ID Identity,_Log表 僅“複製”ID int,“不複製” Identity屬性,
以便 Insert Update Delete時,可以Insert到Log表。)
2.對現有表,創建Insert,Update,Delete的觸發器,
並將相應數據 記錄到對應的_Log表
相應代碼如下:
BEGIN TRAN BEGIN TRY --定義TAB_CURSOR DECLARE TAB_CURSOR CURSOR read_only FOR SELECT name FROM SysObjects Where XType='U' -- AND name = N'T01ConstItem' and [name] <> N'dtproperties' ORDER BY Name; --打開 OPEN TAB_CURSOR DECLARE @P_TabName NVARCHAR(200); DECLARE @P_TabName_Log NVARCHAR(200); DECLARE @P_Create_Log_Tab NVARCHAR(4000); DECLARE @P_Create_Trig_I NVARCHAR(4000); DECLARE @P_Create_Trig_U NVARCHAR(4000); DECLARE @P_Create_Trig_D NVARCHAR(4000); FETCH NEXT FROM TAB_CURSOR INTO @P_TabName --迴圈 WHILE (@@FETCH_STATUS <> -1) BEGIN IF (@@FETCH_STATUS <> -2) BEGIN SET @P_TabName_Log = CONCAT(@P_TabName,N'_Log'); SET @P_Create_Log_Tab = N' SELECT * '; SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' ,N''I '' AS Action'); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' ,Getdate() AS ActionDate '); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' INTO '); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,@P_TabName_Log ); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' FROM ' ); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,@P_TabName); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' UNION ALL '); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' SELECT TOP (1) * '); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' ,N''I '' AS Action'); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' ,Getdate() AS ActionDate '); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,N' FROM ' ); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab,@P_TabName); SET @P_Create_Log_Tab = CONCAT(@P_Create_Log_Tab, N' WHERE 1=0 ; '); EXEC( @P_Create_Log_Tab); --SET @P_Create_Log_Tab = CONCAT(N' SET IDENTITY_INSERT ',@P_TabName_Log ,' ON '); --EXEC( @P_Create_Log_Tab); SET @P_Create_Trig_I = N' create trigger '; SET @P_Create_Trig_I = CONCAT(@P_Create_Trig_I,N' trig_',@P_TabName,N'_I '); SET @P_Create_Trig_I = CONCAT(@P_Create_Trig_I,N' on ',@P_TabName,N' after INSERT as '); SET @P_Create_Trig_I = CONCAT(@P_Create_Trig_I, N' begin '); SET @P_Create_Trig_I = CONCAT(@P_Create_Trig_I, N'insert into ',@P_TabName_Log ); SET @P_Create_Trig_I = CONCAT(@P_Create_Trig_I,N' select * , N''I'',Getdate() from Inserted ; ' ); SET @P_Create_Trig_I = CONCAT(@P_Create_Trig_I, N'end '); --select @P_Create_Trig_I; EXEC( @P_Create_Trig_I); SET @P_Create_Trig_U = N' create trigger '; SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' trig_',@P_TabName,N'_U '); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' on ',@P_TabName,N' after UPDATE as '); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N' begin '); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N'insert into ',@P_TabName_Log ); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' select * , N''UD'',Getdate() from Deleted ; ' ); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N'insert into ',@P_TabName_Log ); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' select * , N''UI'',Getdate() from Inserted ; ' ); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N'end '); EXEC( @P_Create_Trig_U); SET @P_Create_Trig_U = N' create trigger '; SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' trig_',@P_TabName,N'_D '); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' on ',@P_TabName,N' after DELETE as '); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N' begin '); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N'insert into ',@P_TabName_Log ); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U,N' select * , N''D'',Getdate() from Deleted ; ' ); SET @P_Create_Trig_U = CONCAT(@P_Create_Trig_U, N'end '); EXEC( @P_Create_Trig_U); END FETCH NEXT FROM TAB_CURSOR INTO @P_TabName END --關閉 CLOSE TAB_CURSOR --釋放 DEALLOCATE TAB_CURSOR COMMIT TRAN; END TRY BEGIN CATCH SELECT ERROR_MESSAGE() AS ErrorMessage ,ERROR_SEVERITY() AS ErrorSeverity ,ERROR_STATE() AS ErrorState ROLLBACK TRAN; END CATCH