| Author |
Topic |
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-10 : 17:30:33
|
| I hope I can explain this clearlyI have one table with two fieldstblCompanyParent_ID | Company_ID123 | 345123 | 567345 | 895345 | 967895 | 654I need to do this somehowSELECT Parent_ID, Company_ID FROM tblCompanyWHERE Parent_ID = 123The query will returnParent_ID | Company_ID123 | 345123 | 567Then I need to take the Company_ID and see if they are also Parent_ID'sSELECT Parent_ID, Company_IDFROM tblCompany WHERE Parent_ID IN (345, 567)The Query will ReturnParent_ID | Company_ID345 | 895345 | 967I need to automate this somehow. I have to get the Company_IDs from the result set each time and see if they are Parent_ID'sthis can go on and onI can not create stored proceduresI can create temp tables and cursors onlyI hope I explained this clearly.I appreciate any help. I'm really in a jamThank you |
|
|
guptam
Posting Yak Master
161 Posts |
Posted - 2009-03-10 : 20:21:11
|
| So you are saying that chain of parent->child is endless?i.e. Once you figure out the parent ID it will have childs and those childs can have childs. With no stop?-- Mohit K. GuptaB.Sc. CS, Minor JapaneseMCITP: Database AdministratorMCTS: SQL Server 2005http://sqllearnings.blogspot.com/ |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2009-03-10 : 21:35:20
|
| Recursive CTE's would work well for this type of problem. The example in the documentation is almost exactly identical to what you are trying to do: http://msdn.microsoft.com/en-us/library/ms186243.aspx# |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-11 : 10:17:46
|
| Yes, once I figure out the parent ID it will have childs and then those childs will also be in the parent_ID column having other children and so on. It however is not endless but it can go on for quite sometimeThank you |
 |
|
|
guptam
Posting Yak Master
161 Posts |
Posted - 2009-03-11 : 10:22:36
|
| Recursive CTE is your friend as pointed out by Sunita then but just be careful as it can have performance impact also :)-- Mohit K. GuptaB.Sc. CS, Minor JapaneseMCITP: Database AdministratorMCTS: SQL Server 2005http://sqllearnings.blogspot.com/ |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-11 : 10:30:09
|
| Ok. Thank you both for your help!!! I'm I may be back with questons about CTE as I've never used this before. Thanks again!!!!! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-11 : 10:52:58
|
| ok thank you. I don't think that will happen based on the way the table was populated. Thank you for the heads up!!! |
 |
|
|
guptam
Posting Yak Master
161 Posts |
Posted - 2009-03-11 : 11:07:06
|
| Urg .. yaa if that does exist it will lock the server access for you. So make sure you have DAC enabled (Dedicated Admin Connection) to kill your connection just in case such circular ref exists.Good point Mr. Peso :).Mohit.-- Mohit K. GuptaB.Sc. CS, Minor JapaneseMCITP: Database AdministratorMCTS: SQL Server 2005http://sqllearnings.blogspot.com/ |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-11 : 11:19:19
|
| Does this appear to be correctWITH Parent_CTE AS (SELECT Entity_ID, Parent_Entity_IDFROM tblRankWHERE Parent_Entity_ID = 1736746UNION ALLSELECT p.Entity_ID, p.Parent_Entity_IDFROM tblRank pINNER JOIN Parent_CTE pcte ON pcte.Entity_ID = p.Parent_entity_ID)SELECT * FROM Parent_CTEI'm receiving an erro'Incorrect Syntax near the keyword 'WITH' |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-11 : 11:29:36
|
| Very Very sorry to have troubled you.....Apprently I'm connecting to a SQL Server 2000 instance. The CTE (from what I understand) will not work. Is there another way to get the results I'm looking for?Again...I'm sorry about the confusion |
 |
|
|
guptam
Posting Yak Master
161 Posts |
Posted - 2009-03-11 : 12:02:40
|
| I think your only choice then is cursor and this is how I can think of ...SELECT Parent_ID, Child_ID, (SELECT COUNT(*) FROM tblCompany WHERE PARENT_ID = Child_ID) As ChildCountINTO #ChildListingFROM tblCompanyThen build cursor from there using ChildCount to see if you need to drill further down .. Thanks.-- Mohit K. GuptaB.Sc. CS, Minor JapaneseMCITP: Database AdministratorMCTS: SQL Server 2005http://sqllearnings.blogspot.com/ |
 |
|
|
callawayx14
Yak Posting Veteran
73 Posts |
Posted - 2009-03-11 : 12:47:31
|
| Thank again for all the help!! |
 |
|
|
|