Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:39:14+00:00 2026-05-28T16:39:14+00:00

I need to retrieve records that match IDs stored in a list. The query,

  • 0

I need to retrieve records that match IDs stored in a list. The query, generated at runtime, is simple:

SELECT [whatever FROM sometable] WHERE (id = 1) or (id = 5) or (id = 33).

Which is equivalent to

SELECT [whatever FROM sometable] WHERE [id] IN (1, 5, 33);

This is fine, but what if the list contains hundreds or thousands of IDs? The statement will be huge and at some point the SQL parser might croak, or if it does not, performance will probably be quite bad. How can I do this in a way that is not so sensitive to the number of records being retrieved?

(The reason I can’t just loop over the list and retrieve records one by one is that I need the database to do ORDER BY for me. Records must come from the DB ordered by a specific field, while the list represents records selected by the user in a grid which can be sorted in any number of ways. And yes, I could sort the records in code after I retrieve them, but that’s plan B, since I don’t even need to hold them all in one data structure, only to come properly ordered.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:39:15+00:00Added an answer on May 28, 2026 at 4:39 pm

    If you’re really going to have so many IDs that you’re worried about the SQL parser croaking, you can store them into a temporary table and do a cross-join.

    Simply create the table with one (primary key) column, the ID, then populate it with the desired IDs and use something like:

    SELECT [whatever] FROM [sometable] st, [idtable] it
    WHERE st.id = it.id
    

    That query won’t choke any parser and the rows retrieved will be limited to those having the ID in the temporary table.

    This doesn’t have to be a temporary table, of course, you can leave it lying around provided you ensure only one “thing” uses it at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently struggling with a query that needs to retrieve multiple records from
I need to retrieve the name of the stored procedure that a crystal report
I need to retrieve data from two tables. the first is a list of
I need to retrieve distinct records from a column in a mysql table and
I need to retrieve 5 objects that match a certain complex criteria, and I
I have a local collection of record Id's (integers). I need to retrieve records
I need to retrieve random rows from SQL Server database. I am looking for
I need to retrieve the login ids of the user who is logged onto
I need to retrieve the date from a UIDatePicker (Preferably I would also like
I need to retrieve multiple objects from an external system. The external system supports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.