无限循环父子树(主要是数据库里面的一张表,然后根据ID查找父类的名称,以及查找子类的名称) 自己思考,存点东西。
2010-10-08 18:12
579 查看
代码
--无限循环父子树 create table Father(Fid int identity(1,1),Fname nvarchar(20),FatherId int) insert into Father select '数据库开发',0 union all select 'MS-SQL SERVER',1 union all select '基础类',1 union all select '应用实例',1 union all select 'BI',1 union all select 'BISS',5 union all select 'BIAA',6 union all select 'Oracle',0 union all select 'VFP',8 union all select 'Access',8 union all select 'Sybase',8 union all select 'MySql',8 union all select 'MySqlss',12 select * from Father /* Fid Fname FatherId ----------- -------------------- ----------- 1 数据库开发 0 2 MS-SQL SERVER 1 3 基础类 1 4 应用实例 1 5 BI 1 6 BISS 5 7 BIAA 6 8 Oracle 0 9 VFP 8 10 Access 8 11 Sybase 8 12 MySql 8 13 MySqlss 12 */ select * from Father where FatherId=0 /* Fid Fname FatherId ----------- -------------------- ----------- 1 数据库开发 0 8 Oracle 0 */ -- 一、知道子类ID找父类名称 --1、函数 alter function f_getParent(@fid int) returns varchar(40) as begin declare @ret varchar(40) while exists(select 1 from Father where Fid=@fid and FatherId<>0) begin select @fid=b.Fid,@ret=','+rtrim(b.Fname)+isnull(@ret,'') from Father a,Father b where a.Fid=@fid and b.Fid=a.FatherId end set @ret=stuff(@ret,1,1,'') return @ret end go select Fid,isnull(dbo.f_getParent(Fid),'根目录') as Fname from Father /* Fid Fname ----------- ------------------ 1 根目录 2 数据库开发 3 数据库开发 4 数据库开发 5 数据库开发 6 数据库开发,BI 7 数据库开发,BI,BISS 8 根目录 9 Oracle 10 Oracle 11 Oracle 12 Oracle 13 Oracle,MySql */ --2、存储过程 alter procedure f_getParents @fid int, @parent nvarchar(100) out as begin while exists(select 1 from Father where Fid=@fid and FatherId<>0) begin select @fid=b.Fid,@parent=','+rtrim(b.Fname)+isnull(@parent,'') from Father a,Father b where a.Fid=@fid and b.Fid=a.FatherId end set @parent=stuff(@parent,1,1,'') select @parent end go exec f_getParents 13,''--查MySqlss父目录的名称 /* -------------------------- Oracle,MySql */ -- 二、知道父类ID找所属子类名称 alter procedure f_getSons @FatherId int, @sons nvarchar(100) out as begin while exists(select 1 from Father where FatherId=@FatherId) begin select @FatherId=b.Fid,@sons=','+rtrim(b.Fname)+isnull(@sons,'') from Father a,Father b where a.Fid=@FatherId and b.FatherId=a.Fid end set @sons=stuff(@sons,1,1,'') set @sons=isnull(@sons,'无子类') select @sons end go exec f_getSons 1,'' /* ----------------------------------------- BIAA,BISS,BI,应用实例,基础类,MS-SQL SERVER */
相关文章推荐
- SQL2008+ 根据子类ID来查找最大级父类ID
- mybatis 根据子类获取父类的方法 base_type_detail是数据库里面的表
- 自己写的POIUtil,主要解决从不同的HSSFWorkbook复制sheet以及根据单元格插入图片等
- mysql 查找数据库里面字段的id是否存在 可以限定表和字段
- Android中资源id的获取,根据id获取资源以及根据资源名称获取id。
- JDBC远程从一个MySql数据库中的一张表里面读出数据(这个数据库需要用SSH隧道连接,大约8W条数据),然后分别插入到另一个数据库中的两张表里
- 自己写的一个根据结点名称查找CTreeCtrl树中对应结点的方法(用的是递归思想)
- mysql 创建函数(根据子类,依据父类id,循环查询出所有父类名)
- asp.net中实现,用户根据自己的需要,勾选checkbox,然后把查询到数据库中的内容导出到excel
- 开通了一个微信公众账号,主要想分享一些自己对于行业、技术和产品的思考以及收录精彩内容给读者
- 根据已知字符串遍历整个数据库查找它所在的表和列位置以及出现的次数
- PHP 根据父类查找对应的子类
- MVC操作LocalDB数据库,通过电影类型和名称来查询电影,在控制器里写的方法以及页面代码,自己参考。
- mysql 根据子类id查询所有父类id
- Java---查找数据库的smt_lampcontroller表中,行号为奇数的所有数据的id号,报错以及解决办法
- php从PostgreSQL 数据库检索数据,实现分页显示以及根据条件查找数据
- 自己写的一个根据结点名称查找CTreeCtrl树中对应结点的方法(用的是递归思想)
- tensorflow创建变量以及根据名称查找变量
- tensorflow创建变量以及根据名称查找变量
- Hibernate继承(2)子类属性生成在自己的一张表中,父类的属性一张表