子女父母关系

时间:2019-02-15 02:24:26

标签: sql sql-server

将数据从表1复制到表2。我的代码第一次使用,第二次在ParentId中不发送0

我的情况需要这些要点来实现:

  1. 父子层次可以达到n级。
  2. 父母可以有很多孩子。
  3. 表2不应具有表1的ID和父ID数据,但它必须具有自己的ID和父ID,但请记住,两个表之间的父子关系必须匹配。

    -- Table 1
    create table #Table1 (
    Id int, ParentId int, Name varchar(50))
    insert into #Table1 values(6,0, 'person1')
    insert into #Table1 values(7,0, 'person2')
    insert into #Table1 values(8,7, 'person3')
    insert into #Table1 values(9,6, 'person4')
    SELECT * from #Table1
    
    -- Table 2
    create table #Table2 (
    Id int IDENTITY(1,1) PRIMARY KEY, ParentId int, Name varchar(50))
    
    -- below code copy table1 data into table2 on the first try, the second time it does not take correct data.
    SET IDENTITY_INSERT #Table2 ON
    declare @MaxId int;
    select @MaxId=max(id)from #table2; 
    
    ;WITH NewIDs AS (
      SELECT OriginalID=T.Id, ReplacementID=ROW_NUMBER() OVER (ORDER BY T.ID ASC)
      FROM #Table1 AS T
    )
    INSERT INTO #Table2(Id, ParentId, Name)
      SELECT Id=N1.ReplacementID+coalesce(@MaxId, 0),
        ParentId=ISNULL(N2.ReplacementID, 0)+coalesce(@MaxId, 0),
        Name=T.Name
      FROM #Table1 AS T
      INNER JOIN NewIDs AS N1 ON T.Id=N1.OriginalID
      LEFT JOIN NewIDs AS N2 ON T.ParentId=N2.OriginalID
    
    SET IDENTITY_INSERT #Table2 OFF
    
    SELECT * from #Table1
    SELECT * from #Table2
    

**两次执行后,我得到了这个错误的信息(上一个是表1,下一个是table2)**

it has 4 and 4 in parentId which is wrong, i want 0 and 0

我期望的结果是

it has 0 and 0, this is what i want.

1 个答案:

答案 0 :(得分:2)

好-我不是100%确信这可以解决您更广泛的业务逻辑问题,但是确实会产生您期望的结果。基本上,当您要创建替换ID时,它必须从表中的最后一个现有ID开始,因此下面将执行此操作。

-- Table 1

create table #Table1 (Id int, ParentId int, Name varchar(50))
insert into #Table1 values(6,0, 'person1')
insert into #Table1 values(7,6, 'person2')
insert into #Table1 values(8,7, 'person3')
insert into #Table1 values(9,7, 'person4')
insert into #Table1 values(10,7, 'person5')
insert into #Table1 values(11,9, 'person6')
insert into #Table1 values(12,11, 'person7')
insert into #Table1 values(13,7, 'person8')

SELECT * from #Table1

-- Table 2
create table #Table2 (Id int IDENTITY(1,1) PRIMARY KEY, ParentId int, Name varchar(50))

-- below code copy table1 data into table2 on first
-- try, the second time it does not work.
SET IDENTITY_INSERT #Table2 ON

declare @MaxId int; -- Change Line 1
select @MaxId = max(id) from #table2; -- Change Line 2

;WITH NewIDs AS
(
  SELECT
    OriginalID = T.Id
    , ReplacementID = ROW_NUMBER() OVER (ORDER BY T.ID ASC)
    , ParentId
  FROM #Table1 AS T
)
INSERT INTO #Table2 (Id, ParentId, [Name])
  SELECT
    N1.ReplacementID + coalesce(@MaxId,0) -- Change Line 3
    , case when T.ParentId = 0 then 0 else ISNULL(N2.ReplacementID, 0) + coalesce(@MaxId,0) end -- Change Line 4
    , T.[Name]
  FROM #Table1 AS T
  INNER JOIN NewIDs AS N1 ON T.Id = N1.OriginalID
  LEFT JOIN NewIDs AS N2 ON T.ParentId = N2.OriginalID

SET IDENTITY_INSERT #Table2 OFF

SELECT * from #Table1
SELECT * from #Table2