Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
4.11/5 (2 votes)

I have a stored procedure which returns two records while executing directly from SQL Server Management Studio from which i am taking the top 1 record only.

Let say Record 1 & Record 2

When executing the stored procedure directly from SQL Server Management Studio it returns Record 1 but, calling the same procedure through Entity Framework returns Record 2. but if i'll sort the records it is giving correct result.

I just wanted to know is the default sorting order differs/matters in this case ? Is it different for both the cases ? Can anybody help me in this regard ?

Thanks in advance.

1 solution


As i know, data stored in database is in scattered manner. So, whenever you execute query without sorting, it displays result in that order in which it get data first. Whenever you mentioned the sorting, it again fetches the data in same manner but after fetching all data it apply sorting condition and displays the result as per sorting mentioned.

Hope this will give you some help.

Share this answer
SRS(The Coder) 20-Aug-14 3:46am    
There are two Date columns let say Date1 & Date2, the ordering is made on Date1 column but for this scenario Date1 is same for two records. When i am calling the stored procedure directly the sort order is different Record1 then Record2 but calling the same at the same time through EntityFramework returns result like Record2 first and then Record1.

My concern is why is it behaving differently same procedure when called from Entity Framework ?
Magic Wonder 20-Aug-14 3:57am    
As you said, both records are having same value so whichever record is encountering first while fetching data will get display in that order as your data is already sorted. Database will give priority to First come basis.
SRS(The Coder) 20-Aug-14 4:45am    
Here point is same procedure called directly and through Entity Framework and giving different result, which i do not understand..
Magic Wonder 20-Aug-14 5:17am    
I have given importance of execution of query with or without sorting. By the way, execution from stored proc directly from MGMT studio is always giving same result? How many records are there in your table?
SRS(The Coder) 20-Aug-14 5:22am    
table is having around 50 records from which procedure returning two from which i am taking TOP 1. So top 1 record varies when procedure called directly and when called through entity framework imported function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900