sql查询子节点
A. sql语句查询出一个父节点下的所有子节点
假如你的表名字是tt,创建下面的存储过程,使用存储过程即可查询。
create proc querytree
@user varchar(100)
as
begin
declare @id int
declare @T_tmp table(id int,username varchar(100),parentid int)
insert into @T_tmp select * from tt where username=@user
while(@@rowcount>0)
begin
insert into @T_tmp select * from tt where parentid in (select id from @T_tmp) and id not in(select id from @T_tmp)
end
select * from @T_tmp where username<>@user
end
B. 200分求助!SQL递归查询所有子节点
CREATE PROCEDURE [dbo].[SetColGroup](@sColItemCode VARCHAR(50),@ReportID VARCHAR(50) )
AS
-- 获取ItemCode对应的GroupCode,并更新至临时表中
BEGIN
DECLARE @sSQL VARCHAR(1000)
DECLARE @colGroupCode VARCHAR(50)
DECLARE @SuperColItemCode VARCHAR(50)
DECLARE @colSuperGroupCode VARCHAR(50)
--获取下一级别的数据(这里的SuperColItemCode实际上是下一级数据)
DECLARE curGroupCodeTmp CURSOR LOCAL FOR
SELECT c.ColGroupCode,b.SuperColItemCode,d.ColGroupCode AS ColSuperGroupCode
FROM dbo.ReportItemCross b
INNER JOIN dbo.ReportColItem c ON b.ColItemCode = c.ColItemCode
INNER JOIN dbo.ReportColItem d ON b.SuperColItemCode = d.ColItemCode
WHERE b.ColItemCode = @sColItemCode AND b.ReportID = @ReportID
OPEN curGroupCodeTmp
FETCH NEXT FROM curGroupCodeTmp
INTO @colGroupCode,@SuperColItemCode,@colSuperGroupCode
WHILE @@FETCH_STATUS = 0
BEGIN
--此处进行实际数据处理
IF(ISNULL(@colGroupCode,'')<>'' AND ISNULL(@colSuperGroupCode,'')<>'')
SET @sSQL = 'update ##tmp set ' + @colSuperGroupCode + ' = ''' + @SuperColItemCode
+ ''' where ' + @colGroupCode + ' = ''' + @sColItemCode + ''''
EXECUTE(@sSQL)
--根据条件判断是否结束递归
IF(ISNULL(@SuperColItemCode,'') <> '')
EXEC SetColGroup @SuperColItemCode,@ReportID
FETCH NEXT FROM curGroupCodeTmp
INTO @colGroupCode,@SuperColItemCode,@colSuperGroupCode
END
CLOSE curGroupCodeTmp
DEALLOCATE curGroupCodeTmp
END
C. SQL通过父节点获取所有子节点
这个很简单啊
表结构一般如下tablename(表名)
id--节点ID, name-- 节点名称 parentid父节点ID,
-----获取节点号为6下的所有子节点
select * from tablename t start with id =6 connect by prior id=parentid
D. sql 知道父节点,查询所有的子节点,运用游标,递归,存储过程
呃,因为我不清楚你的表里,这个BPROD
char(15)
老爸
里面记录的值是不是和BMWHS
对应的,所以只能说个大概。
select
sys_connect_by_path(username,'>')
"Path"
from
tmbm
start
with
id=1
connect
by
prior
id=parentid;
其中,id是你要遍历的起始点,比如你想从
厂号=XX
的这个父节点开始寻找他所有的子节点,这里就换成
start
with
BMWHS=XX
然后,connect
by
prior
id=parentid,这里id=parentid,简单解释就是寻找其他记录里,parentid和我的id相同的记录,也就是找子节点。
应该是换成你的BMWHS=BPROD(我不知道你的BPROD
BCHLD
和哪个属性是对应的,是BMWHS吗?)
如果是的话就是下面这样(username是你要返回的值,假设你还是要返回
BSEQ
序号)
select
sys_connect_by_path(BSEQ,'>')
"Path"
from
tmbm
start
with
BMWHS=XX
connect
by
prior
BMWHS=BPROD;
E. SQL语句查询出父节点下的所有子节点
createtable##tmp_users(idint,usernamenvarchar(255),parentidint)
declare@IDint
select@ID=idfromt_Userstwhereexists
(select*fromt_Userst2wheret2.id=t.parentidandt2.username='user1')
execAddSons@ID
select*from##tmp_users
droptable##tmp_users
--存储
createprocereAddSons@idint
as
ifexists(select*fromt_Userswhereparentid=@id)
begin
declare@tmp_IDint
declarecurcursorfor
selectidfromt_Userswhereparentid=@id
opencur
fetchnextfromcurinto@tmp_ID
while@@FETCH_STATUS=0
begin
insertinto##tmp_users
select*fromt_Userstwhereid=@tmp_ID
ifexists(select*fromt_Userswhereparentid=@tmp_ID)
begin
execAddSons@tmp_ID
end
fetchnextfromcurinto@tmp_ID
end
closecur
DEALLOCATEcur
end
--递归调用,不知道是否想要这样
F. SQL递归查询所有子节点
你这样设计的表用递归来显示最恰当 我刚刚好有这个的代码 发给你参考下吧
/// <summary>
/// 绑定根节点
/// </summary>
/// <param name="id"></param>
/// <param name="ddlList"></param>
public void BindSysMenu(string id, DropDownList ddlList)
{
ListItem ll = new ListItem();
ll.Text = "╋--请选择-----";
ll.Value = "-1";
ddlList.Items.Add(ll);
DataTable dt = new ManageContentInfoBll().GetWName(id).Tables[0];
foreach (DataRow dr in dt.Rows)
{
ListItem li = new ListItem();
li.Text = "╋" + dr["W_Name"].ToString();
li.Value = dr["W_ID"].ToString();
ddlList.Items.Add(li);
BindSysMenuChild((dr["W_ID"].ToString()), ddlList, "├—");
}
}
/// <summary>
/// 绑定子节点
/// </summary>
public void BindSysMenuChild(string id, DropDownList ddlList, String separtor)
{
DataTable dt = new ManageContentInfoBll().GetWName(id).Tables[0];
foreach (DataRow dr in dt.Rows)
{
ListItem li = new ListItem();
li.Text = separtor + dr["W_Name"].ToString();
li.Value = dr["W_ID"].ToString();
string separtor_ = separtor + "——";
ddlList.Items.Add(li);
BindSysMenuChild(dr["W_ID"].ToString(), ddlList, separtor_);
}
}
sql 语句 很简单的
select * from WebSubjectMenu where W_Logo ="+id+"
W_Logo 是上级的ID
希望对你有帮助吧。
G. sql语句:如何通过id查找所有的子节点
使用SQL递归查询
Declare@IdInt
Set@Id=0;---在此修改父节点
WithRootNodeCTE(D_ID,D_FatherID,D_Name,lv)
As
(
SelectD_ID,D_FatherID,D_Name,0aslvFrom[LFBMP.LDS].[dbo].[LDS.Dictionary]WhereD_FatherIDIn(@Id)
UnionAll
Select[LFBMP.LDS].[dbo].[LDS.Dictionary].D_ID,[LFBMP.LDS].[dbo].[LDS.Dictionary].D_FatherID,[LFBMP.LDS].[dbo].[LDS.Dictionary].D_Name,lv+1FromRootNodeCTE
InnerJoin[LFBMP.LDS].[dbo].[LDS.Dictionary]
OnRootNodeCTE.D_ID=[LFBMP.LDS].[dbo].[LDS.Dictionary].D_FatherID
)
Select*FromRootNodeCTE
;WithTB([Cd_ID],[ConstituteID],[Cd_PID],[Cd_CName],lv)
as(
Select[Cd_ID],[ConstituteID],[Cd_PID],[Cd_CName],0aslvFROM[LFBMP.Center].[dbo].[ConstituteDetail]Where[Cd_PID]=0And[ConstituteID]=4
unionall
SelectA.[Cd_ID],A.[ConstituteID],A.[Cd_PID],A.[Cd_CName],lv+1FROMTBinnerjoin[LFBMP.Center].[dbo].[ConstituteDetail]asA
onTB.[Cd_ID]=A.Cd_PID
)
Select*FromTB