INSERT Data From One Table Into Multiple Tables

家住魔仙堡 提交于 2019-11-30 05:21:35

问题


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

回答1:


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.




回答2:


To avoid a cursor for large data sets, temporarily include the OldTable_id in the new tables.

BEGIN TRANSACTION

INSERT INTO DependentA (OldTable_id, column1, column2)
SELECT ot.id, ot.c1, ot.c2
FROM OldDatabase.OldTable ot

INSERT INTO BaseTable (OldTable_id, column5)
SELECT ot.id, ot.c5
FROM OldDatabase.OldTable ot

UPDATE BaseTable 
    SET BaseTable.dependentTable1_id = DependentA.id
    FROM BaseTable
    INNER JOIN DependentA on DependentA.OldTable_id = BaseTable.OldTable_id

COMMIT

Do the same for DependentB table and any other tables being normalized out of the OldTable.

Delete OldTable_id after the data migration.




回答3:


[enter image description here][1]ZeorOne is the main table from which you want to get data and insert it into zero and one table respectively.

select idzero,namezero,idone,nameone from zeroone

insert into zero 
select idzero,namezero from zeroone

insert into one
select idone,nameone from zeroone

or you want to use cursor to insert data with selected columns from Zeroone into to two tables the query is here

Declare @idzero int
Declare @namezero varchar(50)
Declare @idone int
Declare @nameone varchar(50)

Declare Cur Cursor  for
select idzero,namezero,idone,nameone from zeroone

open Cur

fetch Cur into @idzero,@namezero,@idone,@nameone

While @@fetch_status = 0
begin 

    insert into zero 
    select @idzero,@namezero 

    insert into one
    select @idone,@nameone 

    fetch Cur into @idzero,@namezero,@idone,@nameone

end 

close Cur
Deallocate Cur


来源:https://stackoverflow.com/questions/2240960/insert-data-from-one-table-into-multiple-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!