gpt4 book ai didi

sql - Insert into Select 未正确插入相同的数据顺序

转载 作者:行者123 更新时间:2023-12-02 23:36:06 26 4
gpt4 key购买 nike

我在SQL Server 2014中使用了insert into命令,但没有按照数据的相同顺序插入。

它显示的行数相同,但数据顺序与下图中看到的不同。

插入命令是:

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]

图 1:源表的选择命令

enter image description here

图 2:目标表的选择命令

enter image description here

我该如何解决这个问题?

最佳答案

SQL 结果集是无序的,除非您有order by。 SQL 表是无序的。

但是,SQL Server 确实提供了至少一种方法来完成您想要的操作。如果您在具有 identity 列的表中使用 order by,并且 select 有一个 order by,则身份会相应递增。

因此,做你想做的事情的一种方法是在 [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>;

然后,当您查询表时,请记住order by:

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

关于sql - Insert into Select 未正确插入相同的数据顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com