博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
经典树型表结构之SORT_NO
阅读量:6983 次
发布时间:2019-06-27

本文共 2322 字,大约阅读时间需要 7 分钟。

  hot3.png

在以下情况需要对经典树型表的sort_no进行重排序:

1、插入节点(插入子树),需调整节点后所有sort_no;
2、删除节点(删除子树),需调整节点后所有sort_no;
3、调整同级节点sub_sort,特别是调整level高的节点,几乎可影响到整棵树;
--update T_TREE set sort_no = null;
call UTIL_E2SAY.sortTree('T_TREE',1);

 

排序工具包代码:

create or replace package UTIL_E2SAY is  --对指定库表从某个id开始设置sort_no排序号  procedure sortTree(p_TabName in varchar2,                     p_StartId in number,                     p_SqlCondition in varchar2 default null);   end;/create or replace package body UTIL_E2SAY is   procedure sortTree(p_TabName in varchar2,                     p_StartId in number,                     p_SqlCondition in varchar2 default null) is    type t_Tab is table of number index by binary_integer;    type t_Cur is ref cursor;         m_SqlCondition varchar2(2000) := '';    m_SqlConditionWhere varchar2(2000) := '';    m_SortNo number(6) := 0;    m_ParentIdTab t_Tab;         v_NumCur t_Cur;    v_Id number;         procedure sortChild(p_StartId in number) is      v_IdCur t_Cur;    begin      execute immediate        'update '||p_TabName||' set sort_no = :sort_no where id = :id'||m_SqlCondition      using m_SortNo, p_StartId;       m_SortNo := m_SortNo + 1;       if m_ParentIdTab.exists(p_StartId) then        open v_IdCur for           'select id' ||           '  from ' || p_TabName ||           ' where pid = :pid' || m_SqlCondition ||           ' order by sub_sort'           using p_StartId;        loop           fetch v_IdCur into v_Id;           exit when v_IdCur%notfound;           sortChild(v_Id);        end loop;        close v_IdCur;      end if;           end sortChild;       begin       if p_SqlCondition is not null then      m_SqlCondition := ' and ' || p_SqlCondition;      m_SqlConditionWhere := ' where ' || p_SqlCondition;    end if;                open v_NumCur for      'select sort_no from '||p_TabName||' where id = :id'||m_SqlCondition      using p_StartId;    fetch v_NumCur into m_SortNo;    close v_NumCur;    if m_SortNo = 0 or m_SortNo is null then      m_SortNo := 1;    end if;     open v_NumCur for      'select distinct pid from '||p_TabName||m_SqlConditionWhere;    loop      fetch v_NumCur into v_Id;      exit when v_NumCur%notfound;      m_ParentIdTab(v_Id) := 1;    end loop;    close v_NumCur;         sortChild(p_StartId);       end; end;/

转载于:https://my.oschina.net/h2do/blog/268134

你可能感兴趣的文章
lintcode:背包问题II
查看>>
处理手势冲突和错乱的一点经验
查看>>
Struts2防止表单重复提交
查看>>
[转]Python格式化输出
查看>>
在Activity中响应ListView内部按钮的点击事件
查看>>
CSS - 修改input - placeholder 和 readonly 的样式
查看>>
常用UI布局
查看>>
在多线程情况下 局部变量与全局变量 哪个比较安全呢
查看>>
算法评测
查看>>
40款非常酷的国外创意名片设计欣赏
查看>>
RadioGroup单选按钮用法
查看>>
POJ 2773 Happy 2006
查看>>
UBIFS介绍 - MTD网站
查看>>
如何使用ITEXTSHARP将HTML代码字符串写进PDF
查看>>
Oracle SQL CPU占用高
查看>>
mongodb简介与增删该查
查看>>
Maya 2015 中英文切换
查看>>
C语言的字符串分割
查看>>
Arduino可穿戴开发入门教程Windows平台下安装Arduino IDE
查看>>
BpBinder 转换为 BpCameraService 流程
查看>>