with cte as ( select unt_id,unt_parent from tb where unt_id=@unt_id union all select a.unt_id,a.unt_parent from tb a join cte b on a.unt_parent=b.unt_id )select unt_id+',' from cte for xml path('')
用sql递归 给你个参考 WITH prgs(parent_key, parent_id, child_key, child_id) AS ( SELECT parent_key, parent_id, child_key, child_id FROM tablename WHERE 查询条件 UNION ALL SELECT e.parent_key, e.parent_id, e.child_key, e.child_id FROM tablename e inner join prgs a ON (a.child_key=e.parent_key AND a.child_id=e.parent_id ) WHERE e.group_id=10 ) select * from prgs
string unitids = ""; public void untids(string uid) {
using (DataBxDataContext bx = new DataBxDataContext()) { List<Units> ulist = bx.Units.Where(p => p.Unt_Parent == Convert.ToInt32(uid)).ToList(); if (ulist.Count > 0) { foreach (Units u in ulist) {
string uu = "";
public void untids(string uid)
{
string uitis = "";
using (DataBxDataContext bx = new DataBxDataContext())
{
List<Units> ulist = bx.Units.Where(p => p.Unt_Parent == Convert.ToInt32(uid)).ToList(); if (ulist.Count > 0)
{
foreach (Units u in ulist)
{
untids( u.Unt_ID.ToString());
}
}
else
{
uu +=","+ uid;
}
这个有问题啊
得到的结果是 6,4,5 数据应该是
23456啊
2 一级单位 1
3 二级单位1 2
4 二级单位2 2
5 二级单位3 2
6 三级单位1 3
(
select unt_id,unt_parent from tb where unt_id=@unt_id
union all
select a.unt_id,a.unt_parent from tb a join cte b on a.unt_parent=b.unt_id
)select unt_id+',' from cte for xml path('')
给你个参考
WITH prgs(parent_key, parent_id, child_key, child_id) AS
(
SELECT parent_key, parent_id, child_key, child_id
FROM tablename
WHERE 查询条件
UNION ALL
SELECT e.parent_key, e.parent_id, e.child_key, e.child_id
FROM tablename e
inner join prgs a ON
(a.child_key=e.parent_key AND a.child_id=e.parent_id )
WHERE e.group_id=10
)
select * from prgs
string unitids = "";
public void untids(string uid)
{
using (DataBxDataContext bx = new DataBxDataContext())
{
List<Units> ulist = bx.Units.Where(p => p.Unt_Parent == Convert.ToInt32(uid)).ToList(); if (ulist.Count > 0)
{
foreach (Units u in ulist)
{
untids( u.Unt_ID.ToString());
}
unitids += "," + uid;
}
else
{
unitids += "," + uid;
}
}
}自己解决了