Shenali Silva
Shenali Silva

Reputation: 137

PL/SQL Loop through a list and find values which not exists in database table

I have a list of names. But this list is quite big(10000+ items). I want to go through this values list and list down values which doesn't exist in the table.

I was able to come up with this,

 select i.column_value as country_code
 from table(SYS.DBMS_DEBUG_VC2COLL('AU', 'IN', 'ZA', 'DK', 'CH', 'NL')) i
 where not exists (select null
                   from country c
                   where c.country_code = i.column_value)

but it limits the number of values to be provided to the function as 1000. Thus I'm not able to give to provide full list at once

ORA-00939: too many arguments for function

Does anyone know a solution to this.

Upvotes: 0

Views: 828

Answers (2)

vishnudattan
vishnudattan

Reputation: 476

Agree with Alex that external table is the way to go, assuming you have permissions to set up a directory and copy the file to the database server. A quick bet would be to query all_directories for existing directories and see if you have access to copy your file there. Also, dba_external_tables would show you existing external tables (if any) that you can describe to show the syntax.

If you're lacking permissions, you may want to explore SQL Loader sqlldr to load the codes from raw file onto a database table, which will then make your query easier and faster.

In sqlldr use OPTIONS (DIRECT=TRUE, PARALLEL=TRUE) UNRECOVERABLE to speed up the load.

Good luck and chat back here if you need more help.

Upvotes: 0

Alex Poole
Alex Poole

Reputation: 191245

If you're really stuck with fixed lists of values, you could union together several table collection expressions, in a subquery (inline view):

select i.column_value as country_code
from (
  select * from table(SYS.odcivarchar2list(
    'AU', 'IN', 'ZA', 'DK', 'CH', 'NL' -- up to 999 entries
  ))
  union all
  select * from table(SYS.odcivarchar2list(
    'AU', 'IN', 'ZA', 'DK', 'CH', 'NL' -- up to 999 entries
  ))
) i
where not exists (select null
                  from country c
                  where c.country_code = i.column_value)

I tend to use odcivarchar2list rather than dbms_debug_vc2coll but it should work with either.

If the data is in a file and you can make that available on the database server you might be able to load it as an external table, which would involve less manual work.

Upvotes: 2

Related Questions