Comparing list of values against table

后端 未结 3 712
梦谈多话
梦谈多话 2021-01-15 09:47

I tried to find solution for this problem for some time but without success so any help would be much appreciated. List of IDs needs to be compared against a table and find

3条回答
  •  自闭症患者
    2021-01-15 10:28

    You can transform a variable into a query using CONNECT BY (tested on 11g, should work on 10g+):

    SQL> WITH DATA AS (SELECT '100,200,300' txt FROM dual)
      2  SELECT regexp_substr(txt, '[^,]+', 1, LEVEL) item FROM DATA
      3  CONNECT BY LEVEL <= length(txt) - length(REPLACE(txt, ',', '')) + 1;
    
    ITEM
    --------------------------------------------
    100
    200
    300
    

    You can then join this result to the table as if it were a standard view:

    SQL> WITH DATA AS (SELECT '100,200,300' txt FROM dual)
      2  SELECT v.id, dbt.value01
      3    FROM dbt
      4    RIGHT JOIN
      5      (SELECT to_number(regexp_substr(txt, '[^,]+', 1, LEVEL)) ID
      6         FROM DATA
      7       CONNECT BY LEVEL <= length(txt) - length(REPLACE(txt, ',', '')) + 1) v
      8       ON dbt.id = v.id;
    
            ID VALUE01
    ---------- ----------
           100 Ann
           300 John
           200 
    

提交回复
热议问题