Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT Data From One Table Into Multiple Tables

I'm using SQL Server 2005.

I am migrating data over from a current database (single table) to a new database (normalized - many tables). In the new database, I have a base table (let's call it "BaseTable"), and multiple other tables (let's call them "DependentA", and "DependentB"). Some of the data from the old database will go to BaseTable, and some will go to the other two. BaseTable has a one-to-one relationship with both DependentA and DependentB, using the Id of them as the foreign key.

So here's my question. How should I migrate the data over? Here is a query I've been trying, which is working except for one thing: the foreign keys in BaseTable for the other two are identical, instead or having a different one each.

Begin SQL:

BEGIN TRANSACTION

DECLARE @dep1Id int

DECLARE @dep2Id int

INSERT INTO DependentA (column1, column2)
SELECT c1, c2
FROM OldDatabase.OldTable
SELECT @dep1Id = Scope_Identity()

INSERT INTO DependentB (column3, column4)
SELECT c3, c4
FROM OldDatabase.OldTable
SELECT @dep2Id = Scope_Identity()

INSERT INTO BaseTable (column5, dependentTable1Id, dependentTablr2Id)
SELECT c5, @dep1Id, @dep2Id
FROM OldDatabase.OldTable

COMMIT
like image 833
jchapa Avatar asked Feb 10 '10 22:02

jchapa


People also ask

Can we insert data from one table to another table?

The SQL INSERT INTO SELECT StatementThe INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

Can you insert into multiple tables at once SQL?

You can not insert data into 2 tables simultaneously in a single session. But if u split the insert statements into 2 statements, it is going to give you the same effect! But make sure to add ORDER by in your SELECT statement for both the inserts.

How to insert multiple values into a table in SQL?

The T-SQL function OUTPUT, which was introduced in 2005, can be used to insert multiple values into multiple tables in a single statement. The output values of each row that was part of an INSERT, UPDATE or DELETE operation are returned by the OUTPUT clause.

Can you add data to multiple tables at the same time?

We all know this: if a long-bearded prophet came down from a mountaintop bearing the ten commandments of Oracle programming, one of them might read thus: Thou canst select from many tables, but thou may only update, delete from or insert into one table at a time. Right? Well, not exactly. Because you can actually add data to multiple tables

Can I write to a remote table using a multi-table insert?

You cannot use a multi-table insert to write to a remote table. You can only use multi-table inserts with tables. Not views, materialized or otherwise. The sum of all the columns in the various insert-into clauses of a multi-table insert statement must not exceed 999.

Why do we need multiple tables in Oracle?

Because you can actually add data to multiple tables We all know this: if a long-bearded prophet came down from a mountaintop bearing the ten commandments of Oracle programming, one of them might read thus: Thou canst select from many tables, but thou may only update, delete from or insert into one table at a time.


1 Answers

The problem is that @dep1Id and @dep1Id are scalar and are retaining the last value only from the two set based inserts.

Since it's a one off you should probably do it as a cursor

DECLARE CURSOR @curs FOR
SELECT c1,c2,c3,c4,c5 FROM OldDatebase

open @curs
fetch next from @curs into
@c1,@c2,@c3,@c4,@c5 --declare these!

while @@fetch_status <> 0
BEGIN

INSERT INTO DependentA (column1, column2) VALUES @c1, @c2

SELECT @dep1Id = Scope_Identity()

INSERT INTO DependentB (column3, column4) VALUES @c3, @c4 

SELECT @dep2Id = Scope_Identity()

INSERT INTO BaseTable (column5, department1Id, department2Id) @c5, @dep1Id, @dep2Id    

fetch next from @curs into
@c1,@c2,@c3,@c4,@c5
END
close @curs
deallocate @curs

My cursor syntax is probably riddled with errors, but you get the idea.

like image 155
Paul Creasey Avatar answered Nov 01 '22 19:11

Paul Creasey