SQL Server存储过程中的级别 [英] Level in Sql server stored procedure

查看:120
本文介绍了SQL Server存储过程中的级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格结构如下所示,其中包含groupID,GroupName,ParentGrID.

就像关卡结构.

请通过C#编码或SQL存储过程帮助我获得根组和所有子组.

B-> B1-> B11 .............................



My Table structure is like below with groupID,GroupName,ParentGrID.

It is like Level Structure.

kindly help me through C# coding or SQL Stored Procedure to get the Root Group and all child Group.

B->B1->B11..........................



GroupID 	GroupName 	ParentGroupID
1 	A 	0
2 	A1 	1
3 	B 	0
4 	B1 	3
5 	B2 	3
6 	B11 	4
7 	B3 	3
8 	C 	0
9 	D 	0

推荐答案

您可以在c#
中使用以下代码来实现 //没有经过详细测试,但可以提供帮助
you can do it with below code in c#
// not tested in detail , but surly help
string data = "";
            DataTable dt = new DataTable();
            DataRow[] DR_DT;
            foreach (DataRow dr in dt.Rows)
            {
                DR_DT = dt.Select("ParnetGroupId = '" + dr["GroupId"] + "'");
                for (int i = 0; i < DR_DT.Length; i++)
                {
                    data += DR_DT[i]["GroupName"].ToString();
                    DR_DT = dt.Select("ParnetGroupId = '" + DR_DT[i]["GroupId"] + "'");
                    while(DR_DT.Length> 0)
                    {
                        data += DR_DT[i]["GroupName"].ToString();
                        DR_DT = dt.Select("ParnetGroupId = '" + DR_DT[i]["GroupId"] + "'");
                    }
                }
                
            }



如果解决了您的问题,请标记为解决方案



mark as solution if its solved your problem


这篇关于SQL Server存储过程中的级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