如果有个表tablename,有两个字段col1,col2
Create Table TableName
(
  col1 int,
  col2 datetime
)建立触发器,要求当更新col1时,触发更新col2的值
Create Trigger trg on TableName
 for Update AS
Declare @cc int
  select @cc = col1 from Deleted
  update col2 = GetDate() where col1 = @cc这样做的话,好象只能做表级的触发器,会造成死循环,有没有什么办法解决?

解决方案 »

  1.   

    好象碰到过此类问题,记得我是用if 语句排除掉的。updated(字段名)来判断是否更改了该字段
      

  2.   

    一样的,
    create trigger trg on tablename
    for update as
    if update(YourColumn)
    .....
    省略号后的东西自个儿加!
    试试看
      

  3.   

    Create Trigger ... on table
    for Update
    as if Update(Col1)
      update Table set Col2=...
    if Update(Col2)
      Update Table set Col1=........
    在觸發器內部默認配置同一個表不會循環觸發
      

  4.   

    可以
    create trigger trigger1 on table1
    for update
    as 
    if update(field1)
    declare @a =  field2 ....uadate table1 set ...
      

  5.   

    Create Trigger ... on table
    for Update
    as if Update(Col1)
      begin
          update Table set Col2=...    --更改字段二
          Update Table set Col1=...    --更改字段一
      end
    if Update(Col2)
      begin
        Update Table set Col1=...       --更改字段一
        Update Table set Col2=...       --更改字段二
      endif Update(Col1) or Update(Col2)
      在觸發器內容的Update,Insert,Delete不會再觸發本身的觸發器。
    .....