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 6592363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:32:42+00:00 2026-05-25T17:32:42+00:00

The scenario is first run an expensive sorted query and save the first 100,000

  • 0

The scenario is first run an expensive sorted query and save the first 100,000 Primary Key (Int32) in a .NET List. Then retrieve detail 100 rows at a time from about a dozen tables. The format of of the detail query is:

    select ... from DetailA where PK in (sorted list of 100 PK).  

The problem is the return is not sorted by the order in the in clause. Need the detail to be sorted. Don’t want apply the sort that was used to create 100,000 master list as that is an expensive and messy sort.

My thought is to load the 100 PK into #tempSort table and use that for the sort. Is there a better approach?

Currently getting the detail one row at a time using SQLDataReader and that takes about 2 seconds. Tested getting all at once using a DataSet with multiple DataTables and that was slower than one row at a time using a SQLDataReader by a factor of 3. This data is being loaded into objects so need for advanced features of a DataSet.

I do not want to save the 100,000 master results in a #temp as this is a big active database and need to move load to the client.

Trying the Table-Value Parameters as suggested.
Is this the proper syntax?

    CREATE TYPE sID100 AS TABLE 
    ( sID INT, sortOrder INT );
    GO

    /* Declare a variable that references the type. */
    DECLARE @sID100 AS sID100;

    /* Add data to the table variable. */
    INSERT INTO @sID100  (sID, sortOrder) 
    values (100, 1), (200,2), (50,3);

    Select * from @sID100;
    select docSVsys.sID, docSVsys.addDate 
    from docSVsys 
    join @sID100 as [sID100] 
    on [sID100].sID = docSVsys.sID 
    order by [sID100].sortOrder
    GO

    Drop TYPE sID100 

Above is not optimal. I am learning but in C# can create a DataTable and pass it to a stored procedure using Tabel-Value Parameters. Exactly what Martin said in the comment but it took some research before I understood his words.

  • 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-25T17:32:43+00:00Added an answer on May 25, 2026 at 5:32 pm

    The order of the PKs in the IN clause is irrelevant.

    Only the outermost ORDER BY matters: any order is arbitrary if not specified

    select ...
    from DetailA
    where PK in (sorted list of 100 PK)
    ORDER BY PK
    

    From MSDN ORDER BY

    When ORDER BY is used in the definition of a view, inline function, derived table, or subquery, the clause is used only to determine the rows returned by the TOP clause. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself.

    Edit, after feedback and including Martin Smith’s idea

    select ...
    from DetailA A
         JOIN
         TVPorTemptable T ON A.PK = T.PK
    ORDER BY
      T.OrderByCOlumn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain my scenario first: I have around 2000 tests to run, which
My first time really getting into caching with .NET so wanted to run a
I'm facing a dilemma (!). In a first scenario, I implemented a solution that
Here's the scenario: if this is a user's first time logging into my web
Scenario: You have an ASP.Net webpage that should display the next image in a
Scenario: Attempting to import many (>100), large(>1M recs) flat-files (csv). Problem: Many records are
First some background. I have a batch-type java process run from a DOS batch
Scenario is such: I have a webapp that I'd like to run dynamically with
So here is the scenario, I am refactoring some spaghetti code. My first problem
I am trying to imitate a scenario where 300,000 consumers are accessing a server.

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.