如何获取插入子项的父ID的id

时间:2016-12-26 20:08:40

标签: sql-server sql-server-2012

我有父子表。

目标是复制记录,但新主键除外。

原始表

void test(double **x)
{
    *x = (double *)malloc(2*sizeof(double));
    (*x)[0] = 2;
    (*x)[1] = 3;
}
int main()
{
    double *x = NULL;
    test(&x);

    printf(" %f", x[0]); //this is where it crashes with no message 
}

插入后:

Parent(id)
1

Child(id,parentId, data)
1,1
2,1

我该怎么做?我遇到问题的部分是获取新的父密钥以用于子记录。

这是我到目前为止所提出的。

Parent
1
2

Child
1,1
2,1
3,2
4,2

2 个答案:

答案 0 :(得分:1)

只需使用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'

现在在Parent&amp ;;中进行插入儿童表

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

还有其他方法可以获取“插入的身份”值。

请参阅文档@@IDENTITY (Transact-SQL)SCOPE_IDENTITY

答案 1 :(得分:0)

尝试以下方法:

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值。