Skip to Main Content
  • Questions
  • How can I track failing sql statements

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Livio.

Asked: December 12, 2008 - 9:58 am UTC

Last updated: October 25, 2010 - 10:39 am UTC

Version: 9.2.0

Viewed 10K+ times! This question is

You Asked

Hello Tom;

I am running a third-party application which returns the error ORA-00942 table or view does not exist. Application returns immediately upon returning the error.

What I want to do is to find out the sql statement that caused the error.
I've been trying a script you posted in another thread, namely
---------------- showsql.sql --------------------------
column status format a10
set feedback off
set serveroutput on

select username, sid, serial#, process, status
from v$session
where username is not null
/

column username format a20
column sql_text format a55 word_wrapped

set serveroutput on size 1000000
declare
    x number;
begin
    for x in
    ( select username||'('||sid||','||serial#||
                ') ospid = ' ||  process ||
                ' program = ' || program username,
             to_char(LOGON_TIME,' Day HH24:MI') logon_time,
             to_char(sysdate,' Day HH24:MI') current_time,
             sql_address, LAST_CALL_ET
        from v$session
       where status = status
         and rawtohex(sql_address) <> '00'
         and username is not null order by last_call_et )
    loop
        for y in ( select max(decode(piece,0,sql_text,null)) ||
                          max(decode(piece,1,sql_text,null)) ||
                          max(decode(piece,2,sql_text,null)) ||
                          max(decode(piece,3,sql_text,null))
                               sql_text
                     from v$sqltext_with_newlines
                    where address = x.sql_address
                      and piece < 4)
        loop
            if ( y.sql_text not like '%listener.get_cmd%' and
                 y.sql_text not like '%RAWTOHEX(SQL_ADDRESS)%')
            then
                dbms_output.put_line( '--------------------' );
                dbms_output.put_line( x.username );
                dbms_output.put_line( x.logon_time || ' ' ||
                                      x.current_time||
                                      ' last et = ' ||
                                      x.LAST_CALL_ET);
                dbms_output.put_line(
                          substr( y.sql_text, 1, 250 ) );
            end if;
        end loop;
    end loop;
end;
/

column username format a15 word_wrapped
column module format a15 word_wrapped
column action format a15 word_wrapped
column client_info format a30 word_wrapped

select username||'('||sid||','||serial#||')' username,
       module,
       action,
       client_info
from v$session
where module||action||client_info is not null;


but I haven't been successful until now. I suppose that's due to the fact the application exists just after the error has been raised, and so does the database session it opens.

Well is there another way to get the failing statement, maybe by tweaking the above procedure?
Thank you in advance.

Livio

and Tom said...

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

[RE] How can I track failing sql statements

Livio, December 12, 2008 - 11:27 am UTC

Exactly what I was looking for! I got the statement.
Thank you again.

Like always thanks

A reader, October 19, 2010 - 1:39 pm UTC

greeting Thomas,

And thanks like always, can the method in the above link capture bind variables values?
Tom Kyte
October 25, 2010 - 10:39 am UTC

you might be able to get some bind metadata depending on version - but in general "no".


the binds would be tracked with fine grained auditing.

You can get some bind metadata using ASH information.

but in general, it won't be 100% reliable (not everything is recorded, it is sampled)

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library