当前位置: 代码迷 >> Sql Server >> 触发器的写法,当表中某些字段的值为指定值时,则触发
  详细解决方案

触发器的写法,当表中某些字段的值为指定值时,则触发

热度:40   发布时间:2016-04-24 08:56:28.0
求一个触发器的写法,当表中某些字段的值为指定值时,则触发
求一个触发器的写法,当表中某些字段的值为指定值时,则触发

有表t1
字段
col1、col2、col3
当col1、col2、col3的值 为 “指定值” 时
则把col1、col2、col3的值修改为 null

------解决思路----------------------
if object_id('tgr_t1', 'tr') is not null
    drop trigger tgr_t1
go
create trigger tgr_t1
on t1
    for insert,update
as
    declare @id int, @col1 nvarchar(128), @col2 nvarchar(128), @col3 nvarchar(128);
    select @id=id,@col1 = col1, @col2 = col2, @col3 = col3 from inserted;
    
    if @col1=N'指定值1'
update t1 set col1=null where id=@id
if @col2=N'指定值2'
update t1 set col2=null where id=@id
if @col3=N'指定值1'
update t1 set col3=null where id=@id
go
  相关解决方案