CREATE TRIGGER trg_A1_update ON A1
FOR UPDATE
AS
BEGIN
   IF UPDATE(age)
      update A2 set age=inserted.age 
END

解决方案 »

  1.   

    drop trigger A1_update
    GO
    CREATE TRIGGER A1_update ON A1
    FOR UPDATE
    AS
    IF UPDATE(age)
    BEGIN
    update b
    set b.age=a.age
    from A1 a, A2 b
    where a.name=b.name and a.age<>b.age
    END 
    GO
      

  2.   

    抱歉,上面的错了
    CREATE TRIGGER trg_A1_update ON A1
    FOR UPDATE
    AS
    BEGIN
       IF UPDATE(age)
          update A2 set age=inserted.age where Name=inserted.name
    END
      

  3.   

    IF UPDATE(age)
      BEGIN    declare @n varchar(50)
        declare @a tinyint    select @n=[name] from deleted
        select @a=age from inserted    update A2
        set age=@a 
        where [name]=@n   END 
      

  4.   

    --同步处理的触发器:
    create trigger t_update on A1
    for update
    as
    if update(age)
    update A2 set age=b.age
    from A2 a join inserted b on a.name=b.name
    go
      

  5.   

    --下面是测试--测试数据
    create table A1(name varchar(10),age int)
    insert into A1
    select 'Kevin',12
    union all select 'Tony',29create table A2(name varchar(10),age int,sex char(2))
    insert into A2
    select 'Kevin',12,'男'
    union all select 'Tony',29,'男'
    go--同步处理的触发器:
    create trigger t_update on A1
    for update
    as
    if update(age)
    update A2 set age=b.age
    from A2 a join inserted b on a.name=b.name
    go--更新测试1:单记录更新
    update A1 set age=18 where name='Kevin'--显示测试结果
    select * from A1
    select * from A2--更新测试2:多记录更新
    update A1 set age=age+1--显示测试结果
    select * from A1
    select * from A2
    go--删除测试环境
    drop table A1,A2/*--测试结果--更新测试1:单记录更新的测试结果
    name       age         
    ---------- ----------- 
    Kevin      18
    Tony       29(所影响的行数为 2 行)name       age         sex  
    ---------- ----------- ---- 
    Kevin      18          男
    Tony       29          男(所影响的行数为 2 行)
    --更新测试2:多记录更新的测试结果
    name       age         
    ---------- ----------- 
    Kevin      19
    Tony       30(所影响的行数为 2 行)name       age         sex  
    ---------- ----------- ---- 
    Kevin      19          男
    Tony       30          男(所影响的行数为 2 行)
    --*/
      

  6.   

    create trigger up_trim on a1 
    for update as
      if exists(select * from inserted where name='kevin' and age='18')
    begin
       update a2 set age='18' 
        from inserted join a2 on inserted.name=a2.name
    end