Showing posts with label sql_text. Show all posts
Showing posts with label sql_text. Show all posts

Wednesday, March 27, 2013

V$SQL_BIND_CAPTURE

This simple example, tested on Oracle 11.2, shows how to use V$SQL_BIND_CAPTURE to see the value of bind variables used in a WHERE clause. First I created a table called TAB1 with 1 row of data: 

SQL> create table tab1 (col1 varchar2(10))
  2  /
 
Table created.
 
SQL> insert into tab1 values ('ANDREW')
  2  /
 
1 row created.
 
SQL> select * from tab1
  2  /
 
COL1
----------
ANDREW
 
SQL>

Then I created a bind variable called VAR1. I used this to select the row of data with COL1 set to ANDREW then change it to BRIAN:

SQL> variable var1 varchar2(10)
SQL> begin
  2  select 'ANDREW' into :var1 from dual;
  3  update tab1 set col1 = 'BRIAN' where col1 = :var1;
  4  end;
  5  /
 
PL/SQL procedure successfully completed.
 
SQL>

I checked that the update had worked:

SQL> select * from tab1
  2  /
 
COL1
----------
BRIAN
 
SQL> 

Then I looked in V$SQL_BIND_CAPTURE to see the value of the bind variable like this: 

SQL> select
  2  a.sql_fulltext, b.name, b.value_string
  3  from v$sql a, v$sql_bind_capture b
  4  where a.sql_id = b.sql_id
  5  and a.child_address = b.child_address
  6  and sql_text like 'UPDATE TAB1%'
  7  /
 
SQL_FULLTEXT
-------------------------------------------------------
NAME
------------------------------
VALUE_STRING
-------------------------------------------------------
UPDATE TAB1 SET COL1 = 'BRIAN' WHERE COL1 = :B1
:B1
ANDREW
 
SQL> 

N.B. This only seems to work for bind variables in WHERE clauses. I also tried to use it to look at the bind variable in the SQL below: 

update tab1 set col1 = :var1 

In this case, the value of the bind variable was not shown.

Friday, November 04, 2011

How to See Another User's SQL

If one user runs some SQL:
 
SQL> show user
USER is "ORACLE"
SQL> l
  1  select to_char(sysdate,'HH24:MI:SS')
  2* Time_Now from dual
SQL> /
 
TIME_NOW
--------
16:04:40
 
SQL>
 
Another user can see it as follows:
 
SQL> show user
USER is "SYS"
SQL> l
  1  SELECT SQL_TEXT FROM V$SQL SQL, V$SESSION SES
  2  WHERE SQL.ADDRESS    = SES.SQL_ADDRESS
  3  AND   SQL.HASH_VALUE = SES.SQL_HASH_VALUE
  4* AND   USERNAME       = 'ORACLE'
SQL> /
 
SQL_TEXT
------------------------------------------------------------
select to_char(sysdate,'HH24:MI:SS') Time_Now from dual
 
SQL>
 
(The script depends on an entry in V$SESSION so the original user still needs to be logged in for it to work.)

Thursday, October 06, 2011

V$SESSION_LONGOPS

You can query long running SQL in V$SESSION_LONGOPS.
 
In the example shown, the SQL is a simple delete statement so I have managed to shorten the output by using a col sql_text format a20. Normally it is much longer.
 
Each time you run the SQL, the figures are recalculated. The elapsed_seconds column, which I have renamed as time_taken, should increase every time the SQL is rerun.
 
The time_remaining column, which I have renamed as time_left, is only an estimate. Normally it goes down each time the SQL is rerun but sometimes it goes up.
 
Once the SQL is finished, the executions column changes to 1 and the time_remaining column goes to 0:
 
  1  SELECT SQL_TEXT, EXECUTIONS,
  2  ELAPSED_SECONDS TIME_TAKEN,
  3  TIME_REMAINING TIME_LEFT
  4  FROM V$SESSION SES, V$SQL SQL,
  5  V$SESSION_LONGOPS LONGOPS
  6  WHERE SES.USERNAME       = 'BRAID'
  7  AND   SES.SQL_ADDRESS    = SQL.ADDRESS
  8  AND   SES.SQL_HASH_VALUE = SQL.HASH_VALUE
  9  AND   SQL.ADDRESS        = LONGOPS.SQL_ADDRESS
 10* AND   SQL.HASH_VALUE     = LONGOPS.SQL_HASH_VALUE
SQL> /
 
SQL_TEXT             EXECUTIONS TIME_TAKEN  TIME_LEFT
-------------------- ---------- ---------- ----------
delete b_alp                  0        204       1010
 
SQL> /
 
SQL_TEXT             EXECUTIONS TIME_TAKEN  TIME_LEFT
-------------------- ---------- ---------- ----------
delete b_alp                  0        710        507
 
SQL> /
 
SQL_TEXT             EXECUTIONS TIME_TAKEN  TIME_LEFT
-------------------- ---------- ---------- ----------
delete b_alp                  1       1220          0
 
SQL>