1

I've made a trigger in SQL and need him to write an output after inserting a new row in the table. Please see the example:

CREATE OR REPLACE TRIGGER GAS_CODES AFTER
  INSERT ON blablatable
FOR EACH ROW

BEGIN 
insert into blabla2table (...,...,...,...)
values (:new...,...,...,..);
---output:
dbms_output.put_line('New row has been added.');
END;
/

When I compile the trigger, it shows in the Script Output, but if I add a new row into the table, there's nothing.

Mira7
  • 15
  • 1
  • 5
  • compiling the trigger will not show the output of `dbms_output.put_line()` as the code is not executed during compilation. What exactly do you see when you compile it? In order to see any dbms_output you have to run `set serveroutput on` before any statement that uses `dbms_output`. Did you do that before running the insert where you expect that output to show up? –  Oct 15 '18 at 08:44
  • Yes, I´ve done that before. – Mira7 Oct 15 '18 at 09:05
  • 1
    @Mira7 Trigger are generally not used to display any of the messages. Triggers are events that are supposed to occur when any DML operation occurs on the base table. Not sure why you want to write `DBMS_OUT` in a trigger. – XING Oct 15 '18 at 09:18
  • When you say *"there's nothing"* do you mean in `blablatable` (insert target) or `blabla2table` (trigger target)? – APC Oct 15 '18 at 09:25
  • Well, my imagination was, that after adding the row. The trigger will copy the row into other table writes into console something (info for the user). – Mira7 Oct 15 '18 at 11:36
  • @APC I meant nothing in console output. – Mira7 Oct 15 '18 at 11:37

2 Answers2

4

You are missing SET SERVEROUTPUT ON. This command is understandable also by SQLDeveloper.

Let's do a quick test inside the SQLDeveloper.

CREATE USER "TEST_SCHEMA" IDENTIFIED BY "TEST";

User "TEST_SCHEMA" created.

GRANT UNLIMITED TABLESPACE TO "TEST_SCHEMA";

Grant succeeded.

CREATE TABLE "TEST_SCHEMA"."NAMES" ("ID" NUMBER, "NAME" VARCHAR2(25), PRIMARY KEY("ID")); 

Table "TEST_SCHEMA"."NAMES" created.

CREATE OR REPLACE TRIGGER "TEST_SCHEMA"."NAMES_TRG_1" AFTER
  INSERT ON "TEST_SCHEMA"."NAMES"
  FOR EACH ROW
  BEGIN
    DBMS_OUTPUT.PUT_LINE('New row has been added.');
  END;
/

Trigger NAMES_TRG_1 compiled

SET SERVEROUTPUT ON

This command won't print anything in SQL Developer. No worries here.

INSERT INTO "TEST_SCHEMA"."NAMES" VALUES (1, 'Mark Smith');

1 row inserted.

New row has been added.

As you can see, the output was there and it was inserted after the actual row was inserted into the table. Works fine.

To cleanup the testcase, run this:

DROP USER "TEST_SCHEMA" CASCADE;

enter image description here

EDIT 1:

When you are working with Table Data Editor, this is behaving differently. Table Data Editor has its own Oracle session and it has different way of capturing DBMS Output.

To open the DBMS capture window, you need to click on "VIEW" menu and select "DBMS Output" option.

DBMS Output option

Then click the green plus button and set the database, that will be captured.

DBMS Output window

Now you can see the output.

Beware as the output here is not "realtime", this window will show something only when there is a buffer flush, and the buffer flush cannot be invoked manually/directly.

