1

I want to query the column_name=object_no in the multiple tables ( up to 100 tables). I'm using Oracle SQL Developer.

To shows the tables that do not have column_name=object_no, I tried with the SQL query below.

SELECT TABLE_NAME
FROM USER_TAB_COLUMNS
WHERE NOT COLUMN_NAME LIKE '%OBJECT_NO%'
ORDER BY COLUMN_ID;

However, it show the same table multiple times and mixed up with the table that also have COLUMN_ID='OBJECT_NO'.

3 Answers 3

1

Because user_tab_columns has table_name and column_name in each rows, so your select only omit the row with that columns, not other rows with other columns of that table.

One query you could use is:

SELECT DISTINCT table_name
FROM user_tab_columns c
WHERE NOT EXISTS (
    SELECT 1
    FROM user_tab_columns
    WHERE column_name LIKE '%OBJECT_NO%'
        AND table_name = c.table_name
) 
ORDER BY table_name;

Other way could work is:

SELECT DISTINCT table_name
FROM user_tab_columns 
WHERE table_name NOT IN (
    SELECT table_name
    FROM user_tab_columns
    WHERE column_name LIKE '%OBJECT_NO%'
) 
ORDER BY table_name;
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much. both solution is work perfectly for me. !
1

Try this:

SELECT 
    DISTINCT TABLE_NAME 
FROM 
    USER_TAB_COLUMNS
WHERE 
    TABLE_NAME NOT IN 
    (
        SELECT 
            TABLE_NAME
        FROM 
             USER_TAB_COLUMNS
        WHERE COLUMN_NAME LIKE '%OBJECT_NO%'
     )

Comments

0

Try this -

SET SERVEROUT ON;

BEGIN

FOR R IN (SELECT 'SELECT '||A.COLUMN_NAME||' FROM '||A.TABLE_NAME||' ' AS QRY FROM USER_TAB_COLUMNS A
WHERE COLUMN_NAME = 'OBJECT_NO')
LOOP
    EXECUTE IMMEDIATE USING R.QRY;
END LOOP;
/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.