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

  • Home
  • SEARCH
  • 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 8165091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:27:33+00:00 2026-06-06T19:27:33+00:00

Dealing with some legacy code and in trying to get a poorly designed database

  • 0

Dealing with some legacy code and in trying to get a poorly designed database to display what we want I did a self-union on a table to display an intersection of records in a certain order – simplified e.g.

SET @setOfRecords = 1234
SET @subsetOfRecords = 4567 

--Set A
SELECT * 
FROM results
WHERE 
    resultSet = @setOfRecords AND
    resultSubset = 4567

UNION

--Set B
SELECT * 
FROM results
WHERE 
    resultSet = @setOfRecords AND
    resultSubset <> 4567

Displays correctly, i.e.

Set A
Set B

However, if I parametrise the subset id this order is reversed – e.g.

SET @setOfRecords = 1234
SET @subsetOfRecords = 4567 

--Set A
SELECT * 
FROM results
WHERE 
    resultSet = @setOfRecords AND
    resultSubset = @subsetOfRecords

UNION

--Set B
SELECT * 
FROM results
WHERE 
    resultSet = @setOfRecords AND
    resultSubset <> @subsetOfRecords

Displays incorrectly, i.e.

Set B
Set A

I am aware that this is not an ideal method, however other constraints have lead to the necessity of this implementation. My question is purely on why the ordering is affected by parametrisation on the resultSubset ID.

Any ideas? Running on SQL Server 2008.

Thanks,
Paul

  • 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-06-06T19:27:34+00:00Added an answer on June 6, 2026 at 7:27 pm

    Why do you expect any specific order? You haven’t included an ORDER BY clause. Therefore SQL Server is free to return the rows in any order it deems most efficient.

    If you want a predictable order, include an ORDER BY clause. If you don’t include one, you’re telling SQL Server that you don’t care about order.

    In this case you can compare the actual execution plans and you will probably find a difference in at least one sort operator. This could be due to a variety of reasons, including parameter sniffing, statistics, etc…

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

Sidebar

Related Questions

I found in some legacy code I'm dealing with this function (in C++) Vec3d
I'm dealing with a bit of a legacy database here, and I'm having some
I am dealing with some python code automatically generated for me. I want to
I'm dealing with some legacy code that uses COMMON blocks extensively and sometimes uses
I'm dealing with some legacy code that stores its data in a proprietary string
I'm dealing with some old @ss legacy code that was written 3 or 4
When I am in debug and I am dealing with some legacy code or
I am receiving a SOAP request from some legacy code, and dealing with it
I am dealing with legacy code and in UI I can find some ID's
I've got some code dealing with HTTP requests and I want to unit-test it.

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.