zoukankan      html  css  js  c++  java
  • 禁用递归调用触发器

    CREATE   TRIGGER   [Update_ChildWareID]   ON   [dbo].[t_goodlctn_std]    
      FOR     UPDATE  
      AS  
      declare   @Lctn_Order   int  
      declare   @OldWareID   int  
      declare   @NewWareID   int  
      declare   @strSql   varchar(8000)  
      declare   @Lctn_Id   varchar(50)  
      IF   Update(Ware_id)  
      Begin  
      select   @OldWareID=Ware_Id     from   deleted  
      select   @Lctn_ID=Lctn_ID,@NewWareID=ware_Id,@Lctn_order=lctn_order   from   inserted  
      if   @OldWareId<>@NewWareId  
      Begin  
      set   @strSql='update   t_goodlctn_std   set   ware_id='+cast(@NewWareID   as   varchar(10))+'where   '  
      select   @strSql=  
      Case    
      when   @Lctn_order=0   then   @strSql+'substring(Lctn_id,1,1)'  
      when   @Lctn_order=1   then   @strSql+'substring(Lctn_id,1,3)'  
      when   @Lctn_order=2   then   @strSql+'substring(Lctn_id,1,4)'  
      when   @Lctn_order=3   then   @strSql+'   Lctn_id'  
      End  
      set   @strSql=@strSql+'='+''''+@Lctn_id+''''  
      exec(@strSql)  
      End  
      End  
       
       
      CREATE   TABLE   [dbo].[t_goodlctn_std]   (  
      [lctn_id]   [varchar]   (30)   COLLATE   Chinese_PRC_CI_AS   NOT   NULL   ,  
      [lctn_name]   [varchar]   (30)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
      [length]   [decimal](10,   0)   NULL   ,  
      [width]   [decimal](10,   0)   NULL   ,  
      [high]   [decimal](10,   0)   NULL   ,  
      [goodtype_id]   [int]   NULL   ,  
      [hold_num]   [decimal](18,   0)   NULL   ,  
      [digt_id]   [int]   NULL   ,  
      [ware_id]   [int]   NULL   ,  
      [parent_lctn]   [varchar]   (30)   COLLATE   Chinese_PRC_CI_AS   NULL   ,  
      [lctn_order]   [smallint]   NOT   NULL    
      )   ON   [PRIMARY]  
       
      其中,Lctn_ID是有编码规则的.  
      仓库级:1位;       料斗2位;         层:1位;         货盒:2位  
      例如A01B01,代表:A号仓库,01号料斗,B层,01号货盒  
       
      现在要求当更改某级的记录的Ware_id时,要求下级也要随着更改,我采用了触发器,但是我现在不知道它具体的工作流程.举个例子来说:  
      有四条记录  
      Lctn_id                                 Ware_ID  
      A                                                 1  
      A01                                             1  
      A01B                                           1  
      A01B01                                       1  
       
      这样,如果更新第一条记录,Lctn_ID为A的Ware_id为2,则触发器将2,3,4的Ware_id都改成2,但是我想了一下这是个递归的调用,因为当触发器更新A01时,实际上又触发了Update触发器.......这肯定要花费时间,而我要求它只进行一次Update就可以了.  
       
      现在请问:1/我设计的触发器有没有问题?  
                        2/如果我的是正确的,如果实现禁用递归
    1.
    企业管理器->服务器->属性->服务器设置->允许激发会激发其它触发器(嵌套触发器)的触发器  
      勾上即可以嵌套,否则不会嵌套。
    2.
    你要设置允许递归才行     exec   sp_dboption   '库名','recursive   triggers','true'

    3. 业管理器   右键   服务器-》属性-》服务器设置-》服务器行为-》去掉允许会激发其他触发器的触发器的选项   试试

    4.和程序中的递归是一样的嘛   
        
    无论你怎么写,都要用到递归算法,因为SQL不支持语句上的直接递归  
       
       
      嗯.我看了,现在确实是"允许嵌套"的设置.  
       
      你说要用到递归算法?这个我用其它的程序设计语言可以实现,用TSQL也可以吗???  
       
      能举个例子吗?  
       
      我从没想过TSQL也能用递归算法!

      create   proc   p_test  
      @a   int  
      as  
      if   @a<10  
      begin  
          set   @a=@a+1  
          exec   p_test   @a  
      end  
      go  
       
      --调用  
      exec   p_test   2
  • 相关阅读:
    遗传算法-目标函数与适应度函数变换
    遗传算法-编码
    Python 绘制甘特图
    fiddler抓包
    使用msf查询补丁和可利用提权漏洞
    Shodan入坑指南
    python 项目自动生成requirements.txt文件
    Tomcat 基于端口的虚拟主机配置
    python简单搭建http server
    metasploit后渗透 之 portfwd端口重定向
  • 原文地址:https://www.cnblogs.com/dadamoney/p/692219.html
Copyright © 2011-2022 走看看