Access数据库一种树形结构的实现和子节点查询
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数据库一种树形结构的实现和子节点查询的更多相关文章
- MongoDB五种树形结构表示法
MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点
jQuery EasyUI 树形菜单 - 树形菜单加载父/子节点 通常表示一个树节点的方式就是在每一个节点存储一个 parentid. 这个也被称为邻接列表模型. 直接加载这些数据到树形菜单(Tree ...
- 数据库索引 引用树形结构 B-数 B+数
MySQL 为什么使用B+数 B-树和B+树最重要的一个区别就是B+树只有叶节点存放数据,其余节点用来索引,而B-树是每个索引节点都会有Data域. 这就决定了B+树更适合用来存储外部数据,也就是所谓 ...
- 递归算法结合数据库 解析 java树形结构
1.准备表结构及对应的表数据a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节 ...
- EasyUI_tree根据数据库数据生成树形结构JSON格式
@Entitypublic class PubComp { @Id private String aguid; // 菜单ID private String pguid; // 父菜单 private ...
- VUE实现Studio管理后台(七):树形结构,文件树,节点树共用一套代码NodeTree
本次介绍的内容,稍稍复杂了一点,用VUE实现树形结构.目前这个属性结构还没有编辑功能,仅仅是展示.明天再开一篇文章,介绍如何增加编辑功能,标题都想好了.先看今天的展示效果: 构建树必须用到递归,使用s ...
- EasyUI 树形菜单加载父/子节点
通常表示一个树节点的方式就是在每一个节点存储一个 parentid. 这个也被称为邻接列表模型. 直接加载这些数据到树形菜单(Tree)是不允许的. 但是我们可以在加载树形菜单之前,把它转换为标准标准 ...
- ELementUI 树形控件tree 获取子节点同时获取半选择状态的父节点ID
使用element-ui tree树形控件的时候,在选择一个子节点后,使用getCheckedKeys 后,发现只能返回子节点的ID,但是其父节点ID没有返回. 解决办法有三种: 1.element ...
- 两个比较好用的JS方法,用来处理树形结构!
一.平级结构转树形结构 /** * 平级结构转树形结构 * * 示例:const jsonDataTree = listTransToTreeData(jsonData, 'id', 'pid', ' ...
随机推荐
- oracle 直接客户端使用
到oracle网站下载直接客户端,http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.htm ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
- 视频-某hadoop高级应用-搜索提示
看了北风的免费视频,只有一个案例,苦逼买不起几百上千的视频教程 先搭建简单的web项目,基于struts,使用到了bootstrap. 界面: web.xml <filter> <f ...
- C - Minimum Inversion Number
Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...
- Autowired properities class
1. Properties类 @ConfigurationProperties(locations = "classpath:build.properties") @JsonInc ...
- iOS开发:获取设备IP地址
一.导入头文件 //首先导入头文件信息 #include <ifaddrs.h> #include <arpa/inet.h> #include <net/if.h> ...
- C中的回调函数
C语言中应用回调函数的地方非常多,如Nginx中: struct ngx_command_s { ngx_str_t name; ngx_uint_t type; char *(*set)(ngx_c ...
- 读取文件txt
/// <summary> /// 读取文件 /// </summary> /// <param name="path ...
- JS中获取table节点的tr或td的内容
<table id="tb1" width="200" border="1" cellpadding="4" ce ...
- Scala中的Map
映射 映射是对偶的集合. 声明映射 映射是对偶的集合. a.声明映射 b.映射中的键值对称作对偶,用( , )表示 c.当映射中不存在key时,取值会报错,解决方案是使用 contains方法,或者g ...