相关插入语句
我有一个表,其中包含有关客户的数据,客户(姓名,地址),其中包含“John Doe”、“Some Street 123”等行。对于表中的每一行,我想在 Person(id, name) 表中插入一行,并在 Address(id, person_id, address) 表中插入一行。
我可以通过为 Customer 中的每一行运行两个插入语句来完成此操作:
insert into Person(name) values (@name);
insert into Address(person_id, address) values (scope_identity(), @address);
但这效率很低。我想批量插入,如下所示:
-- This works, the problem is with the Address table...
insert into Person(name)
select name from Customer
-- This looks good but does not work because name is not unique.
insert into Address(person_id, address)
select p.person_id, c.address
from Customer c join Person p on c.name = p.name
I have a table with data about a customer, Customer(name, address), with rows like "John Doe", "Some Street 123". For each row in the table, I want to insert one row in the Person(id, name) table and also one row in the Address(id, person_id, address) table.
I can accomplish this by running two insert statements for each row in Customer:
insert into Person(name) values (@name);
insert into Address(person_id, address) values (scope_identity(), @address);
But this is inefficient. I want to do the inserts in a batch, kind of like this:
-- This works, the problem is with the Address table...
insert into Person(name)
select name from Customer
-- This looks good but does not work because name is not unique.
insert into Address(person_id, address)
select p.person_id, c.address
from Customer c join Person p on c.name = p.name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
把这个留给像我一样找到这篇文章的谷歌旅行者。
我找到了这个解决方案,它似乎工作得很好,并且不需要任何时髦的模式更改:
https://dba.stackexchange.com/questions/160210/将数据一次性拆分为两个表
他们使用 MERGE 语句执行对第一个表(生成要生成的标识的表)的初始插入。在其他地方使用)。它使用 MERGE 语句的原因是它允许您使用 OUTPUT 语句,您可以使用该语句输出新的标识值以及来自源表(与在标准
INSERT
上使用OUTPUT
语句相反,后者不允许您输出源表标识)。您可以将此输出数据插入到映射表中,并使用该映射表执行第二次插入。这是我的解决方案的示例代码:
Leaving this here for the fellow Google traveler that finds this post like me.
I found this solution, and it seems to work great, and doesn't require any funky schema alterations:
https://dba.stackexchange.com/questions/160210/splitting-data-into-two-tables-in-one-go
They use a
MERGE
statement to perform the initial insert into the first table (the table that is generating the identity to be used everywhere else). The reason it uses theMERGE
statement is because it allows you to use anOUTPUT
statement, which you can use to output both the new identity value as well as the identity value from the source table (as opposed to using anOUTPUT
statement on a standardINSERT
which does not allow you to output the source tables identity). You can insert this output data into a mapping table, and use that mapping table to perform the second insert.Here's my sample code for the solution:
正如您所解释的那样,无法执行此操作,因为您丢失了第一次插入的每行的scope_identity() 值。
解决方法可能是将客户主键字段添加到人员表中,然后将第二次插入与此字段连接:
在插入之前,在人员上创建 customerID 字段,
然后批量插入:
之后,您可以从人员表中删除 customerID 字段:
there is no way to do this as you explain because you lost scope_identity() value of each row of first insert.
A work around may be add Customer primary key fields to Person table and then make join of second insert with this fields:
before insert create customerID field on Person
then bulk inserts:
after that you can remove customerID field from Person table:
最好在两个表中创建一些与它们相关的唯一类型的字段。否则您需要连接,因为您没有条件的唯一字段
It's better that you create some field of unique types in both table are related them.otherwise you want join as you dont have unique field for condition