Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get ParentID Expand / Collapse
Author
Message
Posted Monday, March 7, 2011 12:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 8, 2011 1:35 AM
Points: 2, Visits: 11
I am using sql server 2008 enterprise edition

I have Account Table like this.

AccountID Name ParentID
1 Account 0
2 Asset 0
3 Expensess 1
4 Building 2
5 Income 1
6 Small Expenses 3
7 small Income 5
8 Furniture 4
and so on

If i got 0 then that is main account.until we have to search it has to get.

All accounts related to parent accounts Account and Asset only

if i am retrieving account id=7

then out put is like this

AccountID Name ParentAccountName

7 small Income Account

if i am retrieving account id=8

then out put is like this

AccountID Name ParentAccountName

8 Furniture Asset


Like this i have to get output.

How to write Query for this

Thank's in Advance
Post #1073973
Posted Monday, March 7, 2011 3:41 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:15 AM
Points: 13,017, Visits: 10,800
You'll need a recursive CTE for that.
This MSDN article explains the concept:

http://msdn.microsoft.com/en-us/library/ms186243.aspx




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1074027
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse