sql server - Help with store procedure -


i need following sp. couldnt solve really. have 2 columns in table: 1. parent 2. child

i need create sp using rownum create new table given param (level num) display connection between parent , son, or grandfather & grandson, etc...

for instance:

parent | child 2         4 4         6 

the sp return given level of 1 same table, if hand level 2 (grandfather<-> grandson) show:

parent | child 2        6 

how can that?

thanks all!

declare     @level int set         @level = 3  declare @temp table  (   parent    int,    child     int )  declare @result table (   parent    int,    child     int )  insert     @result select *   testing  while @level > 1 begin     set @level = @level - 1     delete  @temp      insert @temp     select t.parent, r.child     testing t     inner join @result r         on t.child = r.parent      delete @result      insert @result     select * @temp end  select * @result 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -