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

Sql2008 HierarchyID

 
   Database Help (Home) -> Programming RSS
Next:  Stacking SQL in one script  
Author Message
user2308

External


Since: Dec 22, 2004
Posts: 21



(Msg. 1) Posted: Tue Apr 22, 2008 12:11 pm
Post subject: Sql2008 HierarchyID
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi all... just a quick question for those who are lucky enough to be playing
around with Sql2008 yet...

The new HierarchyID datatype, will this datatype automatically prevent the
possibility of endless recursion?
That is, will it prevent a node in the hierarchy from being in it's own
ancestor path?

e.g. 5 --> 2 --> 3 --> 4 --> 1 --> 5 --> 2 --> 3 --> ... ad infinitum

That is definitely the biggest headache of trying to model tree-data
currently with PARENTID/CHILDID fields.

CheerZ,
- Arthur Dent.

 >> Stay informed about: Sql2008 HierarchyID 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 2) Posted: Tue Apr 22, 2008 12:11 pm
Post subject: Re: Sql2008 HierarchyID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Apr 22, 11:11 am, "Arthur Dent"
wrote:
> Hi all... just a quick question for those who are lucky enough to be playing
> around with Sql2008 yet...
>
> The new HierarchyID datatype, will this datatype automatically prevent the
> possibility of endless recursion?
> That is, will it prevent a node in the hierarchy from being in it's own
> ancestor path?
>
> e.g. 5 --> 2 --> 3 --> 4 --> 1 --> 5 --> 2 --> 3 --> ... ad infinitum
>
> That is definitely the biggest headache of trying to model tree-data
> currently with PARENTID/CHILDID fields.
>
> CheerZ,
> - Arthur Dent.

an alternative approach, the materialized path, allows for a very easy
prevention of cycles

 >> Stay informed about: Sql2008 HierarchyID 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 3) Posted: Tue Apr 22, 2008 12:11 pm
Post subject: Re: Sql2008 HierarchyID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> The new HierarchyID data type, will this data type automatically prevent the possibility of endless recursion? That is definitely the biggest headache of trying to model tree-data currently with (parent_node_id, child_node_id) fields [sic:columns]. <<

I think you meant "prevent cycles" (structure concept) instead of
"endless recursion" (procedural concept). The answer as I understand
it, Nope. The HierarchyID is a proprietary version of path
enumeration, and the constraint for preventing cycles in that model is
pretty ugly. You have to check all the path strings for duplicate
node_ids. The only way I know to do this anywhere close to fast, is
to replace every node_id with an empty string in every path string to
see if it will change the length of the path by more than one
node_id.
 >> Stay informed about: Sql2008 HierarchyID 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 4) Posted: Tue Apr 22, 2008 1:28 pm
Post subject: Re: Sql2008 HierarchyID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

A column of data type HIERARCHYID does not represent a tree or graph
automatically. It is up to you to assign values that represent the desired
relationship between nodes.

The HIERARCHYID data type does not prevent cyclic paths by itself. You can
easily simulate one:

CREATE TABLE Employees (
emp_id INT NOT NULL,
manager_id INT,
org_chart_path HIERARCHYID);

INSERT INTO Employees
VALUES (1, NULL, '/1/'),
(2, 1, '/1/2/'),
(3, 2, '/1/2/3/'),
(1, 3, '/1/2/3/1/');

You have to handle it via constraints. In the example below the PRIMARY KEY
will not allow cyclic occurrences:

CREATE TABLE Employees (
emp_id INT NOT NULL PRIMARY KEY,
manager_id INT REFERENCES Employees(emp_id),
org_chart_path HIERARCHYID);

This way the HIERARCHYID data type can be used to address problems that
involve cyclic graphs as well as tree structures.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Sql2008 HierarchyID 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 5) Posted: Tue Apr 22, 2008 3:16 pm
Post subject: Re: Sql2008 HierarchyID [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Alex,

The HierarchyID is actually implemented as materialized path. In the example
I posted you could easily add a CHECK constraint:

CHECK (org_chart_path.ToString() NOT LIKE '%/' + CAST(emp_id AS VARCHAR(10))
+ '/_%')

That will restrict the path to prevent cycles.

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Sql2008 HierarchyID 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
sending HierarchyID from .Net - Pls. Help - I want to pass a node info and its parent node hierarchyid (sql server 2008) from .NET middle tier. I don't think that hierarachyID is available in sqltype of .net 3.5 framework. One work around could be converting hierarchyid to String and pass it a...

need help with query - I want to sum the records in a table (u) which have 'P.M.' in a field value and test against a fixed limit something to the effect of : case when SUM(substring(u.sTime,1,4)='P.M.') &lt;= 64 Then .... except it should work ...

Next?? - I have a field stored as NTEXT(16). I need to replace some of the content in this field. I know the replace function doesnt work. I see there is UPDATETEXT - is this the ONLY way to do it ?? 'cos it looks quite complicated? thanks, Justin

How can I get the resultset from a stored procedure - I have a stored procedure which will output a table. How can I use a sql statement to get the result of the sp? Thanks a lot

Audit V2 - I am looking to write a SQL script that will show me for each table, in all the databases on a server, which users / groups have access. In addition, I want to break the access into whether it is SELECT, UPDATE, INSERT or DELETE permissions. Is this..
   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 ]