读书人

求写存储过程,该如何处理

发布时间: 2012-03-29 12:53:13 作者: rapoo

求写存储过程
DepartId DepartName ParentDepartId SortId
1 技术部 0 1
2 宣传部0 2
3 销售部0 3
4 网络部0 4
5 技术二部 1 5
6 网络二部 4 6
7 技术部1 5 7

先解释一下 通过上面的表 创建一个Treeview DepartId :部门编号 DepartName: 部门名称 ParentDepartId :父级门编号(0代表根目录); SortId:排序编号
我根据SortId 进行树的排序,
技术部
技术二部
技术部1
宣传部
销售部
网络部
网络二部 解释树排列 , 子节点 ParentDepartId =父节点的 DepartId

问题 : 求写一存储过程 可以查到 技术部 技术二部 技术部1 如果有技术3部 同理一样可以查 小弟SQL初来乍到 不盛感激




[解决办法]

SQL code
f not object_id('tempdb..#test') is nullbegindrop table #testendcreate table #test(DepartId int, DepartNamen nvarchar(36), ParentDepartId int, SortId int)insert into #testselect 1,'技术部',0,1 union allselect 2,'宣传部',0,2 union allselect 3,'销售部',0,3 union allselect 4,'网络部',0,4 union allselect 5,'技术二部',1,5 union allselect 6,'网络二部',4,6 union allselect 7,'网络三部',4,7 union allselect 8,'技术三部',1,8 union allselect 9,'网络部1',6,9 union allselect 10,'技术部1',5,10 union allselect 11,'技术部2',8,11 select a.DepartId as f_id,a.DepartNamen as f_name,isnull(b.DepartNamen,'no children') as children_name from #test a left join#test b on a.DepartId=b.ParentDepartId/*1    技术部    技术二部1    技术部    技术三部2    宣传部    no children3    销售部    no children4    网络部    网络二部4    网络部    网络三部5    技术二部    技术部16    网络二部    网络部17    网络三部    no children8    技术三部    技术部29    网络部1    no children10    技术部1    no children11    技术部2    no children*/
[解决办法]

读书人网 >SQL Server

热点推荐