Paste in "Select" does not paste the same data order correctly

I used the insert command in a SQL Server 2014 command, but did not insert it into the same data order.

It shows the same number of rows, but this is not the same data order as in the figures below.

Insert command:

insert into [test].[dbo].[HöjdKortvågVänster] ([Höjd kortvåg vänster (null)]) select [Höjd kortvåg vänster (null)] from [test].[dbo].[test111] 

Figure 1: Select a command for the source table

enter image description here

Figure 2: Select a command for the destination table

enter image description here

What can I do to solve this problem?

+5
source share
1 answer

SQL result sets are unordered unless you have order by . SQL tables are unordered.

However, SQL Server provides at least one method for performing the necessary actions. If you use order by in a table with an identity column, and select has order by , then the identifier increases accordingly.

So, one way to do what you want is to have a column in [HöjdKortvågVänster] :

 id int not null identity(1, 1) primary key insert into [test].[dbo].[HöjdKortvågVänster]([Höjd kortvåg vänster (null)]) select [Höjd kortvåg vänster (null)] from [test].[dbo].[test111] order by <appropriate column here>; 

And then when you query the table, remember order by :

 select * from [test].[dbo].[HöjdKortvågVänster] order by id; 
+5
source

All Articles