March 2, 2013 at 9:34 am
I have a Parent table (Identity PK with 600,000+ rows) and a child table (with maybe 20,000 rows). The parent and child share the same PK (1 to {0 or 1} relationship). This was done (in hindsight I shouldn't have...) because the child table population is sparse.
I have some new adds to the table in a "flattened" version...that is, the child contents are part of the parent row.
I want to insert the new additions to the Parent/Child table, but I won't know the PK on the parent until the parent row is inserted. How does one accomplish this?
Jim
March 2, 2013 at 11:50 am
Look at using the OUTPUT clause when inserting the parent records to capture the information you need to insert the child records.
Hard to give more with so little information to work with here.
March 2, 2013 at 11:53 am
I am assuming the PK on the parent is an IDENTITY column? In those cases I use an OUTPUT clause on the INSERT...SELECT to capture the new IDENTITY value for each inserted parent row, plus any "key column(s)" that uniquely identify the row in my source table, to a temporary table. Then I can join the temporary table to my source table on the "key column(s)" to do an INSERT...SELECT into the child table, also providing the IDENTITY value on each row in the temporary table.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply