Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server Newbies
»
Insert to Parent/Child tables
Insert to Parent/Child tables
Rate Topic
Display Mode
Topic Options
Author
Message
JimS-Indy
JimS-Indy
Posted Saturday, March 02, 2013 9:34 AM
SSC Veteran
Group: General Forum Members
Last Login: Friday, May 10, 2013 7:30 AM
Points: 225,
Visits: 317
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
Post #1425866
Lynn Pettis
Lynn Pettis
Posted Saturday, March 02, 2013 11:50 AM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 8:34 AM
Points: 21,588,
Visits: 27,385
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.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #1425878
opc.three
opc.three
Posted Saturday, March 02, 2013 11:53 AM
SSCertifiable
Group: General Forum Members
Last Login: Today @ 7:33 AM
Points: 6,696,
Visits: 11,713
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
Believe you can and you're halfway there.
--Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler
--Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them.
--Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples.
--Giordy
Post #1425879
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.