Solved

SQL - Store procedure

Posted on 2013-11-28
3
563 Views
Last Modified: 2013-12-02
Dear Expert

I would like to build a SP to product the result from Source to the Report as follows:

Source:

Parent     Child
F00000     W1099999
F00000     RPL12345
W1099999   RPL88888
RPL88888   KKK44444



Report:

Parent     Child      Level   Hierarchy
F00000     W1099999   1       F00000.W1099999
F00000     RPL12345   1       F00000.RPL12345
W1099999   RPL88888   2       F00000.W1099999.RPL88888  
RPL88888   KKK44444   3       F00000.W1099999.RPL88888.KKK44444



That's all

Thanks so much
0
Comment
Question by:beckyng
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
ItWorked earned 300 total points
ID: 39683280
It seems to have I can be done with Common Table Expression(CTE).

Create table #temp
(
      parent nvarchar(100),
      child nvarchar(100)
)

insert #temp values('F00000','W1099999')
insert #temp values('F00000','RPL12345')
insert #temp values('W1099999','RPL88888')
insert #temp values('RPL88888','KKK44444');

with myResult (parent,child,increment)
as
(
      Select *,1 as depth
      from #temp In1Temp
      where In1Temp.parent not in
      (
            Select In2Temp.child
            from #temp In2Temp
      )
      union all
      Select outTemp.*,r.increment+1
      from #temp outTemp
      inner join myResult r
      on outTemp.parent=r.child
      
)
Select *
from myResult;

drop table #temp;

Hope this what you where looking for :)
0
 

Author Comment

by:beckyng
ID: 39683456
HI expert

Thanks for your quick reply.

May I ask the remaing field of "Hierarchy"?

Thanks
0
 
LVL 4

Expert Comment

by:ItWorked
ID: 39691617
Oh! sorry forgot to add remaining part..:(
but here is your updated solution...

Create table #temp
(
      parent nvarchar(100),
      child nvarchar(100)
)

insert #temp values('F00000','W1099999')
insert #temp values('F00000','RPL12345')
insert #temp values('W1099999','RPL88888')
insert #temp values('RPL88888','KKK44444');

with myResult (parent,child,increment,Hierarchy)
as
(
      Select *,1 as depth,convert(nvarchar(max),(parent+'.'+child))
      from #temp In1Temp
      where In1Temp.parent not in
      (
            Select In2Temp.child
            from #temp In2Temp
      )
      union all
      Select outTemp.*,r.increment+1,r.Hierarchy+'.'+outTemp.child
      from #temp outTemp
      inner join myResult r
      on outTemp.parent=r.child
     
)
Select *
from myResult;

drop table #temp;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now