Solved

SQL - Store procedure

Posted on 2013-11-28
3
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IF SQL Query 12 29
Change this SQL to get all nodes 3 38
Linked Server - SP with Param to VIew 7 22
SQL Server code help needed 14 32
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

749 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