I was using a temp table variable in my stored procedure and was doing some insertion into temp table from another temp table with order by clause. It was working in SQL Server 2008. Order by
clause was working while inserting into table.
But now I have upgraded the database to SQL Server 2014 and now order by
clause behavior has changed. It no longer inserts the data in ordered manner.
Eg :
declare @table1 table(id int, datecolumn datetime)
declare @table2 table(id int, datecolumn datetime)
declare @table3 table(id int, datecolumn datetime)
insert into @table1 values(1, getdate());
insert into @table1 values(1, DATEADD(hour, 1, getdate()));
insert into @table1 values(1, DATEADD(hour, 2, getdate()));
insert into @table2 values(2, getdate());
insert into @table2 values(2, DATEADD(minute, 55, getdate()));
insert into @table2 values(2, DATEADD(minute, 130, getdate()));
insert into @table3
select *
from
(select * from @table1
union all
select * from @table2) t
order by datecolumn
select * from @table3
And output is correct in SQL Server 2008 which is
id datecolumn
---------------------------
1 2015-03-31 21:27:48.290
2 2015-03-31 21:27:48.290
2 2015-03-31 22:22:48.290
1 2015-03-31 22:27:48.290
1 2015-03-31 23:27:48.290
2 2015-03-31 23:37:48.290
But in SQL Server 2014, it is showing incorrect
id datecolumn
----------------------------
1 2015-03-31 10:57:22.920
1 2015-03-31 11:57:22.920
1 2015-03-31 12:57:22.920
2 2015-03-31 10:57:22.920
2 2015-03-31 11:52:22.920
2 2015-03-31 13:07:22.920
How do make it work in SQL Server 2014 ?