I have a C/C++ program with embedded Oracle SQL calls in proc (11.2+ version). I have a list of values (int) as an array. I would like to check through a SQL query (as proc code) which values are not in a DB table. For example, say, I have values: 10, 20, 30, 40, 50
stored in an array and my DB table t1 has column col1 that has 10, 20, 40
. So,
Select col1 from t1
will provide me:
10
20
40
So, I am looking for the excluded values, i.e. 30, 50. Can I do this through an embedded Pro*C SQL query? My list is quite large and the DB table has many values.
You need to set up your values in a table. Here is a typical way what will work in most databases:
The syntax might vary depending on the database (
from dual
, a subquery instead of a CTE), but the idea is the same.In Oracle you could use a collection as an alternative to creating a CTE/inline view using unioned queries:
SQL Fiddle.
Here
sys.odcinumberlist
is a built-in collection type, to save you creating your own table type.Aside from being slightly easier to type the values in like that (IMO), you can also use this approach from a client application by passing the values as an array, which can be useful. There are examples here for OCI (C/C++) or here for Java.