Question

How to pass more than 1000 parameters in SQL IN clause

Asked by: anandagrawal444

I am using toad for oracle to retrieve data from oracle database. I am using a query using IN clause. I have to pass 4000-5000 parameters to the IN clause. Is there any solution. There is an inherent limitation of IN clause that it does not accept more than 1000 parameters.

 I am having only the viewer rights so I cannot create a temporary table also.

 A very simple query where this will be required is given hereunder

select * from ap_invoices_all
 where invoice_num in (--here we have to pass 4000-5000 parameters--)
                                  
1:
2:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-10 at 01:03:23ID24639251
Tags

Oracle

,

toad

,

sql

Topics

PL / SQL

,

Oracle Database

Participating Experts
6
Points
500
Comments
25

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Pervasice SQL Update with FROM Claus
    I have the following Pervasive SQL script. It crashes my Pervasive service. I am on Pervasive Ver. 10.
  2. Toad
    How to use Toad and why do we use toad?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: YYatinPosted on 2009-08-10 at 01:06:31ID: 25057889

Use UNION

select * from ap_invoices_all
 where invoice_num in (--pass less then or equal to 1000 parameter--)
UNION
select * from ap_invoices_all
 where invoice_num in (--pass less then or equal to 1000 parameter--)

 

by: ravindran_eeePosted on 2009-08-10 at 01:09:43ID: 25057905

A very simple way of doing it would be loading the more than 1000 values into a table. Then write the query as,

select * from ap_invoices_all
 where invoice_num in (select invoice_num from table_temp)

 

by: ravindran_eeePosted on 2009-08-10 at 01:11:35ID: 25057913

Sorry! Just read the question completely.. Not possible to create temporary tables..
Other option is to make use of OR statement..

select * from ap_invoices_all
 where
invoice_num in (--less than 1000 values---)
OR invoice_num in (--less than 1000 values---)
OR invoice_num in (--less than 1000 values---)

I am not sure if the performance is going to be good for this. You might to have to check it out (performance might not be a problem based on the actual number of records)

 

by: Geert_GruwezPosted on 2009-08-10 at 01:54:15ID: 25058100

you really should consider creating a table to write your values in
presumably the user will be selecting the invoices from a list
you probably populate this list from all the available invoices use a text and id from the invoice to identify it

the best way is write this selection a separate table
the table would be best user based
create table ap_invoices_selected as
  (invoice_num number(11,0) not null, -- datatype of invoice_num
   user_id varchar(100, 0) not null); -- identification of logged in user
you then fill up the selection table with the invoices selected and user id

and then use the query

select * from ap_invoices_all
 where invoice_num in (select invoice_num from ap_invoices_selected where user_id = 'x')

this way there is no limit to (except the diskspace/tablespace) number of invoices

if you only have view rights, then find somebody who has create table privilige
a database change should allways be considered a option for program changes

 

by: anandagrawal444Posted on 2009-08-10 at 02:13:00ID: 25058192


 Dear YYatin,
   
    While UNION can be used in simple queries like I have mentioned, It wold be cumbersome to use in case of queries which are bigger and more complex.

  If you have any other ideas than it would be more helpful

 

by: anandagrawal444Posted on 2009-08-10 at 02:14:53ID: 25058202


 Dear Geert_Gruwez

    Please suggest a solution which does not require the creation of a table.

 

by: shru_0409Posted on 2009-08-10 at 02:17:29ID: 25058210

inform your DBA person and create global temporaty table  gt_invoice_num and insert the all invoice number or u can use OR clause instead of In but it will effected your performance in query



select * from ap_invoices_all, gt_invoice_num
 where invoice_num = inv_num

 

by: Geert_GruwezPosted on 2009-08-10 at 02:20:35ID: 25058226

i don't have any good ideas other than that

unless you have other criteria on what to select your invoices
i don't know many users who like selecting 4000 or 5000 invoices from a list and then getting the data

let's say they have a app which can show this list,
let the user select 1 item per sec (not due to program speed but user speed (mouse selecting))
= 5000 sec = 83min
and then starts the loading
and then sees a error
and then has to start all over ...

not very user friendly