Miroslav Duník
  • 580
  • 2
  • 9
  • Damn....I ´ve made everything as same as you, but I can not see this. I repeat that I have written set SERVEROUTPUT ON. In My trigger there is only begin insert row to the other table,dbms_output.pu_line('something') and end. Thats all. If I compile the code, I can see Trigger compiled, my dbms output. But when I add row, I can see only that insert and commit successful. – Mira7 Oct 15 '18 at 12:16
  • Could be the issue, when I insert the row in the table instead of command? – Mira7 Oct 15 '18 at 12:21
  • @Mira7 what version database? if it's older, dbms_output may not get captured with newer copies of SQL Developer – thatjeffsmith Oct 15 '18 at 12:32
  • It is actually the issue. Try to add those values manually in the table. Does it work anyway? – Mira7 Oct 15 '18 at 12:33
  • Sorry, I missunderstood your question. When you are inserting rows inside "Table Editor" (in "Data" tab), this is different. It has its own Oracle session and the DBMS_OUTPUT is being captured differently. You can open VIEW menu and select "DBMS Output". Beware, that the messages here are NOT shown immediatelly, but only after output buffer flush, which cannot be invoked manually. – Miroslav Duník Oct 15 '18 at 12:58
  • @Mira7 if you are changing it in grid, there is no panel for output data and this message will be ignored. But trigger is working. – mmp Oct 15 '18 at 13:00
  • Is there any way how to see it in "Messages - Log"? – Mira7 Oct 15 '18 at 13:35
  • No. (As far as I know). – Miroslav Duník Oct 15 '18 at 13:41
0

Most likely the client (SQLDeveloper) doesn't read the output buffer. To enable this you must choose from menu "view" -> "dbms output" and then click the green "+" in the dbms output window to read the output buffer for your connection ...

In sqlplus you can do it like this:

SQL> drop table tst purge; 

Table dropped.

SQL> drop table tst2 purge; 

Table dropped.

SQL> create table tst ( tst_no integer); 

Table created.

SQL> create table tst2 ( tst_no integer); 

Table created.

SQL> create or replace trigger tst_trg after insert on tst 
for each row 
begin
insert into tst2 (tst_no) values (:new.tst_no); 
dbms_output.put_line('new row with tst_no='|| :new.tst_no); 
end; 
/  2    3    4    5    6    7  

Trigger created.

SQL> set serveroutput on; 
exec dbms_output.enable; 

insert into tst values (1); SQL> 
PL/SQL procedure successfully completed.

SQL> SQL> 
new row with tst_no=1

1 row created.

SQL> r
  1* insert into tst values (1)
new row with tst_no=1

1 row created.

SQL> select * from tst2; 

    TST_NO
----------
     1
     1

SQL> 

as you can see the output is read and printed in sqlplus, and rows are inserted into the target table tst2

hope it helps...

  • @APC "When I compile the trigger, it shows in the Script Output" - what shows? Still think that the most likely problem here is that reading from the output buffer is not enabled. – Kristian Saksen Oct 15 '18 at 09:34
  • @APC: yes it is - and as stated by Miroslav Duník in this case - getting it to print the output buffer is is enabled by issuing "set serveroutput on" - when Mira7 is saying "When I compile the trigger, it shows in the Script Output" I guess the text is "Trigger GAS_CODES compiled" - that text will show even if serveroutput is not set on - text from the outputbuffer requires serveroutput to be set on to be shown in script output – Kristian Saksen Oct 15 '18 at 09:43
  • Just to be clear - If I compile the trigger, I can see the dbms_out output. But that is not what I want to see. I would like to see this dbms out everytime the new row is added. – Mira7 Oct 15 '18 at 11:40
  • 1
    @Mira7 - do you see the rows in table "blabla2table"? To see the content from dbms_output you must be executing the inserts from the same session thats reading the output buffer. – Kristian Saksen Oct 15 '18 at 11:48
  • @mira7 - DBMS_OUTPUT is really just a convenience for developers. If you want to see it every time in Oracle SQL Developer you can set the configuration so `serveroutput` is enabled by default. But that won't help you in other embodiments. – APC Oct 15 '18 at 11:58
  • If I execute the insert command from the worksheet, I can see the output, however, if I insert it in the table, there is no output. – Mira7 Oct 15 '18 at 12:32