Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Need advice for getting parent hierarchy path

 
   Database Help (Home) -> Programming RSS
Next:  detail and grouping  
Author Message
Sam78

External


Since: Aug 19, 2004
Posts: 50



(Msg. 1) Posted: Mon May 10, 2010 12:54 pm
Post subject: Need advice for getting parent hierarchy path
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

I'm using HierarchyID for creating a products category in my application. As
I allow users to create sub categories, I want to display the entire path of
the parent category so that they can see where they're inserting the new node.

For example: I want to display Root > Electronics > Digital Media Players

Obviously, I need to start by getting this info. If I pass the HierarchyID
of the parent, how do I get the whole path?
--
Thanks,

Sam

 >> Stay informed about: Need advice for getting parent hierarchy path 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 2) Posted: Mon May 10, 2010 11:13 pm
Post subject: Re: Need advice for getting parent hierarchy path [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Here is one example:

CREATE TABLE Foo (
category_hid HIERARCHYID NOT NULL PRIMARY KEY,
category_name VARCHAR(30));

INSERT INTO Foo VALUES(hierarchyid::GetRoot(), 'Root');

DECLARE @hid HIERARCHYID;
SELECT @hid = hierarchyid::GetRoot()
FROM Foo;

INSERT INTO Foo VALUES(@hid.GetDescendant(NULL, NULL), 'Electronics');

SELECT @hid = category_hid
FROM Foo
WHERE category_name = 'Electronics';

INSERT INTO Foo VALUES(@hid.GetDescendant(NULL, NULL), 'Digital Media
Players');

WITH Hierarchy AS (
SELECT category_hid, category_name, CAST(category_name AS
VARCHAR(MAX)) AS category_path
FROM Foo
WHERE category_hid = hierarchyid::GetRoot()
UNION ALL
SELECT F.category_hid, F.category_name, H.category_path + ' > ' +
F.category_name
FROM Foo AS F
JOIN Hierarchy AS H
ON F.category_hid.GetAncestor(1) = H.category_hid)
SELECT category_hid, category_name, category_path
FROM Hierarchy;

/*

category_hid category_name category_path
-------------- ------------------------------
--------------------------------------------
0x Root Root
0x58 Electronics Root > Electronics
0x5AC0 Digital Media Players Root > Electronics >
Digital Media Players

*/

--
Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: Need advice for getting parent hierarchy path 
Back to top
Login to vote
Sam78

External


Since: Aug 19, 2004
Posts: 50



(Msg. 3) Posted: Tue May 11, 2010 9:59 am
Post subject: Re: Need advice for getting parent hierarchy path [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Plamen,

Absolutely fantastic! Thank you very much!

--
Thanks,

Sam


"Plamen Ratchev" wrote:

> Here is one example:
>
> CREATE TABLE Foo (
> category_hid HIERARCHYID NOT NULL PRIMARY KEY,
> category_name VARCHAR(30));
>
> INSERT INTO Foo VALUES(hierarchyid::GetRoot(), 'Root');
>
> DECLARE @hid HIERARCHYID;
> SELECT @hid = hierarchyid::GetRoot()
> FROM Foo;
>
> INSERT INTO Foo VALUES(@hid.GetDescendant(NULL, NULL), 'Electronics');
>
> SELECT @hid = category_hid
> FROM Foo
> WHERE category_name = 'Electronics';
>
> INSERT INTO Foo VALUES(@hid.GetDescendant(NULL, NULL), 'Digital Media
> Players');
>
> WITH Hierarchy AS (
> SELECT category_hid, category_name, CAST(category_name AS
> VARCHAR(MAX)) AS category_path
> FROM Foo
> WHERE category_hid = hierarchyid::GetRoot()
> UNION ALL
> SELECT F.category_hid, F.category_name, H.category_path + ' > ' +
> F.category_name
> FROM Foo AS F
> JOIN Hierarchy AS H
> ON F.category_hid.GetAncestor(1) = H.category_hid)
> SELECT category_hid, category_name, category_path
> FROM Hierarchy;
>
> /*
>
> category_hid category_name category_path
> -------------- ------------------------------
> --------------------------------------------
> 0x Root Root
> 0x58 Electronics Root > Electronics
> 0x5AC0 Digital Media Players Root > Electronics >
> Digital Media Players
>
> */
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> .
>
 >> Stay informed about: Need advice for getting parent hierarchy path 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Get Hierarchy - Table1 contains serverall thousand rows. The concerning columns are EmployeeNumber and ManagerEmployeeNumber. I need to get all direct and indirect employees based on the manager supplied. There are potentially many levels. So if I specify manage...

hierarchy query - I have a table that looks like this. Each row is a reply or a topic when IsAReplyTo = 0 I need to write a query that pulls all rows of a hierarchy of a given ID Table Forum ID IsAReplyTo 4 0 9 4 27 4 63 27 73...

Hierarchy ID - Help - Hi, Since the HierarchyID is of variable length datatype, The following is the snippet from msdn , "The average number of bits that are required to represent a node in a tree with n nodes depends on the average fanout (the average number of chil...

Representing a User Roles Hierarchy - Hi, suppose I have this four tables to represent a User - Role hierarchy: /* Users -------------------------------------------------------------------- */ CREATE TABLE Users ( ID int IDENTITY(1,1) NOT NULL, ...

Hierarchy loop lookup syntax - I have the following Table USE [MyDatabase] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Customer.Location]( [LocationID] [int] NOT NULL, [CustomerID] [smallint] NOT NULL, [LocationName] [nvarchar](30...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]