I need to see all queries coming to database. How to do that? I could not get proper results from a Google search.
-
Why the '-1'? This is a valid question. – rustyx Apr 17 '13 at 08:39
3 Answers
Enable SQL Trace & all the queries coming to the database will be logged.
ALTER SESSION SET sql_trace = true;
ALTER SESSION SET tracefile_identifier = mysqltrace;
The trace file will be present in the udump directory.
If you want to audit the database, look at my previous answer.

- 1
- 1

- 21,321
- 22
- 95
- 134
-
2I believe that should be `ALTER SYSTEM SET sql_trace = true SCOPE=MEMORY` to log all activity against the database. OP should also get familiar with TKProf to format the trace output. – Bob Jarvis - Слава Україні Feb 03 '12 at 16:44
-
2
-
I cannot find the `udump` directory. Could you be a bit more descriptive? Thanks! – Prasannjeet Singh Nov 15 '21 at 14:47
If you need to see all queries from all sessions for a SHORT window of time and you need a really simple solution, this is what I do. (The above answers will only show you SQL being run in one session, this gives all SQL across all sessions, easily.)
1). Create a temp table to store all the retrieved SQL:
-- Fabien pointed out out that 'port may be inaccessible on 10.2
CREATE TABLE "MIKE"."TMP"
( "LOOP_NO" NUMBER(10,0),
"SID" NUMBER,
"SERIAL#" NUMBER,
"PROCESS" VARCHAR2(24 BYTE),
"PROGRAM" VARCHAR2(48 BYTE),
"MODULE" VARCHAR2(64 BYTE),
"OSUSER" VARCHAR2(30 BYTE),
"SCHEMANAME" VARCHAR2(30 BYTE),
"ACTION" VARCHAR2(64 BYTE),
"MACHINE" VARCHAR2(64 BYTE),
"TERMINAL" VARCHAR2(30 BYTE),
"ADDRESS" RAW(8),
"PIECE" NUMBER,
"SQL_TEXT" VARCHAR2(4000)
)
2). Run a nasty polling loop in an anonymous block to gather all SQL run on the system, as long as the block is running:
declare
begin
for j in 1.. 1000 loop
insert into mike.tmp
SELECT j, b.sid, b.serial#, b.process, b.program, b.module, b.osuser, b.schemaname, b.action, b.machine, b.terminal,a.address, a.piece, a.sql_text
FROM V$sqltext_With_Newlines a
join V$Session b on a.address = b.sql_address
WHERE A.ADDRESS NOT IN (select address FROM mike.tmp)
ORDER BY b.sid, a.piece;
commit;
end loop;
end;
3). Query to retrieve SQL:
select distinct osuser, a.address, a.sid, a.piece, a.sql_text
from mike.tmp a
join (select loop_no, sid from mike.tmp where sql_text like '%anytexthere%') b
on a.loop_no = b.loop_no
and a.sid = b.sid
order by a.sid, a.address, a.piece
... please mind that this is just a quick way to trap SQL when you are in a "what the heck is going on here?" situation, and you DO NOT have GUI Tools, and you DO NOT have file access to USER_DUMP_DEST
.

- 2,149
- 20
- 29
-
1Watch, you may not have access to column PORT in V$SESSION ; example : on Oracle 10.2 you don't. – Fabien Haddadi Jan 30 '18 at 12:02
-
2Also, you really want to expand that SQL_TEXT column to much bigger than 64 bytes... Often, yo uneed the full SQL text to make up your mind about what's going on, and have a chance to spot what piece of code sends that query... – Fabien Haddadi Jan 31 '18 at 01:33
-
@ Fabien Haddadi -- Thank you much, I edited the answer to reflect both of your suggestions. – mike Apr 11 '18 at 18:28
-
The table 'V$sqltext_With_Newlines' and its column sql_text is of type varchar of size 64 so you can't log more than the first 64 characters of a query, correct me if I'm wrong. – user3157855 May 28 '19 at 09:44
-
Running XE in Windows here is how I do it to find what user is doing. Start up SQLPlus and run:
> SELECT USERNAME, SID, SERIAL# FROM v$session WHERE userName = '<your user>'
This gets you two integer values, SID and SERIAL# for your user. Your user may have more than one session open. Run this to turn on logging:
> execute dbms_system.set_sql_trace_in_session(<SID>, <SERIAL#>, true)
Next have your app do some work... Find out where the data went using:
> SHOW PARAMETERS user_dump_dest
And you'll get something like: C:\oraclexe\app\oracle\diag\rdbms\xe\xe\trace where you will find a number of trace logs. The .trc files are simply text
When done, turn off the logging so you don't fill up files or slow your db down.
> execute dbms_system.set_sql_trace_in_session(<SID>, <SERIAL#>, false)
There you go - happy diagnosing and reverse engineering!

- 8,912
- 15
- 68
- 107
-
`ORA-06550: line 1, column 7: PLS-00201: identifier 'DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION' must be declared` on Oracle Database 11g Release 11.2.0.4.0 – user323094 Aug 01 '19 at 06:39