当前位置: 代码迷 >> Sql Server >> 一遇到递归就头大, 这个如何效率解决
  详细解决方案

一遇到递归就头大, 这个如何效率解决

热度:80   发布时间:2016-04-27 12:10:53.0
一遇到递归就头大, 这个怎么效率解决?
表A
SQL code
id    name    pid3    c    14    d    15    e    26    f    47    g    38    h    69    i    710    j    711    k    512    l    913    m    1014    n    815    o    14

表B只有id列,如何能效率得到root列, 如下
SQL code
id    root4    17    19    111    212    115    1


------解决方案--------------------
SQL code
;with t(id,pid,topid)as(select id,pid,pid     from A     where not exists (select 1 from A A1 where A.pid=A1.id)union allselect A.id,A.pid,t.topid    from A,t    where A.pid=t.id)select B.id,t.topid [root] from B,t where B.id=t.id;
  相关解决方案