说来惭愧,简单应用数据库近一年,现在才知道触发器这玩意。
查看数据库中的触发器:
select * from sysobjects where xtype='TR'
新建数据库中的触发器,Mtable为表名或者视图名。其中afeter之后的insert可同理替换为update,delete。而after的对立面为instead of
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger Mtable_Insert
on Mtable
after insert
as
begin
print 'hello world'
end
go
创建完触发器后,也可以修改触发器
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[Mtable_Insert]
on [dbo].[Mtable]
after insert
as
begin
declare @num varchar(10)
select @num=Num from inserted
print @num
end
最近插入那条即为inserted。
删除触发器:
DROP TRIGGER [dbo].[Mtable_Insert]