求一个简单的触发器

来源:百度知道 编辑:UC知道 时间:2024/06/17 10:27:58
求一个简单的触发器,例如当更新表table1的列col1,col2时,如果新的值为空,就赋值成0
我使用的是sql数据库

CREATE Trigger dbo.[TriggerOnInsert]

On Table1

For Insert

if ( exists ( select * from inserted where col1 is null or col2 is null ) )

begin

DECLARE @Id AS INT

SET @Id = ( select top 1 [id] from inserted where col1 is null or col2 is null )

update dbo.[Table1] set col1 = 0 , col2 = 0 where [Id] = @Id
end