Search for specific values in any table and column in Oracle database
You need to find a specific value in a large number of tables and fields in an Oracle database. Although you have limited knowledge of the origin of the data, the value "1/22/2008P09RR8" remains unchanged.
Find candidate columns
Although your initial query attempted to identify candidate columns based on the expected naming pattern "%DTN%", this was not successful. Instead, the following query provides a more accurate search:
<code class="language-sql">SELECT owner, table_name, column_name FROM all_tab_columns WHERE column_name LIKE '%DTN%';</code>
However, if "DTN" is just a speculation, this approach may not produce the expected results.
Search all columns
Assuming that the value is indeed stored in a column, you can systematically search every column in each table using:
Method 1: Dynamic SQL
Using PL/SQL and Dynamic SQL, you can generate and execute SQL queries at runtime. The following code snippet achieves this:
<code class="language-sql">SET SERVEROUTPUT ON SIZE 100000 DECLARE match_count INTEGER; BEGIN FOR t IN (SELECT owner, table_name, column_name FROM all_tab_columns WHERE owner != 'SYS' and data_type LIKE '%CHAR%') LOOP EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name || ' WHERE '||t.column_name||' = :1' INTO match_count USING '1/22/2008P09RR8'; IF match_count > 0 THEN dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count ); END IF; END LOOP; END; /</code>
Method 2: Script query
Alternatively, you can generate separate SQL queries for each column in each table. For example, for a table named "table1" the query would look like:
<code class="language-sql">SELECT * FROM table1 WHERE column1 = 'value' OR column2 = 'value' OR column3 = 'value' ... ;</code>
By using these methods, you can carefully search the vast space of Oracle database for the elusive value "1/22/2008P09RR8".
The above is the detailed content of How to Find a Specific Value Across All Tables and Columns in an Oracle Database?. For more information, please follow other related articles on the PHP Chinese website!