数据库结构:
CREATE TABLE [dbo].[cg_tz_log] ( [logid] int NOT NULL IDENTITY(1,1) , operate varchar(10), -- 操作类型 如Insert,Update,Delete. id int, -- 原表ID(主键) [cg_date_o] date NULL , [cg_date_n] date NULL , [cg_id_o] varchar(255) NULL , [cg_id_n] varchar(255) NULL , [cg_sname_o] varchar(255) NULL , [cg_sname_n] varchar(255) NULL , [cg_cgpname_o] varchar(255) NULL , [cg_cgpname_n] varchar(255) NULL , [cg_ggxh_o] varchar(255) NULL , [cg_ggxh_n] varchar(255) NULL , [cg_pp_o] varchar(255) NULL , [cg_pp_n] varchar(255) NULL , [cg_num_o] int NULL , [cg_num_n] int NULL , [cg_dw_o] varchar(255) NULL , [cg_dw_n] varchar(255) NULL , [cg_price_o] money NULL , [cg_price_n] money NULL , [cg_priceall_o] money NULL , [cg_priceall_n] money NULL , [cg_htprice_o] money NULL , [cg_htprice_n] money NULL , [cg_htcbprice_o] money NULL , [cg_htcbprice_n] money NULL , [cg_xsht_o] varchar(255) NULL , [cg_xsht_n] varchar(255) NULL , [cg_xspname_o] varchar(255) NULL , [cg_xspname_n] varchar(255) NULL , [cg_lb_o] varchar(255) NULL , [cg_lb_n] varchar(255) NULL , [cg_f_date_o] date NULL , [cg_f_date_n] date NULL , [cg_f_percent_o] decimal(38,10) NULL , [cg_f_percent_n] decimal(38,10) NULL , [cg_f_price_o] money NULL , [cg_f_price_n] money NULL , [cg_nf_price_o] money NULL , [cg_nf_price_n] money NULL , [cg_p_id_o] varchar(255) NULL , [cg_p_id_n] varchar(255) NULL , [cg_p_price_o] money NULL , [cg_p_price_n] money NULL , [cg_dhqk_o] varchar(255) NULL , [cg_dhqk_n] varchar(255) NULL , [cg_sphone_o] varchar(255) NULL , [cg_sphone_n] varchar(255) NULL , [cg_sfax_o] varchar(255) NULL , [cg_sfax_n] varchar(255) NULL , [cg_slxr_o] varchar(255) NULL , [cg_slxr_n] varchar(255) NULL , [cg_dh_status_o] int NULL DEFAULT ((0)) , [cg_dh_status_n] int NULL DEFAULT ((0)) , [cg_dh_date_o] date NULL DEFAULT (‘2017-9-27‘) , [cg_dh_date_n] date NULL DEFAULT (‘2017-9-27‘) , [cg_dhzq_o] int NULL DEFAULT ((30)) , [cg_dhzq_n] int NULL DEFAULT ((30)) , [cg_kfid_o] varchar(255) NULL DEFAULT ‘‘, [cg_kfid_n] varchar(255) NULL DEFAULT ‘‘, spid int not null, -- spid login_name varchar(100), -- 登录名 prog_name varchar(100), -- 程序名 hostname varchar(100), -- 主机名 ipaddress varchar(100), -- IP地址 runsql varchar(4000), -- 执行的TSQL代码 UDate datetime -- 操作日期时间 ) GO -- ---------------------------- ALTER TABLE [dbo].[cg_tz_log] ADD PRIMARY KEY ([id]) GO
触发器:
-- 建跟踪触发器 create trigger tr_cg_tz_log on cg_tz after update,insert,delete as begin declare @di table(et varchar(200),pt varchar(200),ei varchar(max)) insert into @di exec(‘dbcc inputbuffer(@@spid)‘) declare @op varchar(10) select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted) then ‘Update‘ when exists(select 1 from inserted) and not exists(select 1 from deleted) then ‘Insert‘ when not exists(select 1 from inserted) and exists(select 1 from deleted) then ‘Delete‘ end if @op in(‘Update‘,‘Insert‘) begin insert into cg_tz_log (operate,id, cg_date_o,cg_date_n,cg_id_o,cg_id_n,cg_sname_o,cg_sname_n,cg_cgpname_o,cg_cgpname_n,cg_ggxh_o,cg_ggxh_n, cg_pp_o,cg_pp_n,cg_num_o,cg_num_n,cg_dw_o,cg_dw_n,cg_price_o,cg_price_n,cg_priceall_o,cg_priceall_n, cg_htprice_o,cg_htprice_n,cg_htcbprice_o,cg_htcbprice_n,cg_xsht_o,cg_xsht_n,cg_xspname_o,cg_xspname_n,cg_lb_o,cg_lb_n, cg_f_date_o,cg_f_date_n,cg_f_percent_o,cg_f_percent_n,cg_f_price_o,cg_f_price_n,cg_nf_price_o,cg_nf_price_n,cg_p_id_o,cg_p_id_n, cg_p_price_o,cg_p_price_n,cg_dhqk_o,cg_dhqk_n,cg_sphone_o,cg_sphone_n,cg_sfax_o,cg_sfax_n,cg_slxr_o,cg_slxr_n, cg_dh_status_o,cg_dh_status_n,cg_dh_date_o,cg_dh_date_n,cg_dhzq_o,cg_dhzq_n,cg_kfid_o,cg_kfid_n, spid,login_name,prog_name,hostname,ipaddress,runsql,UDate) select @op,n.id, o.cg_date,n.cg_date,o.cg_id,n.cg_id,o.cg_sname,n.cg_sname,o.cg_cgpname,n.cg_cgpname,o.cg_ggxh,n.cg_ggxh, o.cg_pp,n.cg_pp,o.cg_num,n.cg_num,o.cg_dw,n.cg_dw,o.cg_price,n.cg_price,o.cg_priceall,n.cg_priceall, o.cg_htprice,n.cg_htprice,o.cg_htcbprice,n.cg_htcbprice,o.cg_xsht,n.cg_xsht,o.cg_xspname,n.cg_xspname,o.cg_lb,n.cg_lb, o.cg_f_date,n.cg_f_date,o.cg_f_percent,n.cg_f_percent,o.cg_f_price,n.cg_f_price,o.cg_nf_price,n.cg_nf_price,o.cg_p_id,n.cg_p_id, o.cg_p_price,n.cg_p_price,o.cg_dhqk,n.cg_dhqk,o.cg_sphone,n.cg_sphone,o.cg_sfax,n.cg_sfax,o.cg_slxr,n.cg_slxr, o.cg_dh_status,n.cg_dh_status,o.cg_dh_date,n.cg_dh_date,o.cg_dhzq,n.cg_dhzq,o.cg_kfid,n.cg_kfid, @@spid, (select login_name from sys.dm_exec_sessions where session_id=@@spid), (select program_name from sys.dm_exec_sessions where session_id=@@spid), (select hostname from sys.sysprocesses where spid=@@spid), (select client_net_address from sys.dm_exec_connections where session_id=@@spid), (select top 1 isnull(ei,‘‘) from @di), getdate() from inserted n left join deleted o on o.id=n.id end else begin insert into cg_tz_log (operate,id, cg_date_o,cg_date_n,cg_id_o,cg_id_n,cg_sname_o,cg_sname_n,cg_cgpname_o,cg_cgpname_n,cg_ggxh_o,cg_ggxh_n, cg_pp_o,cg_pp_n,cg_num_o,cg_num_n,cg_dw_o,cg_dw_n,cg_price_o,cg_price_n,cg_priceall_o,cg_priceall_n, cg_htprice_o,cg_htprice_n,cg_htcbprice_o,cg_htcbprice_n,cg_xsht_o,cg_xsht_n,cg_xspname_o,cg_xspname_n,cg_lb_o,cg_lb_n, cg_f_date_o,cg_f_date_n,cg_f_percent_o,cg_f_percent_n,cg_f_price_o,cg_f_price_n,cg_nf_price_o,cg_nf_price_n,cg_p_id_o,cg_p_id_n, cg_p_price_o,cg_p_price_n,cg_dhqk_o,cg_dhqk_n,cg_sphone_o,cg_sphone_n,cg_sfax_o,cg_sfax_n,cg_slxr_o,cg_slxr_n, cg_dh_status_o,cg_dh_status_n,cg_dh_date_o,cg_dh_date_n,cg_dhzq_o,cg_dhzq_n,cg_kfid_o,cg_kfid_n, spid,login_name,prog_name,hostname,ipaddress,runsql,UDate) select @op,o.id, o.cg_date,null,o.cg_id,null,o.cg_sname,null,o.cg_cgpname,null,o.cg_ggxh,null, o.cg_pp,null,o.cg_num,null,o.cg_dw,null,o.cg_price,null,o.cg_priceall,null, o.cg_htprice,null,o.cg_htcbprice,null,o.cg_xsht,null,o.cg_xspname,null,o.cg_lb,null, o.cg_f_date,null,o.cg_f_percent,null,o.cg_f_price,null,o.cg_nf_price,null,o.cg_p_id,null, o.cg_p_price,null,o.cg_dhqk,null,o.cg_sphone,null,o.cg_sfax,null,o.cg_slxr,null, o.cg_dh_status,null,o.cg_dh_date,null,o.cg_dhzq,null,o.cg_kfid,null, @@spid, (select login_name from sys.dm_exec_sessions where session_id=@@spid), (select program_name from sys.dm_exec_sessions where session_id=@@spid), (select hostname from sys.sysprocesses where spid=@@spid), (select client_net_address from sys.dm_exec_connections where session_id=@@spid), (select top 1 isnull(ei,‘‘) from @di), getdate() from deleted o end end go
转载原地址:http://blog.csdn.net/jc_benben/article/details/79218864