CREATE TRIGGER TABLEA_Update 
   ON  TABLEA 
   AFTER UPDATE
AS 
BEGIN
IF UPDATE(用户名)begin Update TABLEB 
          Set 用户名=Inserted.用户名
          From TABLEB,Deleted,Inserted     --Deleted和Inserted时系统临时表
          Where TABLEB.用户名=Deleted.用户名
end
IF UPDATE(密码)begin Update TABLEB 
          Set 密码=Inserted.密码
          From TABLEB,Deleted,Inserted     
          Where TABLEB.密码=Deleted.密码
end
SET NOCOUNT ON;
END
GO

解决方案 »

  1.   

    上面2个Update后面还要加 and TABLEA.ID=TABLEB.ID(这个条件是关联你A B两表记录的字段)
      

  2.   

    1.最好是使用事务,而不要使用触发器。
    建立一个存储过程,在存储过程中使用事务,大致的sql如下:
    create procedute UpdateAAndB
    as
       declare @errorCode int
       select @errorCode=@@error
       begin transaction
       if @errorCode=0
       begin
    update 表A
             set name=新用户名, password=新密码
             where id=用户id
    select @errorCode=@@error
       end
    if @errorCode=0
      begin
      update 表B
               set name=新用户名, password=新密码
               where id=用户id
      select @errorCode=@@error
      end
      if @errorCode=0
    commit transaction
      else
    rollback transaction2.最佳解决办法是重构你的数据库设计:专门建立一张用户表用来存放用户名和密码,A表和B表引用这张用户表,这样用户名和密码更新的时候只要更新用户表就行了。接分!