Oracle触发器不在另一个表中插入值

时间:2015-08-31 13:58:16

标签: oracle triggers insert

我有SOAP_SERVICE表,如果新值插入到此表中,那么值也应该插入到SOAP_SERVICE_STATUS表中。我正在写下面的触发器,它没有任何错误并成功编译。但是当我尝试将值插入SOAP_SERVICE表时,该值不会插入到SOAP_SERVICE_STATUS表中。

create or replace TRIGGER RATOR_MONITORING.TRG_TRK_SOAP_SERVICE_STATUS
  AFTER INSERT 
 ON RATOR_MONITORING_CONFIGURATION.SOAP_SERVICE
 FOR EACH ROW
BEGIN
   INSERT INTO RATOR_MONITORING.SOAP_SERVICE_STATUS (SOAP_SERVICE_STATUS_ID,SOAP_SERVICE_ID,STATUS)
       VALUES (SOAP_SERVICE_STATUS_SEQ.nextval,:new.SOAP_SERVICE_ID,'N');
END;

2 个答案:

答案 0 :(得分:2)

抱歉,无法重现这一个:

SQL> CREATE USER RATOR_MONITORING IDENTIFIED BY "password"
2            DEFAULT TABLESPACE USERS;

User created.

SQL> CREATE USER RATOR_MONITORING_CONFIGURATION IDENTIFIED BY "password"
2            DEFAULT TABLESPACE USERS;

User created.

SQL> GRANT CONNECT TO RATOR_MONITORING, RATOR_MONITORING_CONFIGURATION;

Grant succeeded.

SQL> GRANT CREATE TABLE TO RATOR_MONITORING, RATOR_MONITORING_CONFIGURATION;

Grant succeeded.

SQL> GRANT CREATE SEQUENCE TO RATOR_MONITORING;

Grant succeeded.

SQL> GRANT CREATE ANY TRIGGER TO RATOR_MONITORING;

Grant succeeded.

SQL> ALTER USER RATOR_MONITORING QUOTA UNLIMITED ON USERS;

User altered.

SQL> ALTER USER RATOR_MONITORING_CONFIGURATION QUOTA UNLIMITED ON USERS;

User altered.

SQL> CONNECT RATOR_MONITORING_CONFIGURATION/password
Connected.
SQL> CREATE TABLE RATOR_MONITORING_CONFIGURATION.SOAP_SERVICE (SOAP_SERVICE_ID INTEGER);

Table created.

SQL> CONNECT RATOR_MONITORING/password
Connected.
SQL> CREATE TABLE RATOR_MONITORING.SOAP_SERVICE_STATUS
2              (SOAP_SERVICE_STATUS_ID INTEGER, SOAP_SERVICE_ID INTEGER, STATUS CHAR(1));

Table created.

SQL> CREATE SEQUENCE RATOR_MONITORING.SOAP_SERVICE_STATUS_SEQ;

Sequence created.

SQL> create or replace TRIGGER RATOR_MONITORING.TRG_TRK_SOAP_SERVICE_STATUS
2       AFTER INSERT
3      ON RATOR_MONITORING_CONFIGURATION.SOAP_SERVICE
4      FOR EACH ROW
5     BEGIN
6        INSERT INTO RATOR_MONITORING.SOAP_SERVICE_STATUS (SOAP_SERVICE_STATUS_ID,SOAP_SERVICE_ID,STATUS)
7            VALUES (SOAP_SERVICE_STATUS_SEQ.nextval,:new.SOAP_SERVICE_ID,'N');
8     END;
9     /

Trigger created.

SQL> CONNECT RATOR_MONITORING_CONFIGURATION/password
Connected.
SQL> INSERT INTO RATOR_MONITORING_CONFIGURATION.SOAP_SERVICE (SOAP_SERVICE_ID) VALUES (7);

1 row created.

SQL> COMMIT;

Commit complete.

SQL> CONNECT RATOR_MONITORING/password
Connected.
SQL> SELECT * FROM RATOR_MONITORING.SOAP_SERVICE_STATUS;

SOAP_SERVICE_STATUS_ID SOAP_SERVICE_ID S
---------------------- --------------- -
                     1               7 N

注意:

  1. CREATE ANY TRIGGER权限是RATOR_MONITORING在另一个架构中的表上创建触发器所必需的,
  2. 当我们在SQL * Plus中更改连接时,数据库会为我们创建一个新会话供我们使用。在提交这些更改之前,在一个会话中所做的更改在另一个会话中不可见。
  3. SQL * Plus在断开连接时隐式提交事务。在上面的示例中,我们在最后一个CONNECT之前明确提交,以便我们不依赖于隐式行为。

答案 1 :(得分:1)

最可能的解释是,您选择具有不同连接的表RATOR_MONITORING.SOAP_SERVICE_STATUS,而无需委托在RATOR_MONITORING_CONFIGURATION.SOAP_SERVICE中执行插入的原始会话。 提交后,记录应该是可见的。

另请注意,这是触发器与表不同的模式的副作用;用户RATOR_MONITORING_CONFIGURATION可以在表RATOR_MONITORING.SOAP_SERVICE_STATUS中插入,即使他/她没有INSERT授权!

要启用此功能,用户RATOR_MONITORING必须具有权限create any trigger - 这并不总是被视为最佳做法(强制权限) - 请参阅例如Tom Kyte