[英]How to insert data from source table (but different columns) into two different tables in SQL Server
I have a staging table titled [Staging].我有一个名为 [Staging] 的临时表。 The data from this table needs to be inserted into two separate tables.
该表中的数据需要插入到两个单独的表中。 Half of the columns go to the first table (we'll call it [Table1]) and the other half go to a second table (we'll call it [Table2])
一半的列 go 到第一个表(我们称之为 [Table1]),另一半列 go 到第二个表(我们称之为 [Table2])
Both of these tables have a column titled "ChainID".这两个表都有一个标题为“ChainID”的列。 In [Table1] the ChainID is an identity column.
在 [Table1] 中,ChainID 是一个标识列。 In [Table2] it's not.
在 [Table2] 中不是。
The ChainID is the one column that links these two tables together for when we need to query this data.当我们需要查询这些数据时,ChainID 是将这两个表链接在一起的一列。
I currently have it set up to where it will do the insert into [Table1] which then generates the new ChainIds.我目前已将其设置为插入 [Table1] 的位置,然后生成新的 ChainIds。 I can use "OUTPUT INSERTED.ChainID" to get the ChainId's that were generated but my problem is tying this back to the original staging table in order to grab the rest of the data for the second table.
我可以使用“OUTPUT INSERTED.ChainID”来获取生成的 ChainId,但我的问题是将其绑定回原始临时表,以便获取第二个表的数据的 rest。
DECLARE @Staging TABLE
(
[RowID] [int],
[ChainID] [varchar](50) ,
[LoanNo] [varchar](50) ,
[AssignmentFrom] [varchar](4000),
[AssignmentTo] [varchar](4000),
[CustodianUID] [nvarchar](100) null,
[DocCode] [nvarchar](100) null
)
INSERT
@Staging
SELECT
RowID,
ChainID,
LoanNo,
AssignmentFrom,
AssignmentTo,
CustodianUID,
DocCode
FROM
[MDR_CSV].[dbo].[TblCollateralAssignmentChainImport]
WHERE
UploadID = 1
This is where we do the insert into the first table which generates the new chainIds that will be needed to merge into Table2.这是我们插入第一个表的地方,该表生成新的chainId,需要合并到Table2中。
INSERT INTO
Table1
SELECT
LoanNo,
AssignmentFrom,
AssignmentTo,
CustodianUID
FROM
@Assignments AS MDRCA
WHERE
MDRCA.ChainID IS NULL
Now I need to insert the data from the DocCode field into Table2.现在我需要将 DocCode 字段中的数据插入到 Table2 中。 I can get the list of newly generated ChainIds by doing something such as
我可以通过执行以下操作来获取新生成的 ChainIds 列表
OUTPUT INSERTED.ChainID
But that doesn't help being able to tie the newly generated chainId's back to the corresponding data rows from the Staging table in order to do the insert into Table2.但这无助于将新生成的 chainId 绑定到 Staging 表中的相应数据行,以便插入到 Table2 中。
I ended up figuring out a solution.我最终想出了一个解决方案。 I used a curser to go through each row of data from the staging table one by one.
我使用光标指向 go 逐行通过暂存表中的每一行数据。 That allowed me to do the insert into the first table (with only the pertinent columns from the staging table) along with doing an OUTPUT.INSTERTED ChainID and stored that newly generated ChainID into a table variable.
这使我可以插入第一个表(仅包含临时表中的相关列)以及执行 OUTPUT.INTERTED ChainID 并将新生成的 ChainID 存储到表变量中。 I then assign that ChainID value to a regular variable by doing
然后我通过执行将该 ChainID 值分配给常规变量
@ChainID = (SELECT TOP 1 * FROM @tableVariable) @ChainID = (SELECT TOP 1 * FROM @tableVariable)
I could then use this ChainID to insert into the second table along with the rest of the data from the staging table that pertained to the same row of data (which was possible due to the curser)然后,我可以使用此 ChainID 与来自暂存表的数据的 rest 一起插入到第二个表中,这些数据与同一行数据有关(这可能是由于光标)
Then before the curser loops I drop the table variable and recreate it back at the top of the loop so that the SELECT TOP 1 works correctly each time.然后在光标循环之前,我删除表变量并在循环顶部重新创建它,以便 SELECT TOP 1 每次都能正常工作。
Not sure if it's the best or more efficient way to go about this but at least it worked!不确定这是否是 go 的最佳或更有效的方法,但至少它有效!
Here's an example showing how I got it to work:下面是一个例子,展示了我是如何让它工作的:
TblCollateralAssignmentChainImport is the Staging Table TblCollateralAssignmentChainImport 是暂存表
temp_tblCollateralAssignment is Table1 temp_tblCollateralAssignment 是 Table1
temp_CustodianData is Table2 temp_CustodianData 是表 2
DECLARE @RowID int, @ChainID int;
DECLARE import_cur CURSOR FOR
SELECT RowID
FROM [MDR_CSV].[dbo].[TblCollateralAssignmentChainImport]
WHERE ChainId IS NULL
order by RowID;
OPEN import_cur
FETCH NEXT FROM import_cur
INTO @RowID
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @NewlyCreatedChainId table (nChainId int);
INSERT INTO temp_tblCollateralAssignment
OUTPUT INSERTED.ChainID INTO @NewlyCreatedChainId
SELECT LoanNo, AssignmentFrom, AssignmentTo
FROM TblCollateralAssignmentChainImport
WHERE RowId = @RowID
SET @ChainID = (SELECT TOP 1 nChainId FROM @NewlyCreatedChainId)
INSERT INTO temp_CustodianData (ChainID, LoanNo, CustodianUID, DocCode)
SELECT @ChainID, import.LoanNo, import.CustodianUID, import.DocCode
FROM TblCollateralAssignmentChainImport AS import
WHERE RowId = @RowID
DELETE FROM @NewlyCreatedChainId
FETCH NEXT FROM import_cur
INTO @RowID
END
CLOSE import_cur;
DEALLOCATE import_cur;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.