I'm trying to convert a MySQL DB to MSSQL DB but I'm experimenting some issues when translating the queries, in particular with the following:
MySQL
SELECT GROUP_CONCAT( parent.Name
ORDER BY parent.Lft
SEPARATOR "«" ) AS Path, node.Description
FROM `DB`.`Categories` AS node, `DB`.`Categories` AS parent
WHERE node.Lft BETWEEN parent.Lft AND parent.Rgt AND node.ID = 2
GROUP BY node.Name, node.ID
ORDER BY node.Lft LIMIT 1;
And the result I got is this, which is the one I want to obtain:
+-------------------------------+
| Path |
+-------------------------------+
| Master Node « Vehicles « Cars |
+-------------------------------+
MS SQL
I tried to recreate the MySQL query using the suggestions from this post https://stackoverflow.com/a/5981860/2098159
SELECT STUFF((SELECT ' « ' + [Name] FROM [DB].[dbo].[Categories] FOR XML PATH ('')), 1, 1, '') AS [Path]
FROM [DB].[dbo].[Categories] AS node, [DB].[dbo].[Categories] AS parent
WHERE node.[Lft] BETWEEN parent.[Lft] AND parent.[Rgt] AND node.[ID] = 7
GROUP BY node.[Name], node.[ID], node.[Lft] ORDER BY node.[Lft];
And the result I got is this, no matter which ID I choose:
+---------------------------------------------+
| Path |
+---------------------------------------------+
| « Master Node « Vehicles « Cars « Computers |
+---------------------------------------------+
At this point I only create a Table with 4 records: Master Node, Vehicles, Cars and Computers; just for testing.
I don't know what I'm missing. Does anyone has a suggestion?
Thanks in advance.