我想让MSSQL中某个值不允许修改。或者修改无效应该怎样写触发器?
不是一个列都不允许。。就是其中某一个值!!

解决方案 »

  1.   

    CREATE TRIGGER TRI_NAME  ON TB 
    FOR UPDATE
    AS
    IF UPDATE(COL)    
      
         ROLLBACK
      

  2.   

    create trigger my_tri on ta
    for update
    asif update(col)
    begin
      if (select col from deleted)=某值
    rollbackend
      

  3.   

    看楼主的意思是不是这一列不允许修改,而是这一列不允许某个值插入或者修改成某个值,是吗?
    如果是这样,那么就用CHECK!
      

  4.   

    对..我的意思就是.比如...
    select name from Users where id=1  查询结果是"yyy"..
    我的意思是不允许修改这个值..就是不允许比如这条语句:update Users set name="xxx" where id=1
      

  5.   

    那你的意思是NAME这个列里都不允许改名字么?那得用触发器,用INSTEAD OF触发器
      

  6.   

    就用楼上写的触发器就行create table ta (a int,b int)create trigger tru on ta 
    for update 
    as 
    begin 
    if update(a)
    begin
    raiserror ('column a mustn''t be updated',16,1)
    rollback tran
    end
    end insert into ta select 1,2update ta set b=100update ta set a=100
      

  7.   

    估计也不让删除把create trigger tri_users on Users
    for update,delete
    as
    if not exists(select 1 from inserted) and exists(select 1 from deleted where id=1) --不让删除
       or exists(select 1 from inserted where id=1) and update(name)    --不让更新id=1的name ROLLBACK
    go
      

  8.   

    drop table ta 
    create table ta (a int,b int)create trigger tru on ta 
    for update 
    as 
    begin 
    if exists(select 1 from  deleted d,inserted i where i.a=d.a and i.a=1 and i.b<>d.b)
    begin
    raiserror ('column a mustn''t be updated',16,1)
    rollback tran
    end
    end insert into ta select 1,2 union all select 2,3update ta set b=100 where a=1update ta set b=100 where a=2
      

  9.   

    if object_id('ta') is not null drop table ta 
    go
    create table ta (a int,b int)
    go
    create trigger tru on ta 
    for update 
    as 
    begin 
    if exists(select 1 from  deleted where a=1) and update(b)
    begin
    raiserror ('column a mustn''t be updated',16,1)
    rollback tran
    end
    end 
    go
    insert into ta select 1,2 union all select 2,3
    go
    update ta set b=100 where a=1
    go
    update ta set b=100 where a=2哈哈
    不停的学习阿