create trigger ... on test1
for update
as
   update a set a.[name] = b.[name]
   from test2 a
   join inserted b on a.[name] = b.[name]

解决方案 »

  1.   

    create trigger ... on test1
    for update
    as
    declare @dd char(2)
    select @dd = name from inserted 
    update test2 set a.[name] = @dd where ....
      

  2.   

    happyflystone(爱你) 说的没错,用inserted表中的数据来更新你要同时更新的那张表
    触发器的设计用得比较多的就是inserted,或者deleted。deleted 和 inserted 是逻辑(概念)表。这些表在结构上类似于定义触发器的表(也就是在其中尝试用户操作的表);这些表用于保存用户操作可能更改的行的旧值或新值。
      

  3.   

    怎么不行呢?当我改变Test1时,Test2并没有任何变化.
      

  4.   

    强烈建议用sp;坚决抵制触发器;触发器只适用于实验室的,performance太低;极耗系统资源;不适合企业级应用!!!sp即可提高performance 又可提高数据安全性,所以为什么大型成熟的软件产品,总是适用大量的SP!!
      

  5.   

    CREATE trigger t_update on test1
    for update
    as
       update a set a.[name] = b.[name]
       from test2 a
       join inserted b on a.[name] = b.[name]
      

  6.   

    to HadesX(万骨枯):没办法呀,因为现在再改为用存储过程太麻烦了.我对触发器不是太了解,还请大家多多指教.
      

  7.   

    設置外鍵關聯即可﹕將表Test2中的name與Test1中name外鍵關聯
    方法﹕于SQL企業管理器中右擊test2表=>設計表(Design table)=>Relationships(關聯)=>New(新增)=>Primary key table 選擇test2表,Foreign key table 選擇test1表,關聯字段均選擇name=>OK確定即可
      

  8.   

    to rockyljt(Rocky)  :现在改结构可能对程序造成影响
      

  9.   

    这个问题很简单的呀:
    如果以前两表的姓名就是相等的,现在如果改变其中一个,就连带改这另一个的话就是这样写
    create trigger trg1 on table1 
    for update as 
    begin
         update table2
              set [name]=(select name from inserted )
         from deleted t1
         where table2.[name]=t1.name
    end
    --本触发器只能对于一次插入一个记录有用。马可的语句是行不通的。
    如果你不是通过name来标志两个表之问的关系就好办了。假设是通过ID1则create trigger trg1 on table1 
    for update as 
    begin
         update table2
              set [name]=t1.[name]
         from insert t1
         where table2.[id1]=t1.[id1]
    end
      

  10.   

    注 deleted 表和inserted 表是系统自动生成的,只能在触发器中使用。
      

  11.   

    to ghosthjt(天煞孤星):Incorrect Syntax near the keyword 'insert'
      

  12.   

    create trigger trg1 on table1 
    for update as 
    begin
         update table2
              set [name]=t1.[name]
         from inserted t1
         where table2.[id1]=t1.[id1]
    end他可能是笔误