May 22, 2000 04:29 PM

Maintaining Hierarchies

Rating: (0)
SQL Server Magazine
InstantDoc ID #8826
Use triggers and T-SQL to create a functional and self-maintaining solution for managing hierarchical data
A hot Internet startup lures Andrew away to a new job, so Steven and Michael—who reported to Andrew—need a new manager. Superstar Janet takes Steven and Michael under her management wing, even though she already directly manages Robert, Laura, and Ann. Robert, in turn, manages his own team of employees. But recently, Robert has been looking for better h...

...This article is for paid Professional Members only.

Already a Professional Member? Please log in now:

NOT A PROFESSIONAL MEMBER? YOU CHOOSE:

Professional Membership

Monthly

Annual

VIP Membership

Monthly

Annual

Add a Comment

How to query managers above employees? Works well for querying subordinate nodes. Kevin@c3amulet.com

Anonymous User 12/16/2004 10:01:14 AM


it works so nicely. for baronruns, just update employee=employee and it will trigger the update trigger

Anonymous User 10/22/2004 3:23:57 AM


I already have a table with Employees and their ID’s and their manager’s ID’s. There are about 10,000 employees. I really like this design and want to implement it on my table. I can easily add the lvl and hierarchy columns but getting them initially populated seems to be a daunting task. Has anyone created some kind of stored procedure that can do this?

Thanks,
Baron

BaronRunsFast8/19/2004 6:01:22 PM


Syntax error converting the varchar value '.' to a column of data type int.

I get this when running the syntax referenced in InstantDoc #8826. This happens specifically when I try to re insert teh data after applying the trigger in Listing 2.

Lorenzo Entzminger 3/9/2004 1:44:59 PM


Excellent article; just what I was looking for, I learned alot!

ROBERT2/18/2004 7:36:19 AM


This article is brilliant. I used this idea before, something I implemented a year ago with a user defined function to return the hierarchy. I thought it was not good because of the limitation of the varchar datatype.

The column lvl could be a computed one, based on the value of the hierarchy column, so you do not need to take care of it when you insert, update or delete an employee.

lvl as (len(hierarchy ) - len(replace(hierarchy , '.', ''))) - 2

You can find my example in microsoft.public.sqlserver.programming newsgroup, posted on Thursday, September 18, 2003 11:57 AM with the subject "Re: Hierarchy Tree".


Thanks for the article,

Alejandro Mesa

Alejandro Mesa 10/8/2003 10:17:53 AM


http://www.totada.com/category/

A simple cascading hierarchy 6/7/2001 9:47:10 AM


You must log on before posting a comment.

Are you a new visitor? Register Here