insert into无法插入所选数据

问题描述 投票:0回答:1

在过去的几周里,我一直在努力将68GB的桌子拆分成一个更加标准化的结构,一切都顺利进行到今天。

我试图使用此查询将大表中的精选几列移动到新表中:

insert into [destination] (col1, col2, col3...)
select col1, col2, col3
From [source]
where companyID = [source].companyID

我收到消息,(60113678行受影响),但数据没有插入目的地,并且源表中的数据没有被更改,所以受影响的是什么,为什么不是任何插入目的地的数据?

sql sql-server insert-into
1个回答
1
投票

您似乎想要执行的代码是:

update d
    set col1 = s.col1,
        col2 = s.col2,
        col3 = s.col3
    from destination d join
         sources s
         on s.companyID = s.companyId;

您编写的代码相当于:

insert into [destination] (col1, col2, col3...)
    select s.col1, s.col2, s.col3
    From [source]
    where s.companyID = s.companyID;

where相当于s.companyID is not null。因此,您已将source中的所有60,113,678行插入destination中的新行。

显然,故事的一个道德是理解insertupdate之间的区别。更重要的是,限定查询中的所有列名称。如果你这样做了,你的查询将在source.CompanyID = destination.CompanyId失败 - 你不必弄清楚如何删除60,113,678个新行。

© www.soinside.com 2019 - 2024. All rights reserved.