Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get ParentID


How to get ParentID

Author
Message
ranga.402
ranga.402
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16518 Visits: 13210
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 SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
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