我在一个数据表中同时写入了删除、新增、修改的触发器,发现无法实现触发器的功能,是不是不能这样操作
2个回答
展开全部
不知道用的是什么数指裤据库了.
Oracle / SQL Server 都唯芹简是可以的。 下首扒面是一个 SQL Server 的例子:
INSERT/UPDATE/DELETE行为判断
1> CREATE TRIGGER AfterAllTest
2> ON test_trigger_table
3> FOR INSERT,UPDATE,DELETE
4> AS
5> BEGIN
6> PRINT('AFTER ALL');
7> IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
8> BEGIN
9> PRINT('INSERTING');
10> END;
11> IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
12> BEGIN
13> PRINT('UPDATING');
14> END;
15> IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
16> BEGIN
17> PRINT('DELETING');
18> END;
19> END;
20> go
1> INSERT INTO test_trigger_table(id, name) VALUES (1, 'ABC');
2> UPDATE test_trigger_table SET name = 'XYZ' WHERE id = 1;
3> DELETE FROM test_trigger_table WHERE id = 1;
4> go
AFTER ALL
INSERTING
(1 行受影响)
AFTER ALL
UPDATING
AFTER ALL
DELETING
1>
Oracle / SQL Server 都唯芹简是可以的。 下首扒面是一个 SQL Server 的例子:
INSERT/UPDATE/DELETE行为判断
1> CREATE TRIGGER AfterAllTest
2> ON test_trigger_table
3> FOR INSERT,UPDATE,DELETE
4> AS
5> BEGIN
6> PRINT('AFTER ALL');
7> IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
8> BEGIN
9> PRINT('INSERTING');
10> END;
11> IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
12> BEGIN
13> PRINT('UPDATING');
14> END;
15> IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
16> BEGIN
17> PRINT('DELETING');
18> END;
19> END;
20> go
1> INSERT INTO test_trigger_table(id, name) VALUES (1, 'ABC');
2> UPDATE test_trigger_table SET name = 'XYZ' WHERE id = 1;
3> DELETE FROM test_trigger_table WHERE id = 1;
4> go
AFTER ALL
INSERTING
(1 行受影响)
AFTER ALL
UPDATING
AFTER ALL
DELETING
1>
参考资料: http://hi.baidu.com/wangzhiqing999/blog/item/126bdc0c7a7dd79b0a7b829d.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询