Oracle 遍历树 start with () connect by prior ()

本文介绍了使用 Oracle SQL 实现自上而下及自下而上的数据遍历方法,并提供了具体的 SQL 语句示例。通过 START WITH 和 CONNECT BY PRIOR 子句实现层次遍历查询,适用于具有层次结构的数据表。

自上而下遍历


select o.* from t_cc_object o  start with o.objectid = 1000000000000001 connect by prior o.objectid  = o.parentid

由下面的数据可知:遍历是从一个树枝遍历到底,再遍历第二个。



自下而上遍历

select o.* from t_cc_object o  start with o.objectid = 1000000000005324 connect by prior o.parentid = o.objectid


数据结构:



表结构:

-- Create table
create table T_CC_OBJECT
(
  OBJECTID       NUMBER(16) not null,
  OBJECTTYPEID   NUMBER(16) not null,
  TOTALCODE      NVARCHAR2(64),
  ENABLED        NCHAR(1) default '1',
  OBJECTCODE     NVARCHAR2(64),
  OBJECTNAME     NVARCHAR2(128) not null,
  AUTOINCREASE   NUMBER,
  DESCRIPTION    NVARCHAR2(256),
  PARENTID       NUMBER(16),
  GENERATION     NUMBER,
  OBJECTPOSITION NUMBER not null,
  HASCHILDREN    NCHAR(1) default '0',
  CREATETIME     DATE
)
tablespace CC_ECS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    next 1
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints
alter table T_CC_OBJECT
  add constraint PK_T_CC_OBJECT primary key (OBJECTID)
  using index
  tablespace CC_ECS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate check constraints
alter table T_CC_OBJECT
  add constraint CKC_ENABLED_T_CC_OBJ
  check (Enabled is null or (Enabled in ('1','0')));
alter table T_CC_OBJECT
  add constraint CKC_HASCHILDREN_T_CC_OBJ
  check (hasChildren is null or (hasChildren in ('0','1')));
-- Create/Recreate indexes
create index IDX_OBJECT_OBJECTNAME on T_CC_OBJECT (OBJECTNAME)
  tablespace CC_ECS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
create index IDX_OBJECT_PARENTID on T_CC_OBJECT (PARENTID)
  tablespace CC_ECS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
create index I_CC_OBJECT_TOTALCODE on T_CC_OBJECT (TOTALCODE)
  tablespace CC_ECS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值