• 通过数据库编程进行递归(Oracle)


    一、用于测试数据及其结构和生成该测试数据的sql语句如下:
    1、测试数据
    nodeId     parentId
    ---------- ----------
    A01        A
    A02        A
    A03        A
    A0101      A01
    A0102      A01
    A0201      A02
    A0202      A02
    B01        B
    B02        B
    B0201      B02
    B0202      B02
    B020101    B0201
    B020102    B0201

    2、建表及生成数据的sql语句
    create table tree
    (
        nodeId varchar2(10) not null,
        parentId varchar2(10) not null
    );

    insert into tree
    select 'A01', 'A' from dual union all
    select 'A02', 'A' from dual union all
    select 'A03', 'A' from dual union all
    select 'A0101', 'A01' from dual union all
    select 'A0102', 'A01' from dual union all
    select 'A0201', 'A02' from dual union all
    select 'A0202', 'A02' from dual union all
    select 'B01', 'B' from dual union all
    select 'B02', 'B' from dual union all
    select 'B0201', 'B02' from dual union all
    select 'B0202', 'B02' from dual union all
    select 'B020101', 'B0201' from dual union all
    select 'B020102', 'B0201' from dual;

    二、递归
    1、递归sql
    select parentId, nodeId
    from tree
    start with parentId = 'B02'
    connect by parentId = prior nodeId
    order by parentId, nodeId;

    2、执行结果
    parentId   nodeId
    B02 B0201
    B02 B0202
    B0201 B020101
    B0201 B020102

    指定parentId时要注意,oracle是区分大小写的。

  • 相关阅读:
    Eclipse修改背景颜色(豆沙绿)
    项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved
    jdk1.7 环境变量配置
    Maven的安装、配置及使用入门
    tomcat端口作用
    《Maven实战》
    Maven 详解
    遍历Map的四种方法
    遍历properties文件
    题库终结
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260745.html
Copyright © 2020-2023  润新知