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', ' ...
随机推荐
- Codeforces gym 100685 E. Epic Fail of a Genie 贪心
E. Epic Fail of a GenieTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685 ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad
We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...
- iOS开发——网络编程Swift篇&(一)网络监测
网络监测 enum ReachabilityType { case WWAN, WiFi, NotConnected } public class Reachability { /** :see: O ...
- oschina git 私有项目使用。。
工作流程 安装设置 git 下载最新版本的git http://git-scm.com/downloads 当你安装完成git的时候,你需要简单的配置一下,打开终端: 用户名 第一步,你需要告诉git ...
- UNIX基础知识之出错处理
当UNIX函数出错时,常常会返回一个负值,而且整型变量errno通常被设置为含有附加信息的一个值.例如,open函数如成功执行则返回一个非负文件描述符,如出错则返回-1.在open出错时,有大约15种 ...
- cocos2dx libiconv 转码
iconv下载(Android)已编译完的iconv包(用这个即可) ios自带libiconv,只需#include <iconv.h>即可 步骤 1.libiconv解压文件放置 直 ...
- Apache 学习笔记(心得)
http://blog.csdn.net/btbtd/article/details/288027#2 # 分类:# 01.常规设置# 02.虚拟主机# 03.<Directory> + ...
- jQuery之父:每天都写点代码
去年秋天,我的“兼职编程项目”遇到了一些问题:要不是从 Khan Academy 的项目里挪出时间来的话,我根本没办法将不理想的进度弥补上. 这些项目遇到了一些严重的问题.之前的工作我主要是在周末,有 ...
- 浅谈JS执行环境及作用域
今天刚刚开通博客,也是第一次写博文,略感紧张.作为一个表达能力弱弱的人来说,自己花三分钟理解一个知识点,当别人问起时,也许需要30分钟才只是让别人知道自己在说什么,一点也不夸张,希望在博客上可以练习 ...