用计算列,把id作为计算列
create table #t2 (name varchar(100), bianhao int, id as bianhao)
insert into #t2(name, bianhao) values('aaa','1')
insert into #t2(name, bianhao) values('bbb','2')select * from #t2drop table #t2

解决方案 »

  1.   

    create table table1   
    (name char(20),  bianhao int,  id int)
    insert table1
    select    'aaa'    ,1          ,1
    union all
    select    'bbb'    ,2          ,2
    create trigger cx on table1
    for insert 
    as
    update table1
    set bianhao=(select id from inserted)
    where id=(select id from inserted)
    go
    insert table1(name,id)
    values('ccc',3)select * from table1name                 bianhao     id          
    -------------------- ----------- ----------- 
    aaa                  1           1
    bbb                  2           2
    ccc                  3           3(所影响的行数为 3 行)
      

  2.   

    create trigger trg_test on table1
    for insert 
    as
    update table1
    set bianhao=B.id
    from table1 A inner join inserted B on A.id=B.id
      

  3.   

    --抄
    create trigger trg_test on table1
    for insert 
    as
    update table1
    set bianhao=B.id
    from table1 A inner join inserted B on A.id=B.id--也可以直接有计算列
    create table #t2 (name varchar(100), bianhao int, id as bianhao)
    insert into #t2(name, bianhao) values('aaa','1')
    insert into #t2(name, bianhao) values('bbb','2')select * from #t2drop table #t2