Comparing list of values against table

后端 未结 3 686
梦谈多话
梦谈多话 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:08

    there are many built in public collection types. you can leverage one of them like this:

    with ids as (select /*+ cardinality(a, 1) */ column_value id
                   from table(UTL_NLA_ARRAY_INT(100, 200, 300)) a
                )
    select ids.id, case when m.id is null then '**NO MATCH**' else m.value end value
      from ids
             left outer join my_table m
                         on m.id = ids.id;
    

    to see a list of public types on your DB, run :

    select owner, type_name, coll_type, elem_type_name, upper_bound, precision, scale from all_coll_types
     where elem_type_name in ('FLOAT', 'INTEGER', 'NUMBER', 'DOUBLE PRECISION')
    

    the hint

    /*+ cardinality(a, 1) */
    

    is just used to tell oracle how many elements are in our array (if not specified, the default will be an assumption of 8k elements). just set to a reasonably accurate number.

提交回复
热议问题