Good day folks,
Trying to wrap my head around this and manage to get it working to some extent - but still struggling a bit.
I want to find two values in two columns for this example: VARCHAR and DATE JOHN 1984-01-01
Let's say this is John's birth date.
I want to be able to find the value JOHN with the DATE value in lets say 2000 tables and to complicate things the column names are not all the same. The data types are.
The following selects all the tables with the two column names which I require.
SELECT A.TABLE_NAME, A.COLUMN_NAME, B.COLUMN_NAME
FROM all_tab_columns A
JOIN all_tab_columns B
ON A.TABLE_NAME = B.TABLE_NAME
AND B.DATA_TYPE = 'DATE'
AND A.COLUMN_NAME IN ('NAME', 'FULLNAME')
So, now I get the tables I need which has the data type DATE where the column names NAME and FULLNAME exists.
So now of the 2000 tables I have 300 that match my criteria. I want to search through the tables found for JOHN with the date 1984-01-01 where the JOHN could be in FULLNAME or NAME and the 1984-01-01 could be in any column name ie. DTTM, BDAY,DATEFLD,DTFIELD etc.
I want the TABLE_NAME where these two values exists in the same row, nothing else.
I have looked at these type of examples:
Search All Fields In All Tables For A Specific Value (Oracle)
But keep getting issues. What am I missing?
DECLARE
match_count INTEGER;
v_search_string VARCHAR2 (11) := 'JOHN';
BEGIN
FOR t
IN (SELECT A.owner, A.table_name, A.column_name
FROM all_tab_columns A
JOIN all_tab_columns B
ON A.TABLE_NAME = B.TABLE_NAME
AND A.COLUMN_NAME IN ('NAME', 'FULLNAME')
AND B.DATA_TYPE = 'DATE'
AND A.TABLE_NAME LIKE 'DATA%')
LOOP
BEGIN
EXECUTE IMMEDIATE
'SELECT * FROM '
|| t.owner
|| '.'
|| t.table_name
|| ' WHERE '
|| t.column_name
|| ' = :1 '
INTO match_count
USING v_search_string;
IF match_count > 0
THEN
DBMS_OUTPUT.put_line (
t.table_name
);
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (
'Error encountered trying to read '
|| t.column_name
|| ' from '
|| t.owner
|| '.'
|| t.table_name);
END;
END LOOP;
END;
/