在Trigger中使用字典表时输出不正确

时间:2018-04-20 14:15:01

标签: oracle oracle11g triggers

我使用ALL_TABLES/ALL_TAB_COLUMNS来计算模式(EDW_SRC)和另一个模式(EDW_STG)中的表的数量。当我在我的sql开发人员中运行查询时,我得到了正确的计数,如下所示。但是如果我在触发器中放入相同的查询,我会错误地计算其他模式(EDW_STG)。

请参考以下代码: (这只是复制问题的示例代码,而不是我的业务要求。我在实际代码中引用ALL_TAB_COLUMNS来获取不同架构中特定表中的列数,我有Select选择权。 )

select user from dual;

USER
-----
EDW_SRC

DROP TABLE ABC;
Table ABC dropped.

CREATE TABLE ABC(ID NUMBER);
Table ABC created.

select count(1) EDW_STG_CNT 
from all_tables
where owner='EDW_STG';--Different Schema

EDW_STG_CNT
----------
101

select count(1) EDW_SRC_CNT 
from all_tables
where owner='EDW_SRC';--My Schema

 EDW_SRC_CNT
------------
1554

create or replace trigger trig_test_dml_abc
before insert on abc
DECLARE
V_STG_CNT number :=NULL;
V_SRC_CNT number :=NULL;
begin
DBMS_OUTPUT.PUT_LINE('***** TRIGGER OUTPUT *****');
select count(1) into V_SRC_CNT from all_tables
where owner='EDW_SRC'; --My Schema
DBMS_OUTPUT.PUT_LINE('My Schema EDW_SRC_CNT :'||V_SRC_CNT);

select count(1) into V_STG_CNT from all_tables
where owner='EDW_STG'; --Different Schema
DBMS_OUTPUT.PUT_LINE('Different Schema EDW_STG_CNT :'||V_STG_CNT);
end;

Trigger TRIG_TEST_DML_ABC compiled

INSERT INTO ABC VALUES (2);
1 row inserted.

***** TRIGGER OUTPUT *****
My Schema EDW_SRC_CNT :1554
Different Schema EDW_STG_CNT :2

不同的模式计数应该是101.为什么它会变为2。

Oracle版本:     Oracle Database 11g企业版11.2.0.4.0版 - 64位生产

由于 ķ

0 个答案:

没有答案