是否可以在Oracle中触发器内创建或替换视图?
视图是通过连接2个表创建的,其中一个是触发器更新的表
答案 0 :(得分:3)
只是为了提供所有选项(但是在触发器内创建视图的想法可能很奇怪......)你可以在触发器中创建一个视图。是的,隐式COMMIT
将会跟随,但如果我们使触发器在自治事务中工作,那么动态DDL将不会失败。使用Luke Woodward的例子:
CREATE TABLE test (a integer);
INSERT INTO test (a) VALUES (5);
CREATE OR REPLACE TRIGGER test_trig
AFTER UPDATE ON test
FOR EACH ROW
DECLARE
-- making the trigger work outside of the main transaction
PRAGMA autonomous_transaction;
BEGIN
EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW test_view AS SELECT * FROM test';
END;
/
UPDATE test SET a = 6;
SELECT * FROM test_view;
A ---------- 6
答案 1 :(得分:2)
以下是您尝试执行此操作时会发生的情况:
SQL> CREATE TABLE test (a integer);
Table created.
SQL> INSERT INTO test (a) VALUES (5);
1 row created.
SQL> COMMIT;
Commit complete.
SQL> CREATE OR REPLACE TRIGGER test_trig
2 AFTER UPDATE ON test
3 FOR EACH ROW
4 BEGIN
5 EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW test_view AS SELECT * FROM test';
6 END;
7 /
Trigger created.
SQL> UPDATE test SET a = 6;
UPDATE test SET a = 6
*
ERROR at line 1:
ORA-04092: cannot COMMIT in a trigger
ORA-06512: at "LUKE.TEST_TRIG", line 2
ORA-04088: error during execution of trigger 'LUKE.TEST_TRIG'