mysql增删改触发器_sql server 用触发器记录增删改操作(转载)

--建跟踪触发器

create triggertr_cg_tz_logon 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 fromdeleted)then 'Update'

when exists(select 1 from inserted) and not exists(select 1 fromdeleted)then 'Insert'

when not exists(select 1 from inserted) and exists(select 1 fromdeleted)then 'Delete' end

if @op in('Update','Insert')begin

insert intocg_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()frominserted nleft join deleted o on o.id=n.idend

else

begin

insert intocg_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()fromdeleted oend

end

go

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值