有一个表:TABLE:ID,NAME两个字段,
其中的NAME字段有三个值:AA,BB,CC,如果用户试图修改这三个值时,我想写一个触发器不允许用户修改这三个值,应该怎么写这个触发器?

解决方案 »

  1.   

    create trigger tri_name on table for update as
    begin
      if (select count(*) from deleted) > 0 
      begin
        raiseerror('不能修改',16,1)
        rollback trasaction
      end
    end
      

  2.   


    create table t ( 
        Col01 int , Col02 int , Col03 int , Col04 int ,
        Col05 int , Col06 int , Col07 int , Col08 int ,
        Col09 int , Col10 int , Col11 int , Col12 int ,
        Col13 int , Col14 int , Col15 int , Col16 int ,
        Col17 int
    );
    go
    insert into t values (
        1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17
    );
    go
    create trigger tri_test on t for 
    update
    as
    begin
    declare @i int
    select @i=count(*) from syscolumns where id=object_id('t')
    while @i>0
    begin
    if (@i=2) and (substring( columns_updated() , (@i-1)/8+1 , 1 ) & power( 2, (@i-1)%8 )) > 0                                     --以第二列为例
      begin
        rollback tran                            --回滚
        RAISERROR ('不能修改指定字段', 16, 1)
      end
    set @i=@i-1
    end
    end
    go
    update t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1
    go
      

  3.   

    楼主的意思可能是:有很多记录,只有当name为AA或BB或CC时,不能修改当前的name值
    在楼上朋友的trigger中增加一条语,也以第二列为例,比如当第二列值为2进,不能修改当前记录的第二列值create table t ( 
        Col01 int , Col02 int , Col03 int , Col04 int ,
        Col05 int , Col06 int , Col07 int , Col08 int ,
        Col09 int , Col10 int , Col11 int , Col12 int ,
        Col13 int , Col14 int , Col15 int , Col16 int ,
        Col17 int
    );
    go
    insert into t values (
        1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17
    );
    go
    create trigger tri_test on t for 
    update
    as
    begin
    declare @i int
    select @i=count(*) from syscolumns where id=object_id('t')while @i>0
    begin
    if (@i=2) and (substring( columns_updated() , (@i-1)/8+1 , 1 ) & power( 2, (@i-1)%8 )) > 0
                                             --以第二列为例
      if (select count(*) from deleted where col02=2)>0         --增加语句
        begin
           rollback tran                            --回滚
           RAISERROR ('不能修改指定字段', 16, 1)
        end
    set @i=@i-1
    end
    end
    go
    update t set col17=1,col15=1,col16=1,col14=1,col09=1,col11=1,col01=1,col02=1,col03=1
    go
      

  4.   

    不出错就不对了,RAISERROR ('不能修改指定字段', 16, 1)这句话抛出异常,让你可以在delphi里捕获,17个字段和2个字段效果一样
      

  5.   

    只要你不更新第二个字段就不会出错在delphi里 try
      ...update部分
    except
      showmessage('第二个字段不能更新');
    end;就知道了
      

  6.   

    fhuibo(Sailor) 写的可以用了,
    create trigger tri_name on songtype for update as
    begin
      if (select name in ('AA','BB','CC') from deleted) > 0 
      begin
        raiserror('不能修改',16,1)
        rollback transaction
      end
    end我想问一下:用IN为什么不可以吗?应该怎么写呢
      

  7.   

    begin
      if (select count(*) from deleted) > 0 
      begin
        RAISERROR ('不能修改',16,1)
        rollback tran
      end
    end这个当然能用了,之不果他不光你指定的字段其他所有的都不能update,另外没有你那种用法
      

  8.   

    create trigger tri_name on songtype for update as
    begin
      if exists (select * from deleted where songtype in ('DISCO精选','舞曲欣赏','新歌推荐')) 
      begin
        raiserror('不能修改',16,1)
        rollback transaction
      end
    end
    这样子这个问题是解决了,但是用户还是可以删除,我想再加用户不可以删除应该怎么办?谢谢