create trigger tr_A_I on A
for insert
as
insert B(id,字段,字段)
select id,字段,字段
from inserted

解决方案 »

  1.   

    /*
    如果test2的id时自增长的。下面的触发器中的内容需要修改。
    test1的id子增长没有关系。
    */
    create table test1 (id_ int identity(1,1),name_ varchar(20))
    gocreate table test2 (id_ int ,name_ varchar(20))
    go
    CREATE TRIGGER tr_test1_i ON [dbo].[test1] 
    FOR INSERT
    AS
    set nocount on
    insert into test2(id_,name_)
    select id_,name_
    from inserted
    set nocount off
    goinsert into test1(name_)
    select 'a'select * from test2
    /*
    id_ name_
    ---------------------
    1 a
    */
      

  2.   

    如果test2的id也时增长的,触发器内容修改为:
    set nocount on
    SET IDENTITY_INSERT test2 on
    insert into test2(id_,name_)
    select id_,name_
    from inserted
    where id_ not in(select id_ from test2)--仅插入test2中没有的id值。
    SET IDENTITY_INSERT test2 off
    set nocount off