将一个表中的数据插入多个表中

时间:2010-02-10 22:56:15

标签: sql sql-server data-migration

我正在使用SQL Server 2005。

我正在将数据从当前数据库(单个表)迁移到新数据库(规范化 - 许多表)。在新数据库中,我有一个基表(我们称之为“BaseTable”)和多个其他表(让我们称之为"DependentA""DependentB")。旧数据库中的一些数据将转到BaseTable,而另一些将转到另外两个。 BaseTable与DependentA和DependentB都有一对一的关系,使用它们的Id作为外键。

所以这是我的问题。我应该如何迁移数据?这是我一直在尝试的一个查询,除了一件事之外它是有效的:BaseTable中的其他两个外键是相同的,而不是每个都有不同的。

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

4 个答案:

答案 0 :(得分:7)

问题是@ dep1Id和@ dep1Id是标量,只保留了两个基于集合的插入的最后一个值。

因为它是一次性的,你应该把它作为光标

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

我的游标语法可能有很多错误,但你明白了。

答案 1 :(得分:4)

要避免使用大型数据集的游标,请在新表中临时包含OldTable_id。

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

对DependentB表和从OldTable规范化的任何其他表执行相同的操作。

数据迁移后删除OldTable_id。

答案 2 :(得分:1)

[在此输入图像描述] [1] ZeorOne是您想要获取数据并将其分别插入零和一个表的主表。

select idzero,namezero,idone,nameone from zeroone

insert into zero 
select idzero,namezero from zeroone

insert into one
select idone,nameone from zeroone

或者您想使用游标从Zeroone中选择列中的数据 进入两个表,查询在这里

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

答案 3 :(得分:0)

DECLARE @Product_Name varchar(50),@Generic_Name varchar(50),@Category_Name varchar(50),@Manufacture_Name varchar(50),
@UOM_Name varchar(50),@ProductId int,@GenericId int,@CategoryId int,@ManufactureId int,@UOMId int

DECLARE MultiplTable CURSOR FOR
SELECT ProductName,GenericName,CategoryName,ManufacturerName,UOMName from Noor_ProductList

open MultiplTable
fetch next from MultiplTable into
@Product_Name,@Generic_Name,@Category_Name,@Manufacture_Name,@UOM_Name --declare these!

while @@fetch_status = 0
BEGIN
INSERT INTO Noor_GenericMaster(GenericName) VALUES (@Generic_Name) 
SELECT @GenericId = Scope_Identity()

INSERT INTO Noor_CategoryMaster(CategoryName) VALUES (@Category_Name) 
SELECT @CategoryId = Scope_Identity()

INSERT INTO Noor_ManufaturerMaster(ManufaturerName) VALUES (@Manufacture_Name) 
SELECT @ManufactureId = Scope_Identity()

INSERT INTO Noor_UOMMaster(UOMName) VALUES (@UOM_Name)
SELECT @UOMId = Scope_Identity()

INSERT INTO Noor_ProductMaster (ProductName,GenericID,CategoryID,ManufaturerID,UOMID)
values (@Product_Name,@GenericId,@CategoryId,@ManufactureId,@UOMId)
SELECT @ProductId = Scope_Identity()  

fetch next from MultiplTable into @Product_Name,@Generic_Name,@Category_Name,@Manufacture_Name,@UOM_Name

END
close MultiplTable
deallocate MultiplTable