日期:2014-05-17 浏览次数:20647 次
CREATE TABLE [dbo].[tb_trigger_test](
[trigger_test_id] [int] IDENTITY(1,1) NOT NULL,
[trigger_test_name] [varchar](100) NULL,
[trigger_test_value] [int] NULL,
CONSTRAINT [PK_tb_trigger_test] PRIMARY KEY CLUSTERED
(
[trigger_test_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TRIGGER TRITest
ON tb_trigger_test
FOR UPDATE,DELETE,INSERT
AS
BEGIN
IF UPDATE(trigger_test_value)
BEGIN
IF EXISTS(SELECT 1 FROM DELETED)--更新
BEGIN
IF EXISTS(SELECT 1 FROM INSERTED a INNER JOIN DELETED b ON a.trigger_test_id=b.trigger_test_id WHERE a.trigger_test_value<b.trigger_test_value)
ROLLBACK TRAN
END
ELSE --插入
BEGIN
IF EXISTS(SELECT 1 FROM INSERTED WHERE trigger_test_value<0)
ROLLBACK TRAN
END
RETURN
END
--删除
IF EXISTS(SELECT 1 FROM DELETED WHERE trigger_test_value<>0)
ROLLBACK TRAN
END