1. 程式人生 > >sql 觸發器記錄修改的欄位和值

sql 觸發器記錄修改的欄位和值

CREATE TRIGGER dbo.tri_Export_Subsidiary_update    ON  dbo.Export_Subsidiary    AFTER UPDATE    AS  BEGIN     -- SET NOCOUNT ON added to prevent extra result sets from     -- interfering with SELECT statements.     SET NOCOUNT ON;     declare @sql nvarchar(max)=''           select @[email protected]

+REPLACE('+case when a.%s=b.%s or (a.%s is null and b.%s is null) then '''' else '',[%s]:[''+isnull(cast(a.%s as nvarchar(max)),''NULL'')+'']->[''+isnull(cast(b.%s as nvarchar(max)),''NULL'')+'']'' end','%s',name)     from sys.syscolumns      where id=OBJECT_ID('Export_Subsidiary')              set @sql=stuff(@sql,1,1,'')              set @sql='insert financial_records_log(financialLog,datetime,operators)     SELECT ''Export_Subsidiary:''+cast(a.Id as varchar)+a.diffdata,convert(varchar(50),getdate(),121),''systemlog'' FROM(      select A.ID,'
[email protected]
+' as diffdata     from deleted a join inserted b on a.id=b.id     ) a where diffdata >''''     '              exec( @sql)   

END