注:原表有可能是,但不是改变里面原来数据行内容的顺序
billid ,itemno,goodsid,unitqty,
45 7 2     , 1
45 4 8     , 2
45 11 4     , 6      
45 12 5     ,2
45 10 6     ,300
45 13 7     ,350
45 9  8     ,200求触发器
billid ,itemno,goodsid,unitqty,
45 1 2     , 1
45 2 8     , 2
45 3 4     , 6      
45 4 5     ,2
45 5 6     ,300
45 6 7     ,350
45 7 8     ,200

解决方案 »

  1.   

    不大明白
    要求是什么时候触发修改itemno?
      

  2.   

    注:原itemno顺序是无规则的,只要重新按行数写入顺序号itemn即可
      

  3.   


    是这样吗:--drop table tbcreate table tb
    (billid int,itemno int,goodsid int,unitqty int)go--drop trigger trigger_tbcreate trigger dbo.trigger_tb  
    on tb  
    for insert  
    as  
      
    update tb  
    set itemno = (SELECT COUNT(1) FROM tb t 
                  WHERE t.billid =t1.billid  
                        AND t.itemno <= t1.itemno) 
    from tb t1   
    inner join inserted i  
            on t1.billid = i.billid  
      
    go  insert into tb
    select 45, 6, 2   , 1
    union all select 45 ,7 ,8     , 2
    union all select 45 ,8 ,4     , 6      
    union all select 45 ,9 ,5     ,2
    union all select 45 ,10 ,6     ,300
    union all select 45 ,11 ,7     ,350
    union all select 45 ,12 ,8     ,200--查询
    select *
    from tb
    /*
    billid itemno goodsid unitqty
    45 1 2 1
    45 2 8 2
    45 3 4 6
    45 4 5 2
    45 5 6 300
    45 6 7 350
    45 7 8 200
    */
      

  4.   

    谢谢你的解答,如果是修改数据\删除数据\插入数据如何解决序号itemno呢?
      

  5.   


    是这样吗:--drop table tbcreate table tb
    (billid int,itemno int,goodsid int,unitqty int)go--drop trigger trigger_tbcreate trigger dbo.trigger_tb  
    on tb  
    for insert  
    as  
      
    update tb  
    set itemno = (SELECT COUNT(1) FROM tb t 
                  WHERE t.billid =t1.billid  
                        AND t.itemno <= t1.itemno) 
    from tb t1   
    inner join inserted i  
            on t1.billid = i.billid  
      
    go  insert into tb
    select 45, 6, 2   , 1
    union all select 45 ,7 ,8     , 2
    union all select 45 ,8 ,4     , 6      
    union all select 45 ,9 ,5     ,2
    union all select 45 ,10 ,6     ,300
    union all select 45 ,11 ,7     ,350
    union all select 45 ,12 ,8     ,200--查询
    select *
    from tb
    /*
    billid itemno goodsid unitqty
    45 1 2 1
    45 2 8 2
    45 3 4 6
    45 4 5 2
    45 5 6 300
    45 6 7 350
    45 7 8 200
    */

    测试了,还是有时候itemno更新不对
      

  6.   

    如果再加入update触发器 则提示:
    超出了存储过程、函数、触发器或视图的最大嵌套层数(最大层数为 32)。
      

  7.   


    不能加update,因为insert 触发器中包含了update语句,要是加了这个update触发器,会导致嵌套触发器,就是死循环
      

  8.   


    不能加update,因为insert 触发器中包含了update语句,要是加了这个update触发器,会导致嵌套触发器,就是死循环现在就是insert 也不对,有什么更好的办法呢?
      

  9.   


    不能加update,因为insert 触发器中包含了update语句,要是加了这个update触发器,会导致嵌套触发器,就是死循环现在就是insert 也不对,有什么更好的办法呢?你能举个例子,就是insert有时候,不对的时候不,要不就先插入到临时表,然后把临时表的数据,进行更新,然后再插入到表里。