首页 MsSql SQLserver2008使用表达式递归查询

SQLserver2008使用表达式递归查询

SQLserver2008使用表达式递归查询语句,需要的朋友可以参考下。

<div class=”codetitle”><a style=”CURSOR: pointer” data=”98064″ class=”copybut” id=”copybut98064″ onclick=”doCopy(‘code98064’)”> 代码如下:<div class=”codebody” id=”code98064″>
–由父项递归下级
with cte(id,parentid,text)
as
(–父项
select id,text from treeview where parentid = 450
union all
–递归结果集中的下级
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.parentid = c.id
)
select id,text from cte ——————— –由子级递归父项
with cte(id,text)
as
(–下级父项
select id,text from treeview where id = 450
union all
–递归结果集中的父项
select t.id,t.text from treeview as t
inner join cte as c on t.id = c.parentid
)
select id,text from cte

本文来自网络,不代表云浮站长网立场。转载请注明出处: https://www.0766zz.com/html/shujuku/mssql/20200831/8396.html
上一篇
下一篇

作者: dawei

【声明】:云浮站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

返回顶部