Solved

SQL - Store procedure

Posted on 2013-11-28
3
584 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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