SERVEROUTPUT shows results one after another execution - oracle

When I execute the following trigger code it is compiled. Then after inserting data, serveroutput says '1 row inserted' trigger result ('User Example has insrted a data') didn't displayed.
Then after execution of another PL/SQL the previous trigger result will appears on display. What should i do to solve this issue?
Here is the trigger and the insertion command and also the results,
/
SET SERVEROUTPUT ON;
CREATE OR REPLACE TRIGGER BTR_SUPERHEROS
BEFORE INSERT ON SUPERHEROS
FOR EACH ROW
ENABLE
DECLARE
V_USER VARCHAR2(30);
BEGIN
SELECT USER INTO V_USER FROM DUAL;
DBMS_OUTPUT.ENABLE();
DBMS_OUTPUT.PUT_LINE('USER:' ||V_USER||' HAS INSERTED A DATA');
END;
/
RESULT - Trigger BTR_SUPERHEROS compiled
INSERT INTO SUPERHEROS VALUES('SUPERMAN');
RESULT - 1 row inserted. (Didn't Shows the DBMS_OUTPUT)

You need to set serveroutput on in the client application (SQL*Plus, SQL Developer etc) in the session where you run a statement, otherwise it won't know that you want it fetch and display the output buffer when the statement completes. The set server output on prior to creating the trigger is just a client setting and doesn't set it permanently for the trigger.
Please confirm whether your test case was run from a single session, like this:
SQL*Plus: Release 12.1.0.1.0 Production on Tue Aug 15 08:17:48 2017
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Last Successful login time: Tue Aug 15 2017 08:14:08 +01:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
SQL> SET SERVEROUTPUT ON;
SQL>
SQL> CREATE OR REPLACE TRIGGER BTR_SUPERHEROS
2 BEFORE INSERT ON SUPERHEROS
3 FOR EACH ROW
4 ENABLE
5
6 DECLARE
7 V_USER VARCHAR2(30);
8 BEGIN
9 SELECT USER INTO V_USER FROM DUAL;
10 DBMS_OUTPUT.ENABLE();
11 DBMS_OUTPUT.PUT_LINE('USER:' ||V_USER||' HAS INSERTED A DATA');
12 END;
13 /
Trigger created.
SQL> INSERT INTO SUPERHEROS VALUES('SUPERMAN');
USER:WILLIAM HAS INSERTED A DATA
1 row created.
Note also that it is not recommended to call dbms_output.enable. From the documentation:
You should generally avoid having application code invoke either the DISABLE Procedure or ENABLE Procedure because this could subvert the attempt of an external tool like SQL*Plus to control whether or not to display output.
You can also assign values using the assignment := operator instead of a select into statement. The trigger could therefore be written as:
create or replace trigger btr_superheros
before insert on superheros
for each row
enable
declare
v_user user_users.username%type := sys_context('userenv', 'current_schema');
begin
dbms_output.put_line('User: ' ||v_user||' inserted value '''||:new.name||'''');
end;
/

Related

Control order of database logon trigger execution in Oracle

We have two "AFTER LOGON ON DATABASE" triggers, each owned by different DBA-owned schemas and serving different purposes. Trigger A is consistently getting executed prior to trigger B. We need to reverse that and ensure that trigger B fires first.
According to the documentation, we supposedly could use "PRECEDES ..." syntax to control this, but when used with a database trigger it throws ORA-25025: cannot specify PRECEDES clause, so that's a dead-end.
Any ideas how to control which of these AFTER LOGON triggers fires first?
Does anybody know if dbms_ddl.set_trigger_firing_property can do this with some secret property value?
PRECEDES is about editioning triggers. Check out FOLLOWS
SQL> create or replace
2 trigger trg1
3 after logon on scott.schema
4 begin
5 null;
6 end;
7 /
Trigger created.
SQL>
SQL>
SQL> create or replace
2 trigger trg2
3 after logon on scott.schema
4 follows trg1
5 begin
6 null;
7 end;
8 /
Trigger created.
SQL>
or at database level
SQL> create or replace
2 trigger trg1
3 after logon on database
4 begin
5 null;
6 end;
7 /
Trigger created.
SQL>
SQL>
SQL> create or replace
2 trigger trg2
3 after logon on database
4 follows trg1
5 begin
6 null;
7 end;
8 /
Trigger created.

Oracle configuration. "create or replace" function create invalid object

I recently moved my database to a new server (both Oracle 11g).
In the new host, there is a weird behavior, that whenever I run something like:
create or replace test_trigger
before insert or update
on test_table
for each row
begin
select 1 from dual;
end;
/
The trigger created is invalid without any error in log. I tried to recompile, it still invalid.
It only happen with the function "create or replace". If I drop the trigger and re-create again, it would be valid.
My question is, did I config something incorrectly? How can I check it? Thank you.
Code you posted won't compile, not in any Oracle database I know. Why? Wrong syntax.
Here's a demonstration:
SQL> create or replace test_trigger
2 before insert or update
3 on test_table
4 for each row
5 begin
6 select 1 from dual;
create or replace test_trigger
*
ERROR at line 1:
ORA-00922: missing or invalid option
SQL> end;
SP2-0042: unknown command "end" - rest of line ignored.
SQL> /
So, what's wrong with it?
create (or replace) wants to know what you're going to create. "test_trigger"? As far as Oracle is concerned, that could be "mickey_mouse" and the result will be the same. It is the trigger keyword that is missing
SELECT in PL/SQL requires an INTO clause, so that you could store the result into something
in order to be able to do that, you have to declare a variable
Here's code that, actually, compiles:
SQL> create or replace trigger test_trigger --> this
2 before insert or update
3 on test_table
4 for each row
5 declare
6 l_dummy number; --> this
7 begin
8 select 1
9 into l_dummy --> this
10 from dual;
11 end;
12 /
Trigger created.
SQL>
So, it seems that you misinterpret reality.

'Invalid input values for pname' during table statistics gathering?

When I'm trying to gather table statistics using GATHER_TABLE_STATS procedure, I'm getting the following error:
ORA-20001: Invalid input values for pname
ORA-06512: at "SYS.DBMS_STATS", line 31513
ORA-06512: at line 2
The code I'm running to gather statistics is
BEGIN
DBMS_STATS.gather_table_stats ('OWNER', 'TABLE_NAME');
END;
/
My Oracle version is Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
I guess you might have misspelled OWNER or TABLE_NAME parameter. Its working fine for me as shown below.
SQL> conn jay/jay
Connected.
SQL> select table_name from tabs;
TABLE_NAME
------------------------------
ROAD
EVENT
ALL_OBS
ACCOUNT
NVARCHAR2_EMAIL
TABLEA
T2
MYTABLE
8 rows selected.
SQL> exec dbms_stats.gather_table_stats('JAY','ROAD');
PL/SQL procedure successfully completed.
Update
As per the My Oracle Support Doc:755577.1, It is possible that the post installation scripts for the patch were not run correctly after a patch was applied.
You may need to reinitialize the DBMS_STATS package using execstat.sql under $ORACLE_HOME/rdbms/admin directory. Or reinstall DBMS_STATS.
Or you might hit the bug- Bug 14479079 : ORA-20001 GATHERING STATS AFTER CPU JULY 2012 PATCH

Oracle procedure inside a trgigger to check the output

I am trying to call my procedure inside a trigger.
The trigger is created successfully but the procedure is calling being called.
I am using oracle 10g with SQL developer tool.
I have tried to see the dbms_output but nothing shows in DBMS_OUTPUT console except"set serveroutput on" when i update the table.
My trigger is
create or replace
TRIGGER trig_sample
AFTER UPDATE
ON sample_table
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
SAMPLE_PROC(:NEW.PARAM1, :NEW.PARAM2 ,:NEW.PRAMA3);
END;
procedure is
CREATE or REPLACE Procedure SAMPLE_PROC(PARAM1 NUMBER,PARAM2 NUMBER,PARAM3 varchar2)
BEGIN
DBMS_OUTPUT.put_line('test : '||test);
UPDATE SAMPLE_TABLE SET STATUS = 'VA'
WHERE SAMPLE_ID = '2012';
END;
In my procedure i have used DBMS_OUTPUT.put_line('test : '||test);
but could not see any output. Please provide any suggestions to view the DBMS output in console and how to check the trigger is called or not?

Running Oracle stored Procedure

I have a sql file that contains a simple procedure to print "Hi" like,
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
When I try to execute this from sql file itself, it just gets compiled and it is not running.
I added the as,
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
/ /* this is required else it throws me compilation error*/
set serveroutput on
EXECUTE skeleton;
I tried calling the same skeleton from other sql file also and even from the sqldeveloper GUI, that also didnt execute this. Only the sqlplus commandline helps me. Let me know what I am missing and the solution for this.
Here are the steps I took using SQL Plus
SQL> CREATE OR REPLACE PROCEDURE skeleton
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE('Hi');
5 END;
6 /
Procedure created.
SQL> set serveroutput on
SQL> EXECUTE skeleton;
Hi
PL/SQL procedure successfully completed.
SQL>
Can you start a new sqlplus session replicate these steps and post the content?
The only change I had to make to your sql to allow running it as an #file was to remove the comment. This is the whole content of the the .sql file:
CREATE OR REPLACE PROCEDURE skeleton
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi');
END;
/
set serveroutput on
EXECUTE skeleton;
You should get an output something like this:
C:\Temp>sqlplus username/password #skeleton.sql
SQL*Plus: Release 11.1.0.6.0 - Production on Mon Oct 5 17:10:46 2009
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Procedure created.
Hi
PL/SQL procedure successfully completed.
SQL>
Try setting set serveroutput on before the dbms_output.
In SQL Developer, you need the "execute script" button which is the second button at the top left (little green arrow in front of document) instead of the one with the big green arrow.
That threw me the first time I used it.
(source: devshed.com)
The reason your first example gets compiled and not run, is that you are not actually asking it to run. You are creating a procedure inside the database, which works correctly, but not calling it. In your second block you have an EXECUTE which is where it actually runs.

Resources