I have only found this using a classic ParentID foreign key method. I wonder how to display full hierarchy path using the HierarchyID
data type in SQL Server 2019.
I have this setup (simplified for this example):
CREATE TABLE tbl2ProductCategories (
-- Primary Key Field
ProductCategoryID INT NOT NULL IDENTITY(1,1),
-- Non Key Fields
Node HIERARCHYID NOT NULL UNIQUE,
ProductCategory NVARCHAR(50) NOT NULL,
-- Constraints
CONSTRAINT PK_tbl2ProductCategories PRIMARY KEY (ProductCategoryID)
);
CREATE TABLE tbl1Products (
-- Primary Key Field
ProductID INT NOT NULL IDENTITY(1,1),
-- Non Key Fields
ProductName NVARCHAR(50) NOT NULL UNIQUE,
-- Constraints
CONSTRAINT PK_tbl1Products PRIMARY KEY (ProductID),
);
-- Each product can be in multiple categories
CREATE TABLE tbl3ProductsCategories (
-- Primary Key Field
ProductID INT NOT NULL,
ProductCategoryID INT NOT NULL,
-- Non Key Fields
IsPrimaryCategory BIT NOT NULL DEFAULT 1
-- Constraints
CONSTRAINT PK_tbl3ProductsCategories PRIMARY KEY (ProductID, ProductCategoryID),
CONSTRAINT FK_tbl3ProductsCategories_tbl1Products FOREIGN KEY (ProductID) REFERENCES tbl1Products (ProductID),
CONSTRAINT FK_tbl3ProductsCategories_tbl2ProductCategories FOREIGN KEY (ProductCategoryID) REFERENCES tbl2ProductCategories (ProductCategoryID)
);
Now for each product, I want to display its full category path where the category is marked as primary. For example:
- Product 1: Products > Category1 > Subcategory1 > Subsubcategory1
- Product 2: Products > Category1 > Subcategory3
- Product 3: Products > Category5
I tried to write the recursive CTE myself but I got lost. I got stuck at the point below:
WITH category_hierarchy(ProductID, ProductName, FullPath, Level) AS
(
SELECT ProductID, ProductName, 0 AS Level, 'Produkty' AS FullPath FROM tbl1Products
UNION ALL
SELECT ProductID, ProductName, Level+1 AS Level, FullPath FROM category_hierarchy WHERE
)
SELECT * FROM category_hierarchy;
Can you please help me point me in the right direction?