将一张表中的数据插入到多张表中

发布于 2024-08-21 11:15:34 字数 797 浏览 3 评论 0原文

我正在使用 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无言温柔 2024-08-28 11:15:34

问题在于 @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

我的光标语法可能充满了错误,但你明白了。

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.

缘字诀 2024-08-28 11:15:34

为了避免大型数据集出现游标,请暂时将 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。

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.

从来不烧饼 2024-08-28 11:15:34

[在此处输入图像描述][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

[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
挖个坑埋了你 2024-08-28 11:15:34
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文