3

鉴于以下(表1):

Id    Field1  Field2     ...
--    ------  -------
NULL  1        2
NULL  3        4
...

我想将 Field1 和 Field2 的值插入到不同的表(Table2)中。Table2 有一个自增整数主键。我想从 Table2 中检索新的 PK 并更新上面的 Id 列(Table1)。

我意识到这不是传统的 - 这不是我需要定期做的事情,只是一次性完成一些迁移工作。我做了一些尝试使用INSERT INTO, OUTPUT, INSERTED.Id,但失败了。“环回”到 Table1 中的 PK 必须与插入的 Field1/Filed2 的值相关联。

4

4 回答 4

2

您应该能够进行插入,然后删除并重新插入。

create table t1
( id int, f1 int, f2 int);

create table t2
( id int primary key IDENTITY , f1 int, f2 int);

insert into t1 (id, f1, f2) values (null, 1, 2);    
insert into t1 (id, f1, f2) values (null, 3, 4);
insert into t1 (id, f1, f2) values (null, 5, 6);
insert into t1 (id, f1, f2) values (null, 5, 6);

insert into t2 (f1, f2) 
select f1, f2 from t1 where id is null;

delete t1 
  from t1 join t2 on (t1.f1 = t2.f1 and t1.f2 = t2.f2);

insert into t1
select id, f1, f2 from t2;

select * from t1;

请参阅SQLFiddle上的此示例。

于 2012-07-16T14:35:39.277 回答
2

您将需要某种类型的唯一键来匹配每个表中的行。我冒昧地将 TempGuid 列添加到您的每个表(您以后可以删除):

-- Setup test data
declare @Table1 table (
      Id int null
    , Field1 int not null
    , Field2 int not null
    , TempGuid uniqueidentifier not null unique
)
insert into @Table1 (Field1, Field2, TempGuid) select 1, 2, newid()
insert into @Table1 (Field1, Field2, TempGuid) select 3, 4, newid()

declare @Table2 table (
      Id int not null primary key identity(1, 1)
    , Field1 int not null
    , Field2 int not null
    , TempGuid uniqueidentifier not null unique
)

-- Fill Table2
insert into @Table2 (Field1, Field2, TempGuid)
select Field1, Field2, TempGuid
from @Table1

-- Update Table1 with the identity values from Table2
update a
set a.Id = b.Id
from @Table1 a
    join @Table2 b on a.TempGuid = b.TempGuid

-- Show results
select * from @Table1

Output如果您在 Table1 上已经有一个要插入 Table2 的唯一键,那将是可行的。您还可以在循环或游标中执行临时唯一键(可能再次使用 GUID)并一次处理一行,但这对我来说似乎更糟。

更新

这是在您的表上运行的实际 SQL:

-- Add TempGuid columns
alter table Table1 add TempGuid uniqueidentifier null
update Table1 set TempGuid = newid()
alter table Table2 add TempGuid uniqueidentifier not null

-- Fill Table2
insert into Table2 (Field1, Field2, TempGuid)
select Field1, Field2, TempGuid
from Table1

-- Update Table1 with the identity values from Table2
update a
set a.Id = b.Id
from Table1 a
    join Table2 b on a.TempGuid = b.TempGuid

-- Remove TempGuid columns
alter table Table1 drop column TempGuid
alter table Table2 drop column TempGuid
于 2012-07-16T14:51:12.423 回答
1

假设您可以完全控制架构定义,请Table2向该引用Table1的主键添加一个外键。
执行数据插入:

INSERT INTO Table2 (Field1, Field2, T1PK)
SELECT Field1, Field2, PK FROM Table1

然后回填Table1:

UPDATE t1 SET Id = t2.PK
FROM Table1 t1 INNER JOIN Table2 t2 ON t2.T1PK = t1.PK

然后从 中删除多余的列 ( T1PK) Table2

编辑:

由于 中没有 PK Table1,因此只需在 中添加一个Table1,使用它,然后将其放在最后。

例如...

ALTER TABLE Table1 ADD COLUMN T1PK UNIQUEIDENTIFIER CONSTRAINT Table1_PK PRIMARY KEY DEFAULT NEWID();

ALTER TABLE Table2 ADD COLUMN T1PK UNIQUEIDENTIFIER NULL

INSERT INTO Table2 (Field1, Field2, T1PK)
SELECT Field1, Field2, T1PK FROM Table1

UPDATE t1 SET Id = t2.PK
FROM Table1 t1 INNER JOIN Table2 t2 ON t2.T1PK = t1.T1PK

ALTER TABLE Table1 DROP CONSTRAINT Table1_PK

ALTER TABLE Table1 DROP COLUMN T1PK

ALTER TABLE Table2 DROP COLUMN T1PK
于 2012-07-16T14:33:21.370 回答
0

这并不漂亮,但应该一次性完成。

create table tableA
(
id int,
field1 int,
field2 int
)

create table tableB
(
id int identity(1,1),
field1 int,
field2 int
)

insert into tableA select NULL, 1, 2
insert into tableA select NULL, 2, 3

declare @field1_value int;
declare @field2_value int;
declare @lastInsertedId int;

DECLARE tableA_cursor CURSOR FOR 
    select field1, field2 from tableA

    OPEN tableA_cursor
    FETCH NEXT FROM tableA_cursor INTO @field1_value, @field2_value 

    WHILE @@FETCH_STATUS = 0
    BEGIN

        insert into tableB select @field1_value, @field2_value
        set @lastInsertedId = (SELECT SCOPE_IDENTITY())
        update a
            set id = @lastInsertedId
            from tableA a
        where field1 = @field1_value
            and field2 = @field2_value
    print @field1_value

    FETCH NEXT FROM tableA_cursor 
    INTO @field1_value, @field2_value
    END
CLOSE tableA_cursor
DEALLOCATE tableA_cursor

不存在检查:

declare @field1_value int;
declare @field2_value int;
declare @lastInsertedId int;

DECLARE tableA_cursor CURSOR FOR 
    select field1, field2 from tableA

    OPEN tableA_cursor
    FETCH NEXT FROM tableA_cursor INTO @field1_value, @field2_value 

    WHILE @@FETCH_STATUS = 0
    BEGIN

        IF NOT EXISTS    
            (
                select * from tableB 
                where field1 = @field1_value 
                and field2 = @field2_value   
            )    
            BEGIN
                insert into tableB 
                select @field1_value, @field2_value

                set @lastInsertedId = (SELECT SCOPE_IDENTITY())

                update a
                    set id = @lastInsertedId
                    from tableA a
                where field1 = @field1_value
                    and field2 = @field2_value
            END     

    FETCH NEXT FROM tableA_cursor 
    INTO @field1_value, @field2_value
    END
CLOSE tableA_cursor
DEALLOCATE tableA_cursor
于 2012-07-16T15:04:59.193 回答