惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
J
Java Code Geeks
Martin Fowler
Martin Fowler
博客园 - Franky
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
B
Blog
The Cloudflare Blog
F
Fortinet All Blogs
量子位
腾讯CDC
博客园 - 司徒正美
D
Docker
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
D
DataBreaches.Net
小众软件
小众软件

博客园 - lgzh

spring boot 调用python ,研发环境下AttributeError: module 'clip' has no attribute 'load' el-tree选中背景变色 jquery datatable分页 png 变透明 Code First 不自动生成数据库 读取文件不是真实的具体路径 setZh.ini win10 开发mfc 64位 ocx控件 win7 下加载MSCOMCTL.OCX getGLES1ExtensionString: Could not find GLES 1.x config! IUnknown(TVarData(Params[0]).VPointer) as Range win8 VB6打开提示MSCOMCTL.ocx未注册 js 动态添加行,删除行,并获得select中值赋予 input .net 安装包制作 大图 预览 android 创建数字签名应用程序 android java.net.socketexception permission denied an error occurred while signing: '.', hexadecimal value 0x00, is an invalid character IIS配置、403.6 无权查看,网页拒绝访问 C#后台调用Delphi 的Ocx
SQL Server 自定义函数 返回树结构函数
lgzh · 2012-11-01 · via 博客园 - lgzh

数据库结构:

image

表内的数据:

image

自定义函数: 递归查出 树下所有节点 ,参数是 父id

 create  function sss(@id as int)
  returns @t table
  (
    id int not null,
    name int not null,
    pid int null
  )
  as
  begin
  declare @lay as int;
  insert into @t 
  select * from tree where pid =@id;
  
  select @lay = min(id) from tree where pid =@id; --第一次 @lay=5
  
  while @lay is not null
  begin
  
    insert into @t 
    select * from sss(@lay);
    
    select @lay=min(id) from tree
    where id>@lay and pid=@id
  end
  return;
  end
  go
  
  
 
.net代码:
 string cons = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();

        using (SqlConnection con=new SqlConnection(cons))
        {
            if (con.State==ConnectionState.Closed)
            {
                con.Open();
                
            }

            string sql = "select * from sss(@id)";
            SqlCommand cmd = new SqlCommand(sql,con);
          
            cmd.CommandType = CommandType.Text;
           

            cmd.Parameters.Add(new SqlParameter("@id", DbType.Int32)).Value = 4;
            cmd.Parameters.Add("@re",DbType.String);
            cmd.Parameters["@re"].Direction = ParameterDirection.ReturnValue;

            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                int i = 0;
                Response.Write(dr[0].ToString() + "\t\t\t" +dr[1].ToString() +"\t\t\t"+  dr[2].ToString() + "</br>");
                i++;
                
            }




            con.Close();

 }

实现的效果如下:

image