if update(某列)
  select 主键列 from inserted where 某列=3

解决方案 »

  1.   


    COL_NAME
    返回数据库列的名称,该列具有相应的表标识号和列标识号。语法
    COL_NAME ( table_id , column_id )
      

  2.   

    create trigger tr_update on 表
    for update
    as
    declare @主键 int
    if update(某列)  --检查有没有被更新
    begin
    select @主键=主键 from inserted where 某列=3
    if @@rowcount=0 --如果=0,则是没有被更新为3
    print '没有被更新为3'
    else
    begin
    select 被更新的主键=@主键
    end
    end
      

  3.   

    注意,由于SQL的触发器不是行级触发,所以如果同时有多条记录的某列被更新为3上面的方法只能得到其中一条记录的主键,如果要得到多条记录的主键,用游标处理.
      

  4.   

    Let me try ....thanks .....
      

  5.   

    正如邹老大所讲,多条记录同时执行时出现了问题,于是我把代码改成如下create trigger tr_update on 表
    for update
    as
    declare @主键 int
    if update(某列)  --检查有没有被更新
    begin
    select @主键=主键 from inserted where 某列=3 if @@rowcount>1---如果总数大于1
    begin
    DECLARE tables_cursor CURSOR FOR  select 主码 from inserted where 某列=3
    begin
    open tables_cursor
    FETCH NEXT FROM tables_cursor INTO 变量2
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    -------执行相关代码
    FETCH NEXT FROM tables_cursor INTO 变量2
    END
    close tables_cursor
    end if @@rowcount=1------如果总数等于1
                        ----执行相关代码
    end结果我的代码执行没有任何效果晕了。还是麻烦各位老大了
      

  6.   

    create trigger tr_update on 表
    for update
    as
    declare @主键 int
    if update(某列)  --检查有没有被更新
    begin
    if exists(select 1 from inserted where 某列=3)
    begin
    DECLARE tables_cursor CURSOR FOR
    select 主码 from inserted where 某列=3 open tables_cursor
    FETCH NEXT FROM tables_cursor INTO 变量2
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    -------执行相关代码
    FETCH NEXT FROM tables_cursor INTO 变量2
    END
    close tables_cursor
    deallocate tables_cursor
    end
    else
    begin
                        ----执行相关代码 end
    end
      

  7.   

    if @@rowcount>0---如果总数大于1  --如果保留你原来的,应该是>0