I have a table named tb_Section. It is designed to support n-level hierarchy. Each row has a ParentSetionId that points to another row's SectionId in case of a Parent/Child relationship.
Now, I have a process where I have to copy these sections one by one to another table keeping the hierarchy intact. I arranged the section by Using order by as follows:
Select
s.SectionId,
s.ParentSectionId,
s.SectionName
From
tb_Section s
Where
s.TemplateId = 123
Order By
Convert(hierarchyid, '/' + cast(sec.ParentSectionId as varchar)+ '/')
I insert these in a TempTable and Insert each section one by one in another table. This was working fine. But after the application went live an issue was reported.
A user firstly created all the sections as Parent Sections and then one by one edited them and made them child of other sections(Used the most difficult way :/) The section hierarchy was as follows:
Super-Parent
Parent1
Child1
Child2
Parent2
Child3
Child4
The above query in this case returns the following rows in order.
SuperParent
Child1
Child2
Parent1
Child3
Child4
Parent2
Instead of the normal case, which should be this
SuperParent
Parent1
Child1
Child2
Parent2
Child3
Child4
Any help regarding this? I can't seem to find the reason why this is happening.
UPDATE
Here is the SQL Script where Children are inserted first.
CREATE TABLE tb_Section(
SectionId BIGINT NOT NULL PRIMARY KEY
,ParentSectionId BIGINT
,SectionName NVARCHAR(50) NOT NULL
);
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (SectionId,ParentSectionId,N'SectionName');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686772,686776,N'Child 1');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686773,686776,N'Child 2');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686774,686777,N'Child 3');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686775,686777,N'Child 4');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686776,686778,N'Parent 1');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686777,686778,N'Parent 2');
INSERT INTO tb_Section(SectionId,ParentSectionId,SectionName) VALUES (686778,NULL,N'Super-Parent');
Here is the result my above query returns with hierarchyId
+-----------+-----------------+---------------+--------------+----------------+
| SectionId | ParentSectionId | SectionTypeId | SectionName | hierarchy |
+-----------+-----------------+---------------+--------------+----------------+
| 686778 | NULL | 1 | Super-Parent | NULL |
| 686772 | 686776 | 2 | Child 1 | 0xF800298C6620 |
| 686773 | 686776 | 2 | Child 2 | 0xF800298C6620 |
| 686774 | 686777 | 2 | Child 3 | 0xF800298C6660 |
| 686775 | 686777 | 2 | Child 4 | 0xF800298C6660 |
| 686776 | 686778 | 2 | Parent 1 | 0xF800298C66A0 |
| 686777 | 686778 | 2 | Parent 2 | 0xF800298C66A0 |
+-----------+-----------------+---------------+--------------+----------------+
You can see that the hierarchy is different, here child items are showing before the Parents. I hope this sample data helps.