Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 使用另一个表中的外键将批量数据插入到两个相关表中_Sql_Sql Server_Tsql_Foreign Keys_Many To Many - Fatal编程技术网

Sql 使用另一个表中的外键将批量数据插入到两个相关表中

Sql 使用另一个表中的外键将批量数据插入到两个相关表中,sql,sql-server,tsql,foreign-keys,many-to-many,Sql,Sql Server,Tsql,Foreign Keys,Many To Many,我已将一些数据从Excel文件导入临时SQL表。然后我尝试将所有行插入到两个相关的表中。就像这样:在我的数据库中有具有多对多关系的事件和参与者表。演员已经增加了。我想将所有事件添加到events表中,然后将每个事件的关系(ActorId)添加到EventActors表中。 (dbo.tentable有标题、ActorId列) 运行此代码时,所有事件都插入到事件中,但由于外键错误,关系没有插入到EventActors中 我认为应该有一个循环。但是我很困惑。我不想为此编写C代码。我知道在SQL Se

我已将一些数据从Excel文件导入临时SQL表。然后我尝试将所有行插入到两个相关的表中。就像这样:在我的数据库中有具有多对多关系的事件和参与者表。演员已经增加了。我想将所有事件添加到events表中,然后将每个事件的关系(ActorId)添加到EventActors表中。 (dbo.tentable有标题、ActorId列)

运行此代码时,所有事件都插入到事件中,但由于外键错误,关系没有插入到EventActors中

我认为应该有一个循环。但是我很困惑。我不想为此编写C代码。我知道在SQL Server中会有一个简单但高级的解决方案技巧。感谢您的帮助。

使用可捕获新ID,并允许从源表和目标表捕获

捕获此信息后,将其连接回temp表以进行第二次插入

注意:每行需要一个唯一的id,这假设临时表中的一行在事件和事件参与者表中都创建了一行

-- Ensure every row has a unique id - could be part of the table create
ALTER TABLE dbo.TempTable ADD id INT IDENTITY(1,1);

-- Create table variable for storing the new IDs in
DECLARE @NewId TABLE (INT id, INT EventId);

-- Use Merge to Insert with Output to allow us to access all tables involves
-- As Insert with Output only allows access to columns in the destination table
MERGE INTO dbo.[Event] AS Target
USING dbo.TempTable AS Source
ON 1 = 0 -- Force an insert regardless
WHEN NOT MATCHED THEN
    INSERT (Title)
    VALUES (Source.Title)
    OUTPUT Source.id, Inserted.EventId
    INTO @NewId (id, EventId);

-- Insert using new Ids just created
INSERT INTO dbo.EventActor (EventId, ActorId) 
    SELECT I.EventId, T.ActorId
    FROM dbo.TempTable T
    INNER JOIN @NewId I on T.id = T.id;

插入新事件时,需要捕获新ID并将其写回临时表中的正确行,以便在添加EventActors时可以使用。谢谢。我用另一种方法弄明白了。但我想学习如何在SQLServer中实现这一点。实际上我理解你建议的逻辑,但我不知道怎么做。我认为应该有一个循环:我将从第一行插入一个事件,然后使用scope_标识为该事件插入一个eventactor。然后走第二行,同样做,直到所有行都完成。这是真的吗?再次感谢。请看下面我的答案。
-- Ensure every row has a unique id - could be part of the table create
ALTER TABLE dbo.TempTable ADD id INT IDENTITY(1,1);

-- Create table variable for storing the new IDs in
DECLARE @NewId TABLE (INT id, INT EventId);

-- Use Merge to Insert with Output to allow us to access all tables involves
-- As Insert with Output only allows access to columns in the destination table
MERGE INTO dbo.[Event] AS Target
USING dbo.TempTable AS Source
ON 1 = 0 -- Force an insert regardless
WHEN NOT MATCHED THEN
    INSERT (Title)
    VALUES (Source.Title)
    OUTPUT Source.id, Inserted.EventId
    INTO @NewId (id, EventId);

-- Insert using new Ids just created
INSERT INTO dbo.EventActor (EventId, ActorId) 
    SELECT I.EventId, T.ActorId
    FROM dbo.TempTable T
    INNER JOIN @NewId I on T.id = T.id;