Oracle SQL : Retrieving non-existing values from IN clause

前端 未结 5 1527
孤独总比滥情好
孤独总比滥情好 2021-01-23 16:00

Having following query:

select table_name
from user_tables
where table_name in (\'A\',\'B\',\'C\',\'D\',\'E\',\'F\');

Assuming only user_tables

5条回答
  •  萌比男神i
    2021-01-23 16:46

    Only way is to use NOT EXISTS by converting the IN clause String into a Table of values.(CTE)

    This is not a clean solution though. As The maximum length of IN clause expression is going to be 4000 only, including the commas..

    WITH MY_STRING(str) AS
    (
      SELECT q'#'A','B','C','D','E','F'#' FROM DUAL
    ),
    VALUES_TABLE AS
    (
      SELECT TRIM(BOTH '''' FROM REGEXP_SUBSTR(str,'[^,]+',1,level)) as table_name FROM MY_STRING
      CONNECT BY LEVEL <= REGEXP_COUNT(str,',')
    )
    SELECT ME.* FROM VALUES_TABLE ME
    WHERE NOT EXISTS
    (SELECT 'X' FROM user_tables u
     WHERE u.table_name = ME.table_name);
    

提交回复
热议问题