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

瘦马 2015-09-07 03:10:08
求一个触发器的写法,当表中某些字段的值为指定值时,则触发

有表t1
字段
col1、col2、col3
当col1、col2、col3的值 为 “指定值” 时
则把col1、col2、col3的值修改为 null
...全文
601 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
等不到来世 2015-09-07
  • 打赏
  • 举报
回复
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

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