SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Recursive CTE Example


Recursive CTE Example

Author
Message
mr.neil.bryan
mr.neil.bryan
SSC Journeyman
SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)

Group: General Forum Members
Points: 87 Visits: 324
Comments posted to this topic are about the item Recursive CTE Example
Mike Stuart
Mike Stuart
SSC Veteran
SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)SSC Veteran (268 reputation)

Group: General Forum Members
Points: 268 Visits: 210
Neil,

Awesome code - thanks! Still trying to get my head around it - lol.

A suggestion: I was looking for a way to get the manager's name as well as their ID in the results and came up with the following:

1) Set the big boss' Reports_To_ID = to his Employee_ID, instead of null; i.e. change:

INSERT INTO @Employees
SELECT 1,'Roy Hodgson',NULL,'Managing Director'

to

INSERT INTO @Employees
SELECT 1,'Roy Hodgson',1,'Managing Director'


2) Change the final select statement to:

SELECT a.Employee_ID ,
a.Employee_Name ,
a.Reports_To_ID ,
b.Employee_Name Manager_Name,
a.Job_Title
FROM _hirearchy a JOIN _hirearchy b ON b.Employee_ID = a.Reports_To_ID

I've attached my code for your / everyone's benefit to see the changes.


The only problem I see with this is if you change the @BossID to a different value (7248 for Kyle Walker, for example), he doesn't show up in the results as an Employee. Can you think of a way to change your query to return the Manager_Name? That would be awesome!

Kudos again,



Mike



Attachments
modified.sql.txt (21 views, 4.00 KB)
Bodhisatya
Bodhisatya
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 338
Crisp. Well written
CGZ
CGZ
Old Hand
Old Hand (385 reputation)Old Hand (385 reputation)Old Hand (385 reputation)Old Hand (385 reputation)Old Hand (385 reputation)Old Hand (385 reputation)Old Hand (385 reputation)Old Hand (385 reputation)

Group: General Forum Members
Points: 385 Visits: 330
Hello everyone!
Great post!

The only way I've found to get the manager name is changing the table "_hirearchy b" for @Employees in the final select with a left join. It also works with the original insert for the ID 1 with Reports_To_ID = NULL.
I guess this decrease performance or, at least, you lose some of the advantages of using CTE.


FROM _hirearchy a LEFT OUTER JOIN @Employees b ON b.Employee_ID = a.Reports_To_ID




Please, tell me if this is too awful :-P.
Greetings to all!
mr.neil.bryan
mr.neil.bryan
SSC Journeyman
SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)SSC Journeyman (87 reputation)

Group: General Forum Members
Points: 87 Visits: 324
Awesome thanks Mike!!
Iwas Bornready
Iwas Bornready
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29452 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search