Solved

Spring JDBC - Oracle Query issue ("IN" parameter)

Posted on 2008-06-17
7
3,029 Views
Last Modified: 2013-12-18
I have an inner query where it has to search through the list of IDs (using IN parameter).  If the size of list of IDs is more than 1000, sql gives error of exceeding the maximum number of expressions in the list. I am using Spring JDBC.
My Query:
SELECT id, name, distance, type,  FROM GLBL_NAME gname GLBL_DEF def WHERE gname.id = def.id and gname.OID in(2508, 1701, 4210, . . . . more than 1000)

above query is giving :
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT id, name, distance, type,  FROM GLBL_NAME gname GLBL_DEF def WHERE gname.id = def.id and gname.OID in(2508, 1701, 4210, . . . . more than 1000) nested exception is java.sql.SQLException: ORA-01795: maximum number of expressions in a list is 1000
0
Comment
Question by:glimlach
7 Comments
 

Author Comment

by:glimlach
ID: 21803369
your quick solutions or alternative implementation solution for this scenario will help me alot..
Thanks in advance..
0
 
LVL 35

Expert Comment

by:johnsone
ID: 21803731
1,000 items is all that Oracle allows to have in an in list.  It is a hard limit.

The only way that I know around this is to use a subquery instead of a list.  That will allow more than 1,000.

SELECT id, name, distance, type,  FROM GLBL_NAME gname GLBL_DEF def WHERE gname.id = def.id and gname.OID in(select....)

If that list is not available by a query, you can create a global temporary table that holds the list and use that in the subquery.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 21803736
you can do it in two ways.

First,  build your string with 2 or more  IN clauses that are or'd together



and
(
 gname.OID in(1,2,3,4....1000)
or
gname.OID in (1001,1002,...,2000)
or
gnamd.OID in (2001,2002,...,3000)
or
....
)

inserting your own list of 1000 id's at a time in each or clause until you exhaust your array of id's.


other way,

insert your data into a global temporary table.  then use a sub-query in your IN clause.


SELECT id, name, distance, type,  FROM GLBL_NAME gname GLBL_DEF def WHERE gname.id = def.id and gname.OID in (select id from your_temp_table)



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:sdstuber
ID: 21803767
or last way....

use a pipelined function that will accept a collection (array) of id's and return a pipelined collection to be used a subquery.  this is effectively the same idea as using the temp table

SELECT id, name, distance, type,  FROM GLBL_NAME gname GLBL_DEF def WHERE gname.id = def.id and gname.OID in (select id from table(your_pipeline_function(:your_array_of_ids)))

0
 
LVL 17

Expert Comment

by:julianopolito
ID: 21860914
Can´t you use EXISTS instead of  IN ? The EXISTS function searches for the presence of a single row meeting the stated criteria as opposed to the IN statement which looks for all occurrences, that´s why the limit of 1000 exists..... (although it makes no sense for me why to have both behaviours)
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 21862583
what do you want to do an exists check against?  You can't do an exists-list like you can an in list.

If you put your data into a temp table or return it with a pipelined function yes, you could use EXISTS.
I'm not sure why you would want to use that instead of IN.  

The IN clause will stop searching as soon as it finds a match.  It won't go through all of the records as you hypothesize.
If you use the pipelined function and you add logging to it you can prove this as the pipeline will stop as soon as a match is found.
0
 

Author Closing Comment

by:glimlach
ID: 31467971
This works. Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 71
SYS password changed. Now can't log in as SYS 27 39
Fill Date time Field 12 29
Pivoting oracle table 9 72
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question