Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Carlos.

Asked: November 06, 2023 - 12:07 pm UTC

Last updated: November 13, 2023 - 3:17 pm UTC

Version: 19

Viewed 1000+ times

You Asked

in a query to a table or to a view can we identify if a column is being used ? and based on that change the where clause ?

and Chris said...

You can find references to a table in all the recently run SQL with a query like:

select * from v$sql
where  upper ( sql_text ) like '%TABLE_NAME%'
and    upper ( sql_text ) like '%COLUMN_NAME%';


And you can find where it's used in PL/SQL code with a query like:

select * from dba_statements
where  text like '%TABLE_NAME%'
and    text like '%COLUMN_NAME%';


Note - you need to be on 12.2 or higher and have compiled the code with these settings first:

ALTER SESSION SET plscope_settings='identifiers:all, statements:all'


These will give false positives - statements that include the table and column name but reference a different table/column or just have it in comments. It also won't show you references if the column is accessed via a view. You can find views that use it with:

select * from dba_views
where  upper ( text_vc ) like '%TABLE_NAME%'
and    upper ( text_vc ) like '%COLUMN_NAME%';


These methods will also overlook rarely run instances in application code, scripts, etc.

and based on that change the where clause ?

Change how and why?

Rating

  (1 rating)

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

Comments

Carlos Santana, November 06, 2023 - 2:28 pm UTC

imagine the table <T> like

t1 t2 t3
a null 1
b null 1

and there are queries done in places that we cannot change
like select * from T where t1='a'


and now someone change the values in the table to be lik e

t1 t2 t3
a null 1
a null 2
a null 3
b null 1
b null 2
b null 3


we want to have the queries that do not use t3 in the where clause to return just one entry



Chris Saxon
November 13, 2023 - 3:17 pm UTC

Why do you want it to return just one row? Is it valid to have many rows for each t1, t2 pair?

If not, why not stop the problem at the source with a unique constraint?

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.