--TRY
CREATE TRIGGER Update_table1 
ON table1 
FOR update 
AS 
if Update(status)          
   insert table2 select id,status from deleted d where d.status=1 

解决方案 »

  1.   

    --TRY
    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS 
    if Update(status)          
       insert table2 select id,status from inserted i where i.status=1 and table2.id=i.id
      

  2.   

    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS 
    insert into table2 select id,[status] from inserted where [status]=1
      

  3.   

    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS 
    if Update(status)          
       insert table2 select aaa.id,aaa.status from inserted aaa where aaa.status=1 
      

  4.   

    create table table1(id int,[status] int)
    create table table2(id int,[status] int)
    insert into table1 select 1,0
    go
    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS 
    insert into table2 select id,[status] from inserted where [status]=1
    go
    update table1 set [status]=1 where id=1
    select * from table2
    update table1 set [status]=0 where id=1
    select * from table2  --不变,还是原来的记录
    go
    drop table table1,table2
    /*
    id          status
    ----------- -----------
    1           1id          status
    ----------- -----------
    1           1*/
      

  5.   


    CREATE TRIGGER Updatetable1 
    ON table1 
    FOR update 
    AS 
    if Update(status)          
       insert table2 select aa.id,aa.status from inserted aa where aa.status=1 
      

  6.   

    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update
    AS 
    if Update(status) 
    begin insert into table2 
    select 
    id,
    [status] 
    from inserted 
    where [status]=1
    end
      

  7.   

    不好意思,请问
    如果向table2插入id时,table2中已经有一样的id,则改变table2中status值为table1中的值,怎么做,谢谢.
      

  8.   


    create table table1(id  int identity(1,1),status nvarchar(128))
    go
    create table table2(id int,status nvarchar(128))
    go
    insert into table1(status) select 'a' union all select 'b';if object_id(N'tr_table2','TR') is not null
    drop trigger tr_table2
    go
    create trigger tr_table2 on table2
    instead of insert 
    as
    declare @rowcount int,@px int ,@id int
    if object_id(N'tempdb..#t','U') is not null
    drop table #t;
    select px=identity(int,1,1),* into #t from inserted
    select @rowcount=@@rowcount
    create unique clustered index index_#t on #t(px);
    if @rowcount =0 return;
    if @rowcount=1
    begin
    if exists(select * from #t a where  not exists(select * from table1 b where a.id=b.id))
    begin
    insert into table2 select id,status from #t
    end
    else
    begin
    insert into table2 select a.id,b.status from #t a inner join table1 b on a.id=b.id
    end
    end
    if @rowcount>1
    begin
    select @px=px,@id=id from (select top 1 * from #t )x
    while @@rowcount>0
    begin
    if not exists(select * from table1 where id=@id)
    begin
    insert into table2 select id,status from #t where px=@px
    end
    else
    begin
    insert into table2 select a.id,b.status from #t a inner join table1 b on a.id=b.id and a.px=@px
    end
    select @px=px,@id=id from (select top 1 * from #t  where px>@px)x
    end
    end
    goselect * from table1--测试
    insert into table2 select 1,'b' union all select 2,'a'
    select * from table1select * from table21 a
    2 b1 a
    2 b--其他测试自己测试可以多条单条换着测试
      

  9.   

    晕,怎么写这么长,是不是理解错我的意思了.
    我的前提是table1中status变为1,向table2中插入值id,status,如果
    在向table2插入id时,table2中已经有一样的id,则改变table2中status值为table1中的值,不要再插入了,
    怎么做,谢谢.
      

  10.   

    触发器应该建在table1上,而且是更新操作,楼主,我晕
      

  11.   

    更新表一(id,status),update操作,自动向表二插入(id,status),如果发现表二中已经有同样的ID号,则不插入数据,而是更新表二对应id的status值与表一一致.怎么就不明白呢,晕
      

  12.   

    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS 
    if Update(status)          
       insert table2 select id,status from inserted i where i.status=1 
      

  13.   


    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS 
    if Update(status)          
       insert table2 select id,status from inserted i 
       where i.status=1 and ID not in (select ID from table2)
      

  14.   

    如果发现表二中已经有同样的ID号,则不插入数据,而是更新表二对应id的status值与表一一致dobear_0922我后面还要跟新这个id一致的呢,请教,怎么写
      

  15.   

    if Update(status)          
       insert table2 select id,status from inserted i 
       where i.status=1 and ID not in (select ID from table2)  update table2 .......from insertied i
       where i.id in(select ID from table2)
    这个咋写????
      

  16.   

    CREATE TRIGGER Update_table1 
    ON table1 
    FOR update 
    AS
    if Update(status)  
    begin
    update table2  --先更新
    set [status]=i.status
    from insertied i
    where table2.id=i.id insert table2  --再插入
    select id,status 
    from inserted i 
    where i.status=1 and ID not in (select ID from table2) 
    end
    go