另一帖地址http://topic.csdn.net/u/20110120/16/47e642fa-33a3-43d8-a63a-9b0b810d3c41.html?seed=664029198&r=71334542原帖内容表a
本身id id   
上级id parentid
类别 type
操作id oprid现在操作id这一列都是空,要更新一下。
如果某条记录中type=3,则将oprid更新为id(没有上级的,type一定为3)
如果记录中type不是3,则要通过树递归查询到上级的id(离子节点最近的一个父节点且type=3的),将这里查询到的id更新到oprid最后更新后得到下面这样的结果id    parentid   type   oprid
1      -          3       1
2      1          3       2
3      2          2       2
4      3          2       2
5      2          3       5
6      5          2       5
那边已有答案
update tba a
set oprid=(case type when 3 then id
else (select parentid from tba
where connect_by_isleaf=1 and connect_by_root(id)=a.id
start with type<>3
connect by id=prior parentid and type<>3)
end)只是我这边编译不过去,
ORA-00600:内部错误代码,参数:[qctcte1],[0],[],[],[],[],[],[]
不知道啥原因
另开一贴的目的是,因为connect_by_isleaf、connect_by_root这两个函数实在是还没搞懂。
而上面的sql中是 要用parentid来更新。
如果表a多两个字段
表a
本身id     id   
上级id     parentid
类别       type
操作id     oprid
本身code   code
操作code   oprcode表中code字段是有数据的,要更新oprcode,需求同更新id
这个code字段没做像id那样有,上下级关系关联的话,这个要怎么更新呢?
结果如下id    parentid   type   oprid   code   oprcode
1      -          3       1      a        a
2      1          3       2      b        b
3      2          2       2      c        b
4      3          2       2      d        b
5      2          3       5      e        e
6      5          2       5      f        e

解决方案 »

  1.   

    connect_by_isleaf 如果为叶子结点,则为1否则为0
    connect_by_root  取得根结点。以上只有10g以上才支持,9i是不支持的。
      

  2.   

    with tab as(
    select 1 id,null parentid,3 type,null oprid,'a' code,null oprcode from dual
    union all
    select 2,1,3,null,'b',null from dual 
    union all
    select 3,2,2,null,'c',null from dual 
    union all
    select 4,3,2,null,'d',null from dual 
    union all
    select 5,2,3,null,'e',null from dual 
    union all
    select 6,5,2,null,'f',null from dual 
    )
    select id,parentid,type,oprid,code,
           (select code from tab where id=b.oprid) oprcode 
    from(
        select id,parentid,type,
              case when type=3 then id
                        else (select parentid from tab
                              where connect_by_isleaf=1 
                              start with id=a.id
                              connect by id=prior parentid and type<>3)
              end oprid,
              code       
        from tab a
    ) bID    PARENTID   TYPE   OPRID   CODE   OPRCODE
    ------------------------------------------------------
    1                 3       1      a        a
    2      1          3       2      b        b
    3      2          2       2      c        b
    4      3          2       2      d        b
    5      2          3       5      e        e
    6      5          2       5      f        e