ADO自定义函数
//要求
Create stored procedure / function in MS SQL Server to retrieve an item’s children in the hierarchy. Here is the hierarchy table structure –
Table Name: |
tb_Tree |
|
Primary Key: |
ItemID |
|
|
||
Column |
Data Type |
Description |
ItemID |
Int |
Item identity |
ParentItemID |
Int |
Parent Item identity |
ItemName |
Nvarchar(30) |
Item Name |
Some sample data –
ItemID |
ParentItemID |
ItemName |
1 |
0 |
Root |
2 |
1 |
First Node |
3 |
2 |
Second Node |
4 |
0 |
根 |
5 |
4 |
第一接点 |
6 |
4 |
第二接点 |
7 |
6 |
第三接点 |
The stored procedure should take two parameters –
1. ItemID
2. Level Count.
And return a subset of tb_Tree meet the condition.
Testing scenarios –
1. ItemID = 4 and Level Count = 1, result should be –
ItemID |
ParentItemID |
ItemName |
4 |
0 |
根 |
2. ItemID = 4 and Level Count = 2, result should be –
ItemID |
ParentItemID |
ItemName |
4 |
0 |
根 |
5 |
4 |
第一接点 |
6 |
4 |
第二接点 |
3. ItemID = 4 and Level Count = 3, result should be –
ItemID |
ParentItemID |
ItemName |
4 |
0 |
根 |
5 |
4 |
第一接点 |
6 |
4 |
第二接点 |
7 |
6 |
第三接点 |
自己写的Funtion:

参考地址:
http://www.wangyeba.com/Article/web01/net/200703/20070327090703.shtml