0

I am converting an old application to Web API and noticed a strange behaviour. I'm executing a simple select statement to fetch records.

select top 10 
      name, weight, id 
from 
     tblA TA 
inner join 
     tblB TB on TA.id = TB.id 
where 
     id = @id 
ORDER BY 
     weight desc 

Unlike a similar question asked on SO, I am using ORDER BY clause in my query and this is working absolutely fine in old application. The data type for column weight is int.

My C# code in the application is:

            List<People> people = new List<People>();    

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = _conn;
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = strSQLText;

            // add parameters
            cmd.Parameters.AddWithValue("@id", ID);

            SqlDataReader sdrPerson;
            sdrPerson= cmd.ExecuteReader();

            using (sdrPerson)
            {
                while (sdrPerson.Read())
                {
                    Person _person = new Person();
                    _person.name = sdrPerson["name"].ToString().Trim();
                    _person.id = Convert.ToInt32(sdrPerson["id"].ToString().Trim());

                    //add person to people list                       
                    people.Add(_person);
                }
            }

The issue: Query is doing order by but the SqlDataReader reads in different order. The query returns 10 rows and out of them one has weight value of 5 and others have value of 7. When ordered DESC, the top 9 rows just come randomly while the last one is always at the bottom or the output. And strangely it only happens if I order DESC and works fine with ASC. Query runs all good in sql server management studio.

The code is absolutely simple and I expected it to work right away. Why is this happening? Is there an issue with Web API or am I doing something wrong here? Or if the more than 1 row have same value for the order by column, does SqlDataReader do random ordering? I've never come across this before.

Sample output:

<people>
<person>
    <id>1234</id>
    <name>Mike</name>
</person>
<person>
    <id>4545</id>
    <name>Sally</name>
</person>
<person>
    <id>576565</id>
    <name>Ben</name>
</person>
<person>
    <id>66107</id>
    <name>Spyro</name>
</person>
<person>
    <id>54879</id>
    <name>Pat</name>
</person>
<person>
    <id>54876</id>
    <name>Kate</name>
</person>
<person>
    <id>40584</id>
    <name>Larry</name>
</person>
<person>
    <id>35742</id>
    <name>John</name>
</person>
<person>
    <id>26432</id>
    <name>George</name>
</person>

8
  • Sorry, sdrPerson is the sql data reader. I missed to put one line of code in the question. Commented Aug 8, 2014 at 0:55
  • Then what does this do ? sdrPerson.Add(_person); Commented Aug 8, 2014 at 0:57
  • Give examples of your output. Commented Aug 8, 2014 at 0:58
  • Sorry, that was mistake in adding the code snippet in the question. I've edited it now. But yes after reading from sql server, I add it to a list before and return that list Commented Aug 8, 2014 at 0:58
  • You are adding it back to datareader I guess Commented Aug 8, 2014 at 0:59

1 Answer 1

1

You say you specify order by weight but the weight values are not unique. Because of this the output may be randomly order as long as it honors the column(s) specified in the order by clause.

If you use order by col1, col2, col3 all three columns are significant but if you only use col1 it is the only column considered in ordering the output. T

Re: the difference in ASC vs. DESC -- this is an artifact of the implementation and you should not rely upon it or expect it to be consistent.

Sign up to request clarification or add additional context in comments.

8 Comments

So does that mean if the value of the ordered by column is same for all or most of the records, the result set in the sql server might not come in the same order to the SqlDataReader? I ordered it by name and it works fine. But the weight value same for most of the records and is it the reason for random order in code although the order is always same in sql server?
Yes, the order may be random on the non-specified columns. You can even see this on back-to-back queries sometimes. SqlDataReader is not changing the order, the variation in order comes from sql server returning arbitrary ordering when it is not specified.
Didn't get what you mean by non-specified columns. Can you please explain? Because I'm specifying the order by column as well as selecting it.
You are only specifying WEIGHT in the order by clause. Presumably you should use WEIGHT, ID or suchlike in the order by clause.
Also, If it is random in code, why isn't it random in sql server for the columns having same values? Shouldn't SqlDataReader be getting the same result set in same order as we see in the ssms?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.