SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Recursive code to create a list
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

charya
Starting Member

1 Posts

Posted - 07/08/2014 :  11:42:24  Show Profile  Reply with Quote
Hey guys

I wasnt really sure if the thread title was great, but here's the problem.

What I need?: A way of dynamically inserting a list in the WHERE clause of a query. (in the form, WHERE ID = 1,2,3,6,9 etc)

Imagine an example DB with 3 columns Student ID, Name, Teacher_ID. (Lets assume Teacher_ID with value 100 means its the Headmaster)

I need to create a list with Student ID's, who are directly/indirectly under the Headmaster. Example:

Headmaster
Teacher 1 (ID 200)
Teacher 2 (ID 250)
Student 1 (ID 300)
Director
Teacher 4
Student 5

In the above example, since I only want those students/teachers under the headmaster, either directly/indirectly, my list would contain Teacher 1, Teacher 2, and Student 1. (In my case, just their ID's, so 200, 250, 300)

Director, Teacher 4 and Teacher 5 wouldnt be in the list since theyre not directly/indirectly Headmaster.

Hope I explained that well.
If you have any questions let me know :)

Thanks!

namman
Constraint Violating Yak Guru

USA
268 Posts

Posted - 07/08/2014 :  16:51:12  Show Profile  Reply with Quote
It is better to have table structure, sample data (with insert command) and list of expected output. Then explain why rows are selected or not selected.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000