首页 > php教程 > PHP开发 > 正文

数据库触发器获取触发触发器的sql语句

高洛峰
发布: 2016-12-14 16:33:45
原创
1381人浏览过

--触发器  

  alter          trigger   tr_standardlog   on   t_standard  

  for   insert,delete,update  

  as  


  --rowcnt  

  declare   @rows   int  

  set   @rows=@@rowcount  


  set   nocount   on  


  --sql 存储执行的sql语句

  create   table   #t   (  

  eventtype    varchar(20),  

  parameters   int,  

  eventinfo   varchar(300)  

  )  


  --获取sql语句
  declare @spid varchar(20)

  set @spid=cast(@@spid as varchar)

  --optype  

  declare   @optype   int  

  set   @optype=2                                                                                                                                 --update  

  if   not   exists   (select   1   from   inserted)   set   @optype=3                         --delete  

  if   not   exists   (select   1   from   deleted)   set   @optype=1                          --insert
 
  if @optype=1
    begin
        insert into t_standardlog(sqltext,optype,createtime)
        select  'insert into tbweigh(weighid,fid,cardno,stdnno,proname,promodel,unit_from,unit_to,transport,driver,'
            +'package,gross_weight,tare_weight,net_weight,deduct,fact_weight,price,amount,unit,gross_time,'
            +'tare_time,net_time,edit_time,opman_gross,opman_tare,opman,editman,scalename_gross,scalename_tare,'
            +'scalename_net,transfer_date,transfer_user,remark,iscanceled,createtime)'
            +'values(:weighid,'''+isnull(cast(a.f_id as nvarchar(18)),'null') +''','''+isnull(a.f_cardno,'null')+''','''+isnull(a.f_stdno,'null')+''','''+isnull(a.f_proname,'null')+''','''+isnull(a.f_promodel,'null')+''','''+isnull(a.f_unitfrom,'null')+''','''+isnull(a.f_unitto,'null')+''','''
            +isnull(a.f_transport,'null')+''','''+isnull(a.f_driver,'null')+''','''+isnull(a.f_memo,'null')+''','+isnull(cast(a.f_gross as nvarchar(30)),'null')+','+isnull(cast(a.f_tare as nvarchar(30)),'null')+','
            +isnull(cast(a.f_net as nvarchar(30)),'null')+','+isnull(cast(a.f_deduct as nvarchar(30)),'null')+','+isnull(cast(a.f_fact as nvarchar(30)),'null')+','
            +isnull(cast(a.f_price as nvarchar(30)),'null')+','+isnull(cast(a.f_money as nvarchar(30)),'null')+','''+isnull(a.f_unit,'null')+''','''+isnull(cast(a.f_grosstime as nvarchar(30)),'null')+''','''
            +isnull(cast(a.f_taretime as nvarchar(30)),'null')+''','''+isnull(cast(a.f_nettime as nvarchar(30)),'null')+''','''+isnull(cast(a.f_edtime as nvarchar(30)),'null')+''','''
            +isnull(a.f_opmangross,'null')+''','''+isnull(a.f_opmantare,'null')+''','''+isnull(a.f_opman,'null')+''','''+isnull(cast(a.f_edtime as nvarchar(30)),'null')+''','''+isnull(a.f_scalenamegross,'null')+''','''+isnull(a.f_scalenametare,'null')+''','''
            +isnull(a.f_scalenamenet,'null')+''',sysdate,:userid,:remark,'+isnull(cast(a.f_iscancel as nvarchar(1)),'null')+',:createtime'
            +');','insert',getdate()
       from inserted a
    end
  else if @optype=3 or @optype=2
    begin
       insert #t exec('dbcc inputbuffer ('+@spid+')')
       insert into t_standardlog(sqltext,optype,createtime) --把执行的sql语句存储在临时表中
       select t.eventinfo,case @optype when 1 then 'insert' when 2 then 'update' when 3 then 'delete' else ''end,getdate() from #t t   --从临时表中提取触发的sql语句
    end

go
set quoted_identifier off 
go
set ansi_nulls on 
go


触发器会把数据临时存储在inserted和deleted表中,inserted 和deleted表是逻辑表,只有在触发器中才存在,触发器结束了,就会自动删除,如果是insert操作,则新添加的数据存储在inserted表中,如果是update操作,其实相当于先删除然后再插入,即在deleted表中记录原来的记录,在inserted表中记录更新后的数据,如果是deleted操作,则删除的数据被记录在deleted表中。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号