我在表中有一个层次结构:
Configuration
(
ConfigurationId int identity primary key,
Name nvarchar(100),
Value nvarchar(100),
ParentId` int foreign key referencing ConfigurationId
)
我的任务是克隆具有所有子代的父代,并保持子代的结构。请记住,ConfigurationId
是身份,它必须保持身份并且不必从1开始。我使用与用于插入/更新的过程相同的过程,仅使用IsClone
参数。>
过程如下:
ALTER PROCEDURE [dbo].[Configuration_Save]
@ConfigurationId INT,
@Name NVARCHAR(500),
@Value NVARCHAR(500),
@ParentId INT,
@IsClone BIT
AS
BEGIN
IF @IsClone = 0
BEGIN
IF (@ConfigurationId = 0)
BEGIN
INSERT INTO [Configuration]([Name], [Value], [ParentId])
VALUES (@Name, @Value, @ParentId)
END
ELSE
BEGIN
UPDATE [Configuration]
SET [Name] = @Name,
[Value] = @Value,
ParentId = @ParentId
WHERE ConfigurationId = @ConfigurationId
END
END
ELSE -- IF IsClone = 1
BEGIN
DECLARE @SourceConfigid INT
SET @SourceConfigid = @ConfigurationId
DECLARE @ClonedConfigId INT
INSERT INTO [Configuration] ([Name], [Value], ParentId)
VALUES (@Name, @Value, NULL)
SET @ClonedConfigId = SCOPE_IDENTITY()
-- solution goes here
END
SELECT @ConfigurationId
END
当前数据如下:
ConfigurationId Name Value ParentId
-------------------------------------------------------
1 prod NULL NULL
2 Security NULL 1
3 SecurityKey NULL 2
4 Issuer NULL 2
5 Audience NULL 2
6 SyncServer NULL 1
7 Address NULL 6
8 SmtpClient NULL 1
9 Host NULL 8
10 Port NULL 8
11 EnableSsl NULL 8
12 Username NULL 8
13 Password NULL 8
14 FromEmail NULL 8
15 Proxy NULL 1
16 UseProxy NULL 15
17 ProxyAddress NULL 15
18 AddressList NULL 15
19 Report NULL 1
20 ApiUrl NULL 19
我希望能够通过插入具有我输入的名称的新根配置来克隆根配置(例如,用ParentId = NULL
,例如上面的一个用ConfigurationId = 1
和Name = prod
)。执行存储过程并将行复制到当前行,唯一的区别是ConfigurationId
(即身份)和ParentId
(应根据新的ConfigurationId
进行更改,同时保持层次结构)。
所需数据如下:
ConfigurationId Name Value ParentId
------------------------------------------------
1 prod NULL NULL
2 Security NULL 1
3 SecurityKey NULL 2
4 Issuer NULL 2
5 Audience NULL 2
6 SyncServer NULL 1
7 Address NULL 6
8 SmtpClient NULL 1
9 Host NULL 8
10 Port NULL 8
11 EnableSsl NULL 8
12 Username NULL 8
13 Password NULL 8
14 FromEmail NULL 8
15 Proxy NULL 1
16 UseProxy NULL 15
17 ProxyAddress NULL 15
18 AddressList NULL 15
19 Report NULL 1
20 ApiUrl NULL 19
21 prod2 NULL NULL
22 Security NULL 21
23 SecurityKey NULL 22
24 Issuer NULL 22
25 Audience NULL 22
26 SyncServer NULL 21
27 Address NULL 26
28 SmtpClient NULL 21
29 Host NULL 28
30 Port NULL 28
31 EnableSsl NULL 28
32 Username NULL 28
33 Password NULL 28
34 FromEmail NULL 28
35 Proxy NULL 21
36 UseProxy NULL 35
37 ProxyAddress NULL 35
38 AddressList NULL 35
39 Report NULL 21
40 ApiUrl NULL 39
与嵌套游标,合并和调用过程/函数相比,我更喜欢CTE解决方案。我尝试了几种以类似名称列出的解决方案,但没有成功。
编辑1: 示例数据的格式
编辑2: 只能克隆根节点,这意味着只有ParentId = NULL的条目才是克隆选项。
任何帮助将不胜感激。
答案 0 :(得分:0)
有许多可用的答案,显示了如何使用附加一些路径信息的递归CTE。这是一个需要根据您的排序首选项进行调整的示例:
;with cteHierarchy AS (
SELECT ConfigurationId, NAme, Value, ParentId,
CAST(ConfigurationID AS varchar(255)) As HierarchyPath
FROM #Configuration WHERE ParentId IS NULL
UNION ALL
SELECT C.ConfigurationId, C.NAme, C.Value, C.ParentId,
--I prefer CONCAT(), but not sure of your SQL version
CAST(P.HierarchyPath + '.' + CAST(C.ConfigurationID AS varchar(255)) as varchar(255)) As HierarchyPath
FROM #Configuration C
JOIN cteHierarchy P ON C.ParentId = P.ConfigurationId
)
SELECT * FROM cteHierarchy Order By HierarchyPath
答案 1 :(得分:0)
以下代码使用CTE和update
来复制指定层次结构。 CTE从根到叶递归遍历并馈送insert
,该行添加了行的“副本”。 output
上的insert
子句产生一张修正对表,其中包含每个新行的新旧ConfigurationId
值。由于output
子句只能访问插入的列值,因此我们“借用”一列(Value
)来存储旧的ConfigurationId
值。然后,使用update
来设置两列:ParentId
的值将更新以引用复制的行,而Value
的值将从原始行中恢复。
请注意,繁忙的工作应包装在事务中。它可以确保复制完成或没有遗漏,并且需要防止其他会话看到不完整的结果或更改完成复制所需的旧Value
数据。
-- Sample data.
declare @Configuration as Table (
ConfigurationId Int Identity,
Name NVarChar(100),
Value NVarChar(100),
ParentId Int );
insert into @Configuration ( Name, Value, ParentId ) values
( 'prod', NULL, NULL ),
( 'Security', NULL, 1 ),
( 'SecurityKey', NULL, 2 ),
( 'Issuer', NULL, 2 ),
( 'Audience', NULL, 2 ),
( 'SyncServer', NULL, 1 ),
( 'Address', NULL, 6 );
--8 SmtpClient NULL 1
--9 Host NULL 8
--10 Port NULL 8
--11 EnableSsl NULL 8
--12 Username NULL 8
--13 Password NULL 8
--14 FromEmail NULL 8
--15 Proxy NULL 1
--16 UseProxy NULL 15
--17 ProxyAddress NULL 15
--18 AddressList NULL 15
--19 Report NULL 1
--20 ApiUrl NULL 19
-- Raw sample data.
select * from @Configuration;
-- Tree sample data.
with Configuration as (
select ConfigurationId, Name, Value, ParentId,
Cast( Right( '0000' + Cast( ConfigurationId as NVarChar(4) ), 4 ) as NVarChar(1024) ) as Path
from @Configuration
where ParentId is NULL
union all
select CC.ConfigurationId, CC.Name, CC.Value, CC.ParentId,
Cast( Path + N'→' + Right( '0000' + Cast( CC.ConfigurationId as NVarChar(4) ), 4 ) as NVarChar(1024) )
from Configuration as PC inner join
@Configuration as CC on CC.ParentId = PC.ConfigurationId )
select *
from Configuration
order by Path;
-- Copy the tree.
declare @RootConfigurationId as Int = 1;
declare @Fixups as Table ( OriginalConfigurationId NVarChar(10), CopyConfigurationId Int );
-- NB: The isolation level needs to guarantee that the Value in the
-- source rows doesn't get changed whilst we fiddle about, nor do we want anyone else peeking.
begin transaction;
-- Copy the tree and save the new identity values.
-- We cheat and tuck the old ConfigurationId into the Value column so that the
-- output clause can save the original and copy ConfigurationId values for fixup.
with Configuration as (
select ConfigurationId, Name, Value, ParentId
from @Configuration
where ConfigurationId = @RootConfigurationId
union all
select CC.ConfigurationId, CC.Name, CC.Value, CC.ParentId
from Configuration as PC inner join
@Configuration as CC on CC.ParentId = PC.ConfigurationId )
insert into @Configuration ( Name, Value, ParentId )
output inserted.Value, inserted.ConfigurationId into @Fixups
select Name, Cast( ConfigurationId as NVarChar(10) ), ParentId
from Configuration as C;
-- Display the intermediate results.
select * from @Fixups;
select * from @Configuration;
-- Fix up the parentage and replace the original values.
update C
set C.ParentId = F2.CopyConfigurationId, Value = CV.Value
from @Configuration as C inner join -- New rows to be fixed.
@Fixups as F on F.CopyConfigurationId = C.ConfigurationId inner join -- New row identity values.
@Configuration as CV on CV.ConfigurationId = F.OriginalConfigurationId left outer join -- Original Value .
@Fixups as F2 on F2.OriginalConfigurationId = C.ParentId; -- Lookup the new ParentId , if any, for each row.
-- Raw sample data.
select * from @Configuration;
-- Tree sample data.
with Configuration as (
select ConfigurationId, Name, Value, ParentId,
Cast( Right( '0000' + Cast( ConfigurationId as NVarChar(4) ), 4 ) as NVarChar(1024) ) as Path
from @Configuration
where ParentId is NULL
union all
select CC.ConfigurationId, CC.Name, CC.Value, CC.ParentId,
Cast( Path + N'→' + Right( '0000' + Cast( CC.ConfigurationId as NVarChar(4) ), 4 ) as NVarChar(1024) )
from Configuration as PC inner join
@Configuration as CC on CC.ParentId = PC.ConfigurationId )
select *
from Configuration
order by Path;
commit transaction;