can't you use date ranges or filter based on customer id or anything like that ?
i can't imagine any user dumb enough to be wanting to do it this way ...

 

by: anandagrawal444Posted on 2009-08-10 at 04:56:34ID: 25059032


 Dear Geert_Gruwez,

    If you don't have the soltion. Let's live with it. However this does not make the user dumb as there could be innumberable possibilities for why this is required.

 

by: anandagrawal444Posted on 2009-08-10 at 04:58:28ID: 25059045


 Dear Geert_Gruwez'

    It does not work like you have written. This does not take 83 minutes. It only takes 83 seconds. All the values are simply copied and pasted from excel and query executed.

 

by: Geert_GruwezPosted on 2009-08-10 at 05:41:38ID: 25059349

okay,
i may have mistyped something there.

The key factor is how the selection of invoices happens.
Are these lines separate one selected with a combination of Ctrl-Click, Shift-Click
or do they select from line X to line Y
how is the sorting on these invoices
are they sorted by date

for example it the data were sorted by date
wouldn't it be simpler to select by date between start and end ?
you would only need to pass 2 variables ...

if the selection is by individual lines then this problem will off course persist
and will allways be performance issues

since you can't pass so many parameters
maybe you should make less parameters like this

assume first you would have 10 parameters of number type:

select * from ap_invoices_all
 where invoice_num in (:p001, :p002, :p003, :p004, :p005, :p006, :p007, :p008, :p009, :p010);

assuming value for :p001 to :p010
:p001 = 100
:p002 = 200
:p003 = 300
:p004 = 400
:p005 = 500
:p006 = 600
:p007 = 700
:p008 = 800
:p009 = 900
:p010 = 1100

you can change your query like this

select * from ap_invoices_all
 where instr(:px001, '-' || invoice_num || '-') > 0;

the variable would be like this:
:px001 = '-100-200-300-400-500-600-700-800-900-1000-1100-';

off course you'll need to convert the invoicenums to a string
this is a way of reducing your parameters

 

by: mrjoltcolaPosted on 2009-08-10 at 06:42:35ID: 25059814

I agree with Geert. If the report needs 4000-5000 user inputs from a front-end, it is time for a re-design.

 

by: gatorvipPosted on 2009-08-10 at 07:05:37ID: 25059984

>>
the variable would be like this:
:px001 = '-100-200-300-400-500-600-700-800-900-1000-1100-';
<<

Quick note here: while this method is indeed clever, it probably would not work for invoice numbers unless they're really short, due to the 4k character limit in SQL varchar2 column. For example, if there are 1000 invoice numbers and you account for the separator ("-") that means you could only have invoices with average length of 3 bytes.

 

by: Geert_GruwezPosted on 2009-08-10 at 07:31:19ID: 25060215

>>gatorvip
i was surprised it even passed with 999 parameters ...
999*3 + 998*',' + length('select * from ap_invoices_all where invoice_num in') > 4k

so the limitations is not 1000 parameters but in actual fact, the 4k boundary ...

conclusion : it will never work, no matter how you format the query

unless: re-design as mrjoltcola suggested

 

by: Geert_GruwezPosted on 2009-08-10 at 07:32:46ID: 25060230

anyway
if somebody would hit any of the oracle databases i administer with a query like this,
they'd get kicked out

 

by: mrjoltcolaPosted on 2009-08-10 at 07:41:32ID: 25060317

Often we see these types of questions, in short:

1) Programmer has read-only privileges
2) Programmer is stuck with bad design
3) Programmer is asking how to workaround bad design

The short answer is to tell the DBA the design is bad, and get him involved in the solution!

I have never asked my programmers to fix my design mistakes on their own.

But if you are not the DBA, or the system has moved to production mode and has frozen, then the points above may not help you with the short-term solution.

If you cannot change the core data model, then the practical solution here has already been stated above: write all of the values into a temp table and when the user clicks "Submit" or "Ok" join the temp table against the target table and Oracle will perform a join as efficiently as possible. Convince the DBA to grant you write privs to your own schema, use that schema user for the temp table, and do the cross-schema join at runtime.

Otherwise, tell your company / DBA that the cost of a programmer only solution to this problem is way more expensive then getting the DBA involved to do it right.

 

