Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在SQL Server中从一个表中获取ID并与另一个表中的记录关联_Sql Server_Identity - Fatal编程技术网

Sql server 如何在SQL Server中从一个表中获取ID并与另一个表中的记录关联

Sql server 如何在SQL Server中从一个表中获取ID并与另一个表中的记录关联,sql-server,identity,Sql Server,Identity,我试着寻找这个问题的答案,但没有结果。这种设置方式背后没有良好的逻辑。这家伙不知道他在做什么,但这是我必须要做的(说来话长) 我使用的是SQL Server 2008R2,我需要从一个表中获取记录,并将数据传输到4个单独的表中,所有这些表都具有一对一的关系(我知道,这并不明智)。我需要从插入数据的第一个表的Identity字段中获取值,然后用相同的ID填充其他3个表,并相应地分散数据。例如: 旧表:字段1、字段2、字段3、字段4 NewTable1:标识字段,字段1 新表2:ID,字段2 新表3

我试着寻找这个问题的答案,但没有结果。这种设置方式背后没有良好的逻辑。这家伙不知道他在做什么,但这是我必须要做的(说来话长)

我使用的是SQL Server 2008R2,我需要从一个表中获取记录,并将数据传输到4个单独的表中,所有这些表都具有一对一的关系(我知道,这并不明智)。我需要从插入数据的第一个表的Identity字段中获取值,然后用相同的ID填充其他3个表,并相应地分散数据。例如:

旧表:字段1、字段2、字段3、字段4

NewTable1:标识字段,字段1 新表2:ID,字段2 新表3:ID,字段3 新表4:ID,字段4

我想在存储过程中处理这个问题。我想做一个循环,但我读到SQL中的循环是不可取的

Loop moving through each record in OldTable... (??)

INSERT INTO NewTable1
  (Field1)
Select Field1 from OldTable

   INSERT INTO NewTable2
     (ID, Field2)
   Select SCOPE_IDENTITY?, Field2 From OldTable Where OldTable.ID = ??

   etc for other 2 tables

Loop to next record in OldTable
我不确定如何使用SCOPE_IDENTITY,但我感觉这将涉及到我如何实现这一点

另外,我可能需要为在NewTable1中创建新记录时设置触发器。我知道,这很疯狂,但我对此无能为力,只能努力解决它

所以,我需要知道 1:最初填充表的最佳方式 2:如何为新记录创建触发器

1的解决方案可能涉及2


请帮忙

使用insert语句的OUTPUT子句怎么样?假设Field1是旧表上的唯一键

Declare @IDinserted table(ID int, Field1 varchar(255));

Insert Into NewTable1(Field1)
Output inserted.ID, inserted.Field1 into @IDinserted
Select OldID, Field1 from OldTable;

Insert Into NewTable2(RowID, Field2)
Select i.ID, o.@Field2 
from @IDinserted i Inner Join OldTable o
  on i.Field1=o.Field1;

您可以使用merge语句的output子句来获取
OldTable
中现有主键与
NewTable1
中新生成的标识
ID
之间的映射

-- Temp table to hold the mapping between OldID and ID
create table #ID
(
  OldID int primary key,
  ID int
);

-- Add rows to NewTable1 and capture the ID's in #ID
merge NewTable1 as T
using OldTable as S
on 1 = 0
when not matched by target then
  insert(Field1) values(S.Field1)
output S.ID, inserted.ID into #ID(OldID, ID);

-- Add rows to NewTable2 using #ID to get the correct value for each row
insert into NewTable2(ID, Field2)
select I.ID, O.Field2
from #ID as I
  inner join OldTable as O
    on I.OldID = O.ID

insert into NewTable3(ID, Field3)
select I.ID, O.Field3
from #ID as I
  inner join OldTable as O
    on I.OldID = O.ID

insert into NewTable4(ID, Field4)
select I.ID, O.Field4
from #ID as I
  inner join OldTable as O
    on I.OldID = O.ID

drop table #ID;


另请参见

旧表中的任何列或列的组合是主键还是唯一的?是的,我想我可以使用该主键将它们关联起来。但是,该字段不存储在新表中。我用它的好方法是什么?