简体   繁体   English

在表中为另一个表中的每个id插入行

[英]Insert row in table for each id in another table

I tried searching here for a similar solution but didn't see one so I was wondering what is the best way to accomplish the following. 我试着在这里搜索类似的解决方案,但没有看到一个,所以我想知道什么是实现以下的最佳方法。

I have a table with 17 million + rows all have a unique ID. 我有一个1700万行的表都有一个唯一的ID。 We have recently created a new table that will be used in conjunction with the previous table where the foreign key of the new table is the unique id of the old table. 我们最近创建了一个新表,它将与前一个表一起使用,其中新表的外键是旧表的唯一ID。

For ex. 对于前者
Table 1 - id, field1, field2, field3... table 2 - table1.id, field1 ... 表1 - id,field1,field2,field3 ...表2 - table1.id,field1 ...

The problem is since we are migrating this into a live environment, we need to back fill table 2 with a row containing the id from table 1 for each row in table 1. ex, table 1 - 1, test, null table 2 now needs to have: 1, null, ... and so on for each row that is in table1. 问题是因为我们正在将它迁移到实时环境中,我们需要在表1中为表1中的每一行填充包含表1的id的行.ex,表1 - 1,test,null表2现在需要为table1中的每一行提供:1,null,...等等。 The main issue is that the ids are not all sequential in table 1 so we will have to read from table 1 and then insert based of the id of found into table 2. 主要问题是表1中的id不是全部顺序的,因此我们必须从表1中读取,然后根据找到的id插入到表2中。

Is there any easier way to go about this? 有没有更简单的方法去做? Thanks in advance Joe 在此先感谢乔

Also to clarify, table 2 will be new data and the only thing that it will contain from table 1 is the id to keep the foreign key relationship 另外要澄清的是,表2将是新数据,它将包含在表1中的唯一内容是保持外键关系的id

Also this is sql server 2000 这也是sql server 2000

If I understand correctly, you want one record in table2 for each record in table1. 如果我理解正确,你需要table2中的每条记录中的一条记录。 Also I believe that apart from the reference to table1, table2 should initially contain blank rows. 另外我认为除了对table1的引用之外,table2最初应该包含空行。

So assuming 所以假设

table1 (ID, field1, field2, ...)
table2 (ID, table1_ID, fieldA, fieldB,...)
-- where table1_ID is a reference to ID of table1

After creating table2 you can simply run this insert statement 创建table2后,您只需运行此insert语句即可

insert into table2(table1_ID) 
select ID from table1

I am not sure I am exactly following you, but would something like this work for you? 我不确定我是否正在关注你,但这样的事情对你有用吗?

INSERT INTO table2 ( SELECT field1, field2, field3... FROM table1 )

If I am understanding correctly you want a record in table2 for every record in table1. 如果我正确理解你想在table2中为table1中的每条记录创建一条记录。 This will do just that. 这样就可以了。 Just match up your fields in the select in the right order, and specify constants for any fields in table2 that you don't have in table1. 只需按正确顺序匹配select中的字段,并为table1中没有table1中的任何字段指定常量。

HTH. HTH。 Let me know if I am not understanding and Ill try to help again. 让我知道,如果我不理解,我会再次尝试帮助。

You need to read this article. 你需要阅读这篇文章。

What are the most common SQL anti-patterns? 什么是最常见的SQL反模式?

The main issue is that the ids are not all sequential in table 1 so we will have to read from table 1 and then insert based of the id of found into table 2 主要问题是表1中的id不是全部顺序的,因此我们必须从表1中读取,然后根据找到的id插入到表2中

Yes, look at my answer in the above article and write a key-walking loop using Item #2. 是的,请看上面文章中的答案,并使用Item#2编写一个键行走循环。

Make sure when you write the insert statement, you provide a fieldlist - as I say in Item #1. 确保在编写insert语句时,提供了一个字段列表 - 正如我在第1项中所说的那样。

With that many rows you may run into issues with transaction log space, and length of time running large insert transactions. 使用这么多行,您可能会遇到事务日志空间问题,以及运行大型插入事务的时间长度。

If run time is a constraint I'd seriously recommend using Bcp (or what ever tool is applicable depending on the platform) 如果运行时间是一个约束,我会认真推荐使用Bcp(或者根据平台不同的工具适用)

Select out the id's from the original table, use that to build a Bcp file for the extension table, then Bcp it in. 从原始表中选择id,使用它为扩展表构建Bcp文件,然后将其Bcp。

You many find it more performant to Bcp in files of 10,000 records instead of one humungus file with 17,000,000 rows. 你们很多人发现,在10,000个记录的文件中,Bcp的性能更高,而不是一个包含17,000,000行的humungus文件。

Also, you can do this in the back ground before go live, and write a t-sql job to pick up and that may have been inserted after you took the snapshop of id's. 此外,您可以在上线之前在后台执行此操作,并编写一个t-sql作业来接收,并且在您使用id的snapshop之后可能已插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM