我有一張關於客戶(客戶(姓名,地址))的數據表,其中包含諸如「John Doe」,「Some Street 123」之類的行。對於表中的每一行,我想在Person(id,name)表中插入一行,在Address(id,person_id,address)表中也插入一行。從屬插入語句
我可以通過在客戶運行的每一行兩個INSERT語句做到這一點:
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