简体   繁体   English

Oracle SQL触发器不起作用

[英]Oracle Sql trigger not working

I have a very simple trigger, that prints out the username and date when a new row is inserted into the users table. 我有一个非常简单的触发器,当在用户表中插入新行时,它会打印出用户名和日期。 But after it successfully compiled, the trigger didn't get triggered (there was no output in the dbms window). 但是在成功编译之后,触发器没有被触发(dbms窗口中没有输出)。 So, I simplified my code until I got this: 因此,我简化了代码,直到得到:

CREATE OR REPLACE TRIGGER logger
AFTER INSERT ON USERS
BEGIN
   DBMS_OUTPUT.PUT_LINE('User added with name:');
END;

If I run the code in the SQL worksheet (from BEGIN to END), I can see the output, but not when I try to use the trigger. 如果我运行SQL工作表中的代码(从BEGIN到END),则可以看到输出,但是在尝试使用触发器时看不到输出。 Where is the problem? 问题出在哪儿?

There are two options, one is that the trigger is not firing, the other is that it is, but you're not seeing the output. 有两种选择,一种是触发器不触发,另一种是触发器,但是您没有看到输出。

  1. Assuming you're working in a console program like sqlplus, make sure to do SET SERVEROUTPUT ON before inserting your row. 假设您正在使用sqlplus之类的控制台程序,请确保在插入行之前先执行SET SERVEROUTPUT ON
  2. If that doesn't fix it, then make sure the trigger is firing. 如果那不能解决问题,请确保触发触发器。 Try creating a simple table: 尝试创建一个简单的表:

    CREATE TABLE logtable ( msg VARCHAR2(30 CHAR)); 创建表日志表(msg VARCHAR2(30 CHAR));

Next, add this to your trigger, 接下来,将其添加到触发器中

INSERT INTO logtable( msg ) VALUES ( 'trigger fired' );

Then, try your insert again. 然后,再次尝试插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM