If I run the SQL in Fig. 1 below, it may return something like this:
Select fname, lname from name_tbl where nam_key = :key
Without using some fancy DBA trace utility, how can I query an Oracle system table to find the value of the bind variable “:key”?
Figure 1. - List the current running sql statement.
select sid, username, sql_text
from v$session,
v$sqltext
where sql_address = address
and sql_hash_value = hash_value
order by sid, piece;
select name, value_string
from v$sql_bind_capture
where sql_id = your_query_id
Upd. or, of course:
select sql_id, value_string
from v$sql_bind_capture
where name = ':key'
Related
I am trying to get count of all rows from views in oracle schema and my code is working fine. But when i try to add a condition like where actv_ind = 'Y', i am unable to get it working. Any suggestions on how to modify the code to make it working?
SELECT view_name,
TO_NUMBER(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) cnt from '||owner||'.'||view_name||
'where'||view_name||'.'||'actv_ind= Y')),'/ROWSET/ROW/CNT')) as VIEW_CNT
FROM all_views
WHERE owner = 'ABC' AND view_name not in ('LINK$')
I am getting the error ACTV_IND : Invalid Identifier.
The error messages from DBMS_XMLGEN are not very helpful so you need to be very careful with the syntax of the SQL statement. Add a space before and after the where, and add quotation marks around the Y:
SELECT view_name,
TO_NUMBER(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) cnt from '||owner||'.'||view_name||
' where '||view_name||'.'||'actv_ind= ''Y''')),'/ROWSET/ROW/CNT')) as VIEW_CNT
FROM all_views
WHERE owner = 'ABC' AND view_name not in ('LINK$');
The query still assumes that every view contains the column ACTV_IND. If that is not true, you might want to base the query off of DBA_TAB_COLUMNS WHERE COLUMN_NAME = 'ACTV_IND'.
Here's a simple sample schema I used for testing:
create user abc identified by abc;
create or replace view abc.view1 as select 1 id, 'Y' actv_ind from dual;
create or replace view abc.view2 as select 2 id, 'N' actv_ind from dual;
How do I return data out of IF statements? I have a IF statement which is meant to return a different result dependent of the result of that statement.
IF :Value = 1 THEN
SELECT Name FROM TABLE_A
ELSEIF :Value = 2 THEN
SELECT Name FROM TABLE_B
ELSEIF :Value = 3 THEN
SELECT Name FROM TABLE_C
but this doesn't work. It expects an INTO statement in those selects. I suspect this is because Oracle can't return out of a block?
Is there a quicker way to return those select statements without creating table variables to store the data or messing around with functions?
You can do this by plain SQL:
SELECT
':Value' user_input,
CASE
WHEN ':Value' IN('a1','a2','a3')
THEN (select name from table_a)
WHEN ':Value' = 'i'
THEN (select name from table_b)
END AS output
FROM
dual
(good info about case)
If you want more than one result in your cases, then you may opt to an intelligent UNION:
SELECT t1_Col_1, t1_Col_2,'&VALUE' from TABLE_1
WHERE '&VALUE' = '1'
UNION ALL
SELECT t2_Col_1, t2_Col_2,'&VALUE' from TABLE_2
WHERE '&VALUE' = '2'
In this solution, types and number of tx_Coln must be the same.
I have the sql id of a query. Now I want to get the machine name and IP of the machine from which this query was run.
I have already checked the sql id in V$SESSION and V$ACTIVE_SESSION_HISTORY but didn't get any results.
I am able to find the sql id in v$sql and LAST_LOAD_TIME is today(7 July 2016).
select LAST_LOAD_TIME from v$sql where SQL_ID='0jf4618m2u7aw' order by LAST_LOAD_TIME desc;
2016-07-04/17:26:02
2016-07-04/17:26:02
select * from V$SESSION where SQL_ID='0jf4618m2u7aw';
no rows selected
select * from V$ACTIVE_SESSION_HISTORY where SQL_ID='0jf4618m2u7aw';
no rows selected
Please help. Thanks in advance.
You have a couple of options...
If it is an active session/process, you can use v$session and v$process:
SELECT DISTINCT
MACHINE,
UTL_INADDR.GET_HOST_ADDRESS(MACHINE) AS IP_ADDR
FROM V$SESSION S,
V$PROCESS P
WHERE S.PADDR = P.ADDR
AND S.SQL_ID = '0jf4618m2u7aw';
If you want a historical view, the DBA_HIST_ACTIVE_SESS_HISTORY might be your friend:
SELECT DISTINCT
MACHINE,
UTL_INADDR.GET_HOST_ADDRESS(MACHINE) AS IP_ADDR
FROM DBA_HIST_ACTIVE_SESS_HISTORY DHASH
WHERE DHASH.SQL_ID = '0jf4618m2u7aw'
AND DHASH.SQL_EXEC_START > TRUNC(SYSDATE);
I have the following query in oracle. I want to convert it to PostgreSQL form. Could someone help me out in this,
SELECT user_id, user_name, reports_to, position
FROM pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;
A something like this should work for you (SQL Fiddle):
WITH RECURSIVE q AS (
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
WHERE po.reports_to = 'dpercival'
UNION ALL
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
JOIN q ON q.user_id=po.reports_to
)
SELECT * FROM q;
You can read more on recursive CTE's in the docs.
Note: your design looks strange -- reports_to contains string literals, yet it is being comapred with user_id which typicaly is of type integer.
I'm trying to understand the Oracle plan_table and ran few SQL statements to populate the plan_table...From the statements generated in the plan_table, How can I identify the order in which the statements are executed.
Selecting directly from the PLAN_TABLE is somewhat "deprecated". At least it's absolutely unnecessary nowadays. You can use dbms_xplan to view the execution plan of an explained statement:
explain plan for
select *
from your_table;;
select *
from table(dbms_xplan.display);
More details in the manual: http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_xplan.htm#CACICEDJ
The manual also contains an example (hierarchical) SELECT statement to retrieve the contents from the PLAN_TABLE directly:
SELECT id, LPAD(' ',2*(LEVEL-1))||operation operation, options,
object_name, object_alias, qblock_name, position
FROM plan_table
START WITH id = 0 AND statement_id = 'xxxxx'
CONNECT BY PRIOR id = parent_id AND statement_id = 'xxxxx'
ORDER BY id;
The above is taken from: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9010.htm#sthref5965
You need to replace 'xxxx' with the statement_id you are using (which requires a set statement_id in the explain plan statement)