SQL - 克隆记录及其后代

时间:2011-05-09 23:45:33

标签: sql sql-server tsql sql-server-2008

我希望能够在同一个表中克隆记录及其后代。我的表的一个例子如下:

表1

id | parentid | name
---------------------
 1 |    0     |  'Food'
 2 |    1     |  'Taste'
 3 |    1     |  'Price'
 4 |    2     |  'Taste Requirements'

“id”列是主键并自动递增。 “食物”记录(即id = 1)在其下面有两个记录,称为“品味”和“价格”。 “味道”记录下面有一个名为“味道要求”的记录。我希望能够克隆'Food'记录,以便Table1看起来如下所示:

表1

id | parentid | name
---------------------
 1 |    0     |  'Food'
 2 |    1     |  'Taste'
 3 |    1     |  'Price'
 4 |    2     |  'Taste Requirements'
 5 |    0     |  'Cookies'
 6 |    5     |  'Taste'
 7 |    5     |  'Price'
 8 |    6     |  'Taste Requirements'

(其中'Cookies'是我想要创建的新类别的名称)。我可以使用以下方法选择'Food'的所有后代:

with Table1_CTE( id, parentid, name )
as
(
  select t.id, t.parentid, t.name from Table1 t
    where t.id = 1
  union all
  select t.id, t.parentid,t. name from Table1 t
    inner join Table1_CTE as tc
      on t.parentid = tc.id
)
select id, parentid, name from Table1_CTE

我可以使用:

克隆'Food'记录(即id = 1)
insert into Table1 ( parentid, name )
  select ( parentid, 'Cookies' ) 
  from Table1 where id = 1

但是我在尝试将两个查询组合起来克隆'Food'的后代时遇到了问题。此外,我试图避免使用存储过程,触发器,curosrs等。我正在尝试做什么?我在网上看到了一些例子,但无法将它们应用到我的要求中。

2 个答案:

答案 0 :(得分:2)

正如Martin建议的那样,您需要启用IDENTITY_INSERT,以便您可以推送自己的身份值。您可能还需要获取表锁以确保Max(Id)返回正确的值。

If object_id('tempdb..#TestData') is not null
    Drop Table #TestData
GO
Create Table #TestData
    (
    Id int not null identity(1,1) Primary Key
    , ParentId int not null
    , Name varchar(50) not null
    )
GO
Set Identity_Insert #TestData On
GO  
Insert #TestData( Id, ParentId, Name )
Values( 1,0,'Food' )
    , ( 2,1,'Taste' )
    , ( 3,1,'Price' )
    , ( 4,2,'Taste Requirement' );


With Data As
    (
    Select Cast(MaxId.Id + 1 As int) As Id
        , T.ParentId
        , 'Copy Of ' + T.name As Name
        , T.Id As OldId
        , 0 As OldParentId
    From #TestData As T
        Cross Join( Select Max( id ) As Id From #TestData ) As MaxId
    Where T.Name = 'Food'
    Union All
    Select Cast(Parent.id + Row_Number() Over( Order By Child.Id ) + 1 As int)
        , Parent.Id
        , 'Copy of ' + Child.Name
        , Child.Id
        , Child.ParentId
    From Data As Parent
        Join #TestData As Child
            On Child.ParentId = Parent.OldId
    )
Insert #TestData( Id, ParentId, Name )
Select Id, ParentId, Name
From Data
GO
Set Identity_Insert #TestData Off
GO  

结果

id | parentid | name
-- | -------- | -----------------
1  | 0        | Food
2  | 1        | Taste
3  | 1        | Price
4  | 2        | Taste Requirement
5  | 0        | Copy Of Food
7  | 5        | Copy of Taste
8  | 5        | Copy of Price
9  | 7        | Copy of Taste Requirement

答案 1 :(得分:0)

假设您的CTE选择了一个根记录及其所有后代(当我使用上面的数据进行复制时,它似乎没有),那么您可以克隆所有选定的记录并插入如下:

with Table1_CTE( id, parentid, name )
as
(
  select t.id, t.parentid, t.name from Table1 t
    where c.icategoryid = 1
  union all
  select t.id, t.parentid,t. name from Table1
    inner join Table1_CTE as tc
      on t.parentid = tc.id
)
insert into  dbo.testinsertheirarchy  ( parentid, name )  
select  parentid, name from Table1_CTE