Sql server 如何获取id';用于插入子项的父ID的数目
我有父表和子表 目标是复制记录,除非使用新的主键 原始表格Sql server 如何获取id';用于插入子项的父ID的数目,sql-server,sql-server-2012,Sql Server,Sql Server 2012,我有父表和子表 目标是复制记录,除非使用新的主键 原始表格 Parent(id) 1 Child(id,parentId, data) 1,1 2,1 插入后: Parent 1 2 Child 1,1 2,1 3,2 4,2 我该怎么做?我遇到的问题是获取用于子记录的新父密钥 这就是我到目前为止所想到的 --DECLARE VARS declare @currentMetadataDocumentSetId int = 1, --Ohio @newMetadataDocumen
Parent(id)
1
Child(id,parentId, data)
1,1
2,1
插入后:
Parent
1
2
Child
1,1
2,1
3,2
4,2
我该怎么做?我遇到的问题是获取用于子记录的新父密钥
这就是我到目前为止所想到的
--DECLARE VARS
declare @currentMetadataDocumentSetId int = 1, --Ohio
@newMetadataDocumentSetid int = 3; --PA
--CLEANUP
IF OBJECT_ID('tempdb..#tempFileRowMap') IS NOT NULL
/*Then it exists*/
DROP TABLE #tempFileRowMap
--Remove existing file row maps.
delete from file_row_map where metadata_document_set_id = @newMetadataDocumentSetid;
--Create a temptable to hold data to be copied.
Select [edi_document_code],
[functional_group],
[description],
3 as [metadata_document_set_id],
[document_name],
[incoming_file_row_subtype],
[metadata_document_id],
[document_subcode],
[outgoing_file_row_subtype],
[asi_type_code],
[asi_action_code],
[metadata_document_set],
file_row_map_id as orig_file_row_map_id
into #tempFileRowMap
from file_row_map fileRowMap
where metadata_document_set_id = @currentMetadataDocumentSetId;
--Select * from #tempFileRowMap;
Insert into file_row_map select
[edi_document_code],
[functional_group],
[description],
[metadata_document_set_id],
[document_name],
[incoming_file_row_subtype],
[metadata_document_id],
[document_subcode],
[outgoing_file_row_subtype],
[asi_type_code],
[asi_action_code],
[metadata_document_set]
from #tempFileRowMap
--Show Results
Select * from file_row_map fileRowMap where fileRowMap.metadata_document_set_id = @newMetadataDocumentSetid
--Update Detail
Select
[file_row_map_id],
[file_row_column],
[element_code],
[element_metadata_id],
[col_description],
[example],
[translate],
[is_used],
[is_mapped],
[page_num],
[subcode],
[qualifier],
[loop_code],
[loop_subcode],
[default_value],
[delete_flag]
into #tempFileRowMapDetail
from [dbo].[file_row_map_detail] d
left join #tempFileRowMap m
on m.orig_file_row_map_id = d.file_row_map_id
select * from #tempFileRowMapDetail
尝试以下方法:
DECLARE @Table1 TABLE (
ID INT NOT NULL PRIMARY KEY,
ParentID INT NULL, -- FK
[Desc] VARCHAR(50) NOT NULL
);
INSERT @Table1 (ID, ParentID, [Desc])
VALUES
(1, NULL, 'A'),
(2, 1, 'AA.1'),
(3, 1, 'AA.2'),
(4, NULL, 'B'),
(5, 4, 'BB.1'),
(6, 4, 'BB.2'),
(7, 4, 'BB.3'),
(8, 7, 'BBB.1');
DECLARE @ParentID INT = 4;
DECLARE @LastID INT = (SELECT TOP(1) ID FROM @Table1 x ORDER BY x.ID DESC)
IF @LastID IS NULL
BEGIN
RAISERROR('Invalid call', 16, 1)
--RETURN ?
END
SELECT @LastID AS LastID;
/*
LastID
-----------
8
*/
DECLARE @RemapIDs TABLE (
OldID INT NOT NULL PRIMARY KEY,
[NewID] INT NOT NULL UNIQUE
);
WITH CteRecursion
AS (
SELECT 1 AS Lvl, crt.ID, crt.ParentID --, crt.[Desc]
FROM @Table1 crt
WHERE crt.ID = @ParentID
UNION ALL
SELECT cld.Lvl + 1 AS Lvl, crt.ID, crt.ParentID --, crt.[Desc]
FROM @Table1 crt
JOIN CteRecursion cld ON crt.ParentID = cld.ID
)
INSERT @RemapIDs (OldID, [NewID])
SELECT r.ID, @LastID + ROW_NUMBER() OVER(ORDER BY r.Lvl) AS [NewID]
FROM CteRecursion r;
--INSERT @Table1 (ID, ParentID, [Desc])
SELECT nc.[NewID] AS ID, np.[NewID] AS ParentID, o.[Desc]
FROM @Table1 o -- old
JOIN @RemapIDs nc /*new child ID*/ ON o.ID = nc.OldID
LEFT JOIN @RemapIDs np /*new parent ID*/ ON o.ParentID = np.OldID
/*
ID ParentID Desc
----------- ----------- --------------------------------------------------
9 NULL B
10 9 BB.1
11 9 BB.2
12 9 BB.3
13 12 BBB.1
*/
注:有一些小的变化,应该工作w。许多
ParentID
s值。只需使用OUTPUT
子句即可获得精确的父表主键值
让我们为您的案例构建示例模式
--For Capturing inserted ID
CREATE TABLE #ID_CAPTURE (PARENT_ID INT,ORDER_NME VARCHAR(20));
--Your Intermidiate Data To insert into Actual Tables
CREATE TABLE #DUMMY_TABLE (ORDER_NME VARCHAR(20), ITEM_NME VARCHAR(20));
--Actual Tables
CREATE TABLE #ORDER_PARENT (ORDER_ID INT IDENTITY,ORDER_NME VARCHAR(20))
CREATE TABLE #ORDER_CHILD (CHILD_ID INT IDENTITY ,ORDER_ID INT, ORDER_NME VARCHAR(20))
INSERT INTO #DUMMY_TABLE
SELECT 'BILL1','Oil'
UNION ALL
SELECT 'BILL1', 'Gas'
UNION ALL
SELECT 'BILL2', 'Diesel'
现在在父表和子表中执行插入操作
INSERT INTO #ORDER_PARENT
OUTPUT inserted.ORDER_ID, inserted.ORDER_NME into #ID_CAPTURE
SELECT DISTINCT ORDER_NME FROM #DUMMY_TABLE
INSERT INTO #ORDER_CHILD
SELECT C.PARENT_ID, ITEM_NME FROM #DUMMY_TABLE D
INNER JOIN #ID_CAPTURE C ON D.ORDER_NME = C.ORDER_NME
SELECT * FROM #ID_CAPTURE
SELECT * FROM #ORDER_CHILD
还有其他方法可以获取插入的标识值
请参阅文档,子ID列是否自动编号?@ZoharPeled,合并上有趣的链接。以前不知道“合并”。@HolmesIV是的,Child\u ID是自动编号。您能补充一点说明吗?我相信这是一个很好的答案,但我的技能不足以仅仅从SQL理解它。请参阅“深层次的第一顺序比较”一节