0

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). 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. Where is the problem?

4
  • 3
    From my point of view there is no point to use a trigger to print something when it’s triggered. To do something more feasible, why dont you try to insert something in some table and you see if it works? Commented May 5, 2018 at 11:13
  • 1
    Your problem is that DBMS_OUTPUT.PUT_LINE is aimed to write to console. So, you need to have the trigger making some change that could be checked without using the console. You can, for instance, send an e-mail to yourself (just to test that the trigger is fired, not as the normal functionality of the trigger). Commented May 5, 2018 at 11:34
  • I completely agree with @Jaime Drq and @FDavidov. Besides, if you really want to see on the console, then you need to issue set serveroutput on command without need of begin .. end unless you call from an external application. Commented May 5, 2018 at 11:52
  • I know, that there is no point of the code, this is an assignment for my class. The task is to create any trigger, and I choose to print the name and date of the registration. Commented May 5, 2018 at 12:45

1 Answer 1

1

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.
  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));

Next, add this to your trigger,

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

Then, try your insert again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.