Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear team
I am facing an wired issue, where On each execution InnerJoin return result in random order.
This occurred on SQL 2008R2 and 2012 also.

Both inner join column have (same) datatype NUMERIC(18,0)

what could be possible reason ?
here is my sample query

SQL
SELECT info.col1, 
info.col2, info.col3, info.col4
FROM table1 AS info INNER JOIN tavle2 AS pln ON  pln.col2 =info.col6 WHERE info.col6 = 'I00262'


What I have tried:

1. I have check datatypes of both columns (which are same)
Posted
Updated 26-Feb-17 17:49pm

1 solution

You've made the assumption that database tables have an order to begin with. They don't. The records from a table can be returned in any order at any time. This comes down to where the data came from in the SQL Server. This means coming straight from disk, from cache pages, affected by some other operations going on, what have you. They could be written to the database in random order. There are a lot of factors in play.

I've got code that logs a lot of data to a table for debugging purposes. The records are all written in a specific order when the code writes them (timestamped) but when you query the database later you find the records are all jumbled up.

The only time records are guaranteed to have any kind of order at all is when you add an ORDERBY clause to your SELECT query. Other that that, assume they are out of order.
 
Share this answer
 
Comments
koolprasad2003 26-Feb-17 23:56pm    
Thanks for the elaboration Dave.
Now, if same database files (MDF/LDF) I have de-attach and re-attach on other server, I have not observed any random order output
on each execution it fetches data in same order.
above scenario is again put me on think.
Dave Kreskowiak 27-Feb-17 0:03am    
And that doesn't change my answer at all.

In the end you are not guaranteed any order at all unless you put an ORDER BY clause in the query.
koolprasad2003 27-Feb-17 0:20am    
That's all clear. Thanks.

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