表       id      A       B
         1       xq      0
         2       xp      0
          ........
         100       xp      1如上表:id是个顺序递增(+1)的列,当新增加的id=100这条记录,因为这时候字段B为1,,我要写个触发器把他改为0,而不去动其他数据
请问怎么写,数据库环境 sql2005
思路应该是获取到这个id  然后update  请教大侠!

解决方案 »

  1.   

    另外同时插入多行的时候
    比如:同时插入id  101 到105  B字段都要更新为0怎么做?是不是能从 inserted表来获取后更新
      

  2.   


    update table1 set B=0 where id>99一句代码全部搞定。
      

  3.   

    没必要用触发器,插入语句后面跟句update table1 set B=0 where id>99,再用个事务包裹起来即可。
    另外可否考虑下,让插入的时候就保证B列的值为0,省的插入后再修改了。
      

  4.   

    if OBJECT_ID('t1','U') is not null drop table t1
    go
    create table t1
    (
    tid int identity(1,1) primary key,
    tA varchar(10),
    tB varchar(10)
    )
    truncate table t1
    declare @i int set @i=0;
    while(@i<101)
    begin
    insert into t1(tA,tB) values('tA','tB');
    set @i=@i+1;
    end
    go
    create trigger update_t1 on t1
    for insert
    as
    declare @tid int set @tid=0
    begin
    select @tid=count(0) from t1;
    if(@tid=100)
    begin
    update t1 set TB='1' where tid=@tid;
    end
    end
    goselect * from t1 where tid=100
      

  5.   

    没写注释,那个出发前在while循环之前执行