?
Solved

SQL - Store procedure

Posted on 2013-11-28
3
Medium Priority
?
587 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 900 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

765 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