Subject Re: [firebird-support] Copy records
Author Martijn Tonies
Hi,

> I'm pretty new to firebird (and databases at all), so execuse me, if this
is
> to basic question for this forum.
>
> I have two tables, master and detail. Each record in master table can have
> multiply records in detail table.
>
> Now I want to make a copy of record in master table (with new id of
course),
> and at the same time copies of all records in detail table, which refers
to
> the record from master table.
>
> Example:
>
> Master:
> ID1,Name1,Town1,...
>
> Detail
> ID1,Order1,Desc1,...
> ID1,Order2,Desc2,...
> ID1,Order3,Desc3,...
>
> After copy of record ID1 from master I want to get:

First, copy the master records:

INSERT INTO master (ID, Name, Town) VALUES
SELECT 'ID2', Name, Town
WHERE ID = 'ID1';

You do this by inserting the rows that gets returned by
selecting the "ID1" row, but with a new ID, "ID2".

> Master:
> ID1,Name1,Town1,...
> ID2,Name1,Town1,...
>
> Detail
> ID1,Order1,Desc1,...
> ID1,Order2,Desc2,...
> ID1,Order3,Desc3,...
> ID2,Order1,Desc1,...
> ID2,Order2,Desc2,...
> ID2,Order3,Desc3,...
>
> Is there a simple way to do this ?

Next, do the same for the detail rows, but again use
"ID2" to insert.

With regards,

Martijn Tonies
Database Workbench - developer tool for InterBase, Firebird, MySQL & MS SQL
Server.
Upscene Productions
http://www.upscene.com