by: anandagrawal444Posted on 2009-08-10 at 12:41:14ID: 25063319


 Geert_Gruwez
   
     If you are an MIS person in place of database administrator than first yu will get kicked out if you are not prepared to comply with different information requirements.

 

by: Geert_GruwezPosted on 2009-08-10 at 22:09:44ID: 25066271

lol,
the one query would probably cost a lot to maintain
and would also cause some performance issues

the one thing you need to do is find the dba and explain your problem
show the solution for your 1000+ parameters
the dba will jump to give you a better solution

if the dba doesn't jump then he/she doesn't mind a bad performing database

i'm affraid that this solution will never give optimal results for the end user

i got (virtually speaking) whacked on my fingers too last week by our senior dba
i was in the top 5 of bad performing queries
i do both sides (programming and dba), 1 foot in either job
it is allways a joint effort to get the best out of a program, both code and queries
i learned a lot about tuning and we both came to the conclusion that the basic problem is not the code, not the queries, but the whole way the system is operating
we need a completer redesign

so i can imagine completely what problems can arise

it is allways in the best interest of everyone to build as good an app as it can be, providing the design of the system is good enough

 

by: anandagrawal444Posted on 2009-08-20 at 08:02:38ID: 25143377


 I think the only feasible solution for this is to use the sub query for passing more than 1000 parameters. The limitation of 1000 parameters is not applicable if the data comes through a sub query.

 Thanks to all the experts who helped in finding the solution.

 

by: mrjoltcolaPosted on 2009-08-20 at 08:25:48ID: 25143725

How do you intend to build the subquery?

From a design perspective, why does the app need 1000 parameters? Is this a GUI where the user selects many rows, page at a time, and then submits? If so, then after each select you could post the value to the database into a temporary table, then use the subquery when you finally submit.

 

by: anandagrawal444Posted on 2009-08-20 at 10:08:36ID: 25144837

this requirement is not for the creation of a regular report.

Sometimes for one time reporting requirements instead of creating a totally new report which may also result into complexities of creating joins and testing the data it would be better and muh faster to use the output from one query and pass it as an input into another query. This may also give faster results

 

by: mrjoltcolaPosted on 2009-08-20 at 10:26:08ID: 25145016

I agree, but the question was regarding passing the parameters from the user level to the database. Somehow I lost track of how we transitioned from that to having them in the database already. I agree, its a better solution, but it was not clear how you solved the original problem of collecting the values that were originally passed in an IN() clause. If you are happy with the suggestions, great, no need to discuss more, but I asked for clarification for the sake of future readers.

 

by: anandagrawal444Posted on 2009-08-20 at 20:01:45ID: 25148854


 We are using the application Toad for oracle where we get the viewer rights only, so that the user can retrieve one time data requirements.

So as an end user we can also retrieve data from back end. We can run queries in toad. Now you would have got the requirement of passing so many parameters. Usually we may get the data out of one query, put it in excel and join the parameters through concatenate function. Suppose we get the output from first query as follows : 25,35,40,50,60.

Now using excel concatenate function we will convert the data into : '25','35','40','50','60'. Now this set of data can be pasted in second toad query and the data can be retrieved from it.

This may be lot easier than creating joins between the two queries. Sometimes joining them may also result into inaccurate data.

Passing the data of one query in another query results into faster execution also. Even using the sub query will affect the performance a lot.

This type of back end data retrieval is necessary where you need to get one set of data out of several modules like payables.purchasing fixed assets etc. and no standard report is available.

Any further suggestions and clarifications are most welcome.

 

by: mrjoltcolaPosted on 2009-08-21 at 06:34:22ID: 25151608

Your final explanation of your problem adequately clarifies the question. The act of using a query tool + Excel as an intermediate step was the whole problem causing the limitation, so the use of a direct subquery is the best solution. Good luck.

 

by: anandagrawal444Posted on 2009-08-22 at 06:15:48ID: 31613592

To summarise more than 1000 parameters can be passed with the IN clause through following three methods :

(1) Using Union function
(2) Using Temporary Table ( If access is available)
(3) Using Sub-Query

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...