How would I insert into multiple related tables using a table valued parameter? Is there any way to do this without using a WHILE loop?
I know how to insert from a table valued parameter into one table:
INSERT INTO dbo.Table
SELECT * FROM @TVP
But what do I do when I want to insert all of someone's information into multiple tables, and have those records related to each other by foreign keys? For example, I want to insert their address info into the dbo.Address table, their phone info into the dbo.Phone table, and then the PKs for the Phone record and the Address record should be inserted in the dbo.Person table, along with FirstName and LastName. How do I get those PKs and insert them into the Person table, all as part of inserting with a table valued parameter that has multiple records in it?
I know how to insert from a table valued parameter into one table:
INSERT INTO dbo.Table
SELECT * FROM @TVP
But what do I do when I want to insert all of someone's information into multiple tables, and have those records related to each other by foreign keys? For example, I want to insert their address info into the dbo.Address table, their phone info into the dbo.Phone table, and then the PKs for the Phone record and the Address record should be inserted in the dbo.Person table, along with FirstName and LastName. How do I get those PKs and insert them into the Person table, all as part of inserting with a table valued parameter that has multiple records in it?
Comment