根据分类ID获得所有子ID和所有父ID
2015-12-28 15:00
148 查看
数据表
ID Name Parent_ID 1 计算机 NULL 2 联想计算机 1 3 戴尔计算机 1 6 手机 NULL 7 诺基亚手机 6 8 苹果手机 6 9 HTC手机 6 10 工程采购 NULL 11 建筑工程 10 12 房产采购 10 13 联想悦动 2
根据传入的分类ID,返回它下面的所有子分类。
-- 根据ID获得它下面的所有子分类
create function UF_GetChildMaterialCategories( @Id int ) returns @tb table (ID int) as begin insert into @tb select ID from MaterialCategories where Parent_ID = @Id while @@Rowcount >0 --只要有下级节点就循环 begin insert into @tb select a.ID from MaterialCategories as a inner join @tb as b on a.Parent_ID = b.id and a.id not in(select id from @tb) end return end go
--执行 select * from UF_GetChildMaterialCategories(1)
根据传入的分类ID,返回它的上级分类。
--根据传入的分类ID,返回它的上级分类。
create function UF_GetParentMaterialCategories( @Id int ) returns @tb table (id int) as begin insert into @tb select parent_id from MaterialCategories where id = @Id while @@Rowcount >0 begin insert into @tb select a.parent_id from MaterialCategories as a inner join @tb as b on a.id = b.id and a.parent_id not in(select id from @tb) end return end go
--执行
select * from UF_GetParentMaterialCategories(13)
相关文章推荐
- **Eclipse Access Restriction**
- 如何用GAT抓取log
- 长沙Uber优步司机奖励政策(12月28日到1月3日)
- Jersey初始化配置
- GridView的简单使用
- bootstrap ace admin 整合java HTML5 全新高大尚web后台框架
- C++ 大规模程序设计 之 包
- file转换成bytes
- 广州Uber优步司机奖励政策(12月28日到1月3日)
- day①:python的变量及数据类型
- Android开发获取GPS位置,包含基站\wifi\gps 几种方法
- 对话框DiaLog
- iOS--分段控件UISegmentedControl
- 傅里叶变换
- 武汉Uber优步司机奖励政策(12月28日到1月3日)
- 厦门Uber优步司机奖励政策(12月28日到1月3日)
- 树链剖分(二)
- LinuxShell算术运算
- HTTP之Content-Length
- 二维码弹出