all groups > sql server programming > july 2003 >
You're in the

sql server programming

group:

Parent-child insert/copy



Parent-child insert/copy sqlvs NO[at]SPAM myway.com
7/20/2003 7:18:16 PM
sql server programming: Hello,

[I posted this to microsoft.public.sqlserver, but I think it's been
phased out? I couldn't locate the group on DevelopersDex.]

My basic question is, how can I insert/copy related parent/child rows
from one database or set of tables to another? [I've searched old
posts for the last couple of hours, but kept finding questions about
XML inserts? And BOL examples are too simple.]

Here are two sample tables:

Parent
------
ParentID [Identity column]
ParentName

Child
-----
ChildID [Identity column]
ParentID
ChildName

There are about twenty parent records I want to copy, with several
hundred related child records, so I thought I'd create a stored
procedure and just pass in the ParentID for each set. I've tried the
following but this fails on the "SELECT ChildName" subquery because,
naturally, it returns more than one value:

INSERT INTO ParentTemp (ParentTempName)
SELECT ParentName FROM Parent WHERE ParentID = 3
INSERT INTO ChildTemp (ParentTempID, ChildTempName)
SELECT (SELECT SCOPE_IDENTITY()),
(SELECT ChildName FROM Child WHERE ParentID = 3)

This seems like it should be a pretty simple and common task, but I
haven't found the answer yet. As a programmer, I rarely deal with
large sets of data in this fashion (retrieving the data, yes, but I
don't think I've ever done multi-level inserts beyond a single parent
and its related children--most applications just don't work that
way!), but I'd really like to learn how to do this and similar tasks.
The destination here is a production database with live data (I have
back-ups and of course will take the necessary precautions).

I've thought about creating by hand each Parent record in the
production database, then hard-coding the created identity values for
each set of child records, but I imagine there's a one-shot way to get
this done.

Also, for future reference, does anyone have any book recommendations
that might help in this area? I've checked out a number of SQL books
in the past, but they seem not to have the answers I need (too simple
or much too specific/pedantic).

Re: Parent-child insert/copy Jacco Schalkwijk
7/21/2003 9:33:15 AM
You can select a variable (or function) together with a column from a table:

INSERT INTO ParentTemp (ParentTempName)
SELECT ParentName FROM Parent WHERE ParentID = 3

INSERT INTO ChildTemp (ParentTempID, ChildTempName)
SELECT SCOPE_IDENTITY(),
ChildName FROM Child WHERE ParentID = 3

--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.


[quoted text, click to view]

Re: Parent-child insert/copy sqlvs NO[at]SPAM myway.com
7/21/2003 4:46:39 PM
[quoted text, click to view]

Re: Parent-child insert/copy Joe Celko
7/21/2003 7:38:23 PM
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Making a guess at what you meant, you don't have any tables
becuase you have no keys.

Let's re-do your tables to have real keys and DRI actions instead of
that non-relational, proprietary IDENTITY property (it is not a
column!).

CREATE TABLE Parents
(parent_id INTEGER NOT NULL PRIMARY KEY,
parent_name CHAR(15) NOT NULL);

CREATE TABLE Children
(child_id INTEGER NOT NULL,
parent_id INTEGER NOT NULL
REFERENCES Parents(parent_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
child_name CHAR(15) NOT NULL,
PRIMARY KEY (child_id, parent_id));

If human beings have to use the child_id and parent_id, then take the
time to design them -- check digits, syntax verification rules, external
verification rules,etc.

Put the parents into a file, and then write a program to insert them and
verify the key, one row at a time (sorry, but this is the only safe way
to clean up this data). Then do the same with the children and watch
for the DRI to throw out the orphans that accumulate when your schema is
this poorily designed.

If this is actually a general hierarchy and not just a two-level one,
then look at the nested sets model for the structure.

--CELKO--


*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button