How to get ParentID

  • 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

  • You'll need a recursive CTE for that.

    This MSDN article explains the concept:

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

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply