BOOL CManageDataBase::GetDepTreeAllSons( int rootItem )

{

CADORecordset Rst(&m_DataBase);

BOOL bResult = FALSE;

CString strSQL;

int curItem = rootItem;

int level = 0;

try

{

strSQL.Format(_T("delete from Temp_DepSons"));

if(!m_DataBase.Execute((LPCTSTR)strSQL))

throw IDS_PROC_ERROR;

strSQL.Format(_T("delete from DepSons"));

if(!m_DataBase.Execute((LPCTSTR)strSQL))

throw IDS_PROC_ERROR;

level = 1;

strSQL.Format(_T("insert into Temp_DepSons values(%d,%d)"), curItem, level);

if(!m_DataBase.Execute((LPCTSTR)strSQL))

throw IDS_PROC_ERROR;

while (level > 0)

{

strSQL.Format(_T("select 1 from Temp_DepSons where levelson = %d"), level);

if (!Rst.Open((LPCTSTR)strSQL))

throw 0;

if (Rst.IsEOF())

{

--level;

continue;

}

strSQL.Format(_T("select top 1 item from Temp_DepSons where levelson = %d"), level);

if (!Rst.Open((LPCTSTR)strSQL))

throw 0;

if (!Rst.IsEOF())

{

Rst.GetFieldValue(0, curItem);

}

strSQL.Format(_T("delete from Temp_DepSons where item = %d"), curItem);

if(!m_DataBase.Execute((LPCTSTR)strSQL))

throw IDS_PROC_ERROR;

strSQL.Format(_T("insert into DepSons(id,parentid) select curno, parentno from Deptree where curno = %d"), curItem);

if(!m_DataBase.Execute((LPCTSTR)strSQL))

throw IDS_PROC_ERROR;

strSQL.Format(_T("insert into Temp_DepSons(item,levelson) select curno, %d from Deptree where parentno = %d"), level+1, curItem);

if(!m_DataBase.Execute((LPCTSTR)strSQL))

throw IDS_PROC_ERROR;

if (m_DataBase.GetRecordsAffected() > 0)

{

++level;

}

}

bResult = TRUE;

}

catch (int nID)

{

m_err.SetLastErrorDescription(nID);

}

return bResult;

}

//树型结构

CREATE TABLE `Deptree` (

`CURNO` INTEGER,

`PARENTNO` INTEGER,

`LAYER` BYTE

)

GO

//结果表

CREATE TABLE `DepSons` (

`id` INTEGER,

`parentid` INTEGER

)

GO

CREATE INDEX id ON DepSons(id)

GO

CREATE INDEX parentid ON DepSons(parentid)

GO

//中间表

CREATE TABLE `Temp_DepSons` (

`item` INTEGER,

`levelson` INTEGER

)

GO

CREATE INDEX id ON Temp_DepSons(item)

GO

CREATE INDEX levels ON Temp_DepSons(levelson)

GO

添加数据后的结构如下:

数据库数据如下:

如果是SQL Server 可以写个函数,这样:

CREATE FUNCTION [dbo].[GetDepTreeAllSons](@RootItem as int)

RETURNS @result TABLE ( id int, parentid int)

AS

BEGIN

declare @level int

declare @curItem int

declare @stack table( item int, level int )

set @curItem = @RootItem --根结点编号

set @level = 1

insert into @stack values( @curItem, @level )

while @level > 0

begin

if not exists( select 1 from @stack where level=@level )

begin

set @level = @level - 1

continue ;

end

select top 1 @curItem = item from @stack where level = @level

delete from @stack where item = @curItem

insert into @result select CURNO,PARENTNO from Deptree where CURNO = @curItem

insert into @stack select CURNO,@level+1 from Deptree where PARENTNO=@curItem

if @@rowcount > 0

set @level = @level + 1

end

--输入出结果

return

END

Access数据库一种树形结构的实现和子节点查询的更多相关文章

  1. MongoDB五种树形结构表示法

    MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...

  2. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点

    jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点 通常表示一个树节点的方式就是在每一个节点存储一个 parentid. 这个也被称为邻接列表模型. 直接加载这些数据到树形菜单(Tree ...

  3. 数据库索引 引用树形结构 B-数 B+数

    MySQL 为什么使用B+数 B-树和B+树最重要的一个区别就是B+树只有叶节点存放数据,其余节点用来索引,而B-树是每个索引节点都会有Data域. 这就决定了B+树更适合用来存储外部数据,也就是所谓 ...

  4. 递归算法结合数据库 解析 java树形结构

    1.准备表结构及对应的表数据a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节 ...

  5. EasyUI_tree根据数据库数据生成树形结构JSON格式

    @Entitypublic class PubComp { @Id private String aguid; // 菜单ID private String pguid; // 父菜单 private ...

  6. VUE实现Studio管理后台(七):树形结构,文件树,节点树共用一套代码NodeTree

    本次介绍的内容,稍稍复杂了一点,用VUE实现树形结构.目前这个属性结构还没有编辑功能,仅仅是展示.明天再开一篇文章,介绍如何增加编辑功能,标题都想好了.先看今天的展示效果: 构建树必须用到递归,使用s ...

  7. EasyUI 树形菜单加载父/子节点

    通常表示一个树节点的方式就是在每一个节点存储一个 parentid. 这个也被称为邻接列表模型. 直接加载这些数据到树形菜单(Tree)是不允许的. 但是我们可以在加载树形菜单之前,把它转换为标准标准 ...

  8. ELementUI 树形控件tree 获取子节点同时获取半选择状态的父节点ID

    使用element-ui  tree树形控件的时候,在选择一个子节点后,使用getCheckedKeys 后,发现只能返回子节点的ID,但是其父节点ID没有返回. 解决办法有三种: 1.element ...

  9. 两个比较好用的JS方法,用来处理树形结构!

    一.平级结构转树形结构 /** * 平级结构转树形结构 * * 示例:const jsonDataTree = listTransToTreeData(jsonData, 'id', 'pid', ' ...

随机推荐

  1. tomcat配置虚拟主机

    在眼下,非常多server都是一台server对外能够訪问非常多个javaEE的项目,这样的实现方式就须要在tomcat里面配置虚拟主机了!以下就说说怎样配置虚拟主机: 找到tomcat的安装文件夹, ...

  2. [AngularJS] Provider

    This lesson describes what is really happening when you use the angularfactory and how you can make ...

  3. session销毁

    session.invalidate(),session.invalidate的销毁是把这个session所带的用户彻底的销毁,这个session跟用户已经紧密联合在一起,所以就一起销毁了,这样就算换 ...

  4. SHELL 详解

    http://blog.csdn.net/vah101/article/details/6173488 ( a=2;b=4;c=9; ) 子shell 环境 { a=2;b=4;c=9; } 当前sh ...

  5. Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 其他数据类型(二)

    1.方法 定义如下所示: def string_message(str='') if str.empty? "It's an empty string!" else "T ...

  6. sql 自定义函数-16进制转10进制

    做过笔记,好记性不如烂笔头: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HEXTOINT]') and ...

  7. redis中5种数据结构的使用

    一.redis 数据结构使用场景 原来看过 redisbook 这本书,对 redis 的基本功能都已经熟悉了,从上周开始看 redis 的源码.目前目标是吃透 redis 的数据结构.我们都知道,在 ...

  8. postgresql常用SQL

    --查看数据库 select * from pg_database; --查看表空间 select * from pg_tablespace; --查看语言 select * from pg_lang ...

  9. [改善Java代码]静态变量一定要先声明后赋值

    建议32: 静态变量一定要先声明后赋值 这标题看着让人很纳闷,什么叫做变量一定要先声明后赋值?Java中的变量不都是先声明后使用的吗?难道还能先使用后声明?能不能暂且不说,我们先来看一个例子,代码如下 ...

  10. Angular 2 从0到1 (三)

    作者:王芃 wpcfan@gmail.com 第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:A ...