zoukankan      html  css  js  c++  java
  • Sqlserver 递归查询

    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ALL的不同部分,作为提供数据的部分。

    特别对于UNION ALL比较有用。因为UNION ALL的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用WITH AS短语,则只要执行一遍即可。

    with as 查询的模板 

     with test as 
     (
    select * fromunion all
    select  t.* from 表 t,test m  where t.ID=m.ParentId
     )
     select distinct * from  test order by  ID

    自上到下

      with test as 
     (
    select cb_ProjectAccount.* from cb_ProjectAccount  
    union all
    select  t.* from cb_ProjectAccount t,test m  where t.ParentProjectAccountGUID=m.ProjectAccountGUID
     )
     select distinct * from  test order by  ProjectAccountGUID

    自下到上递归

      ;with test as 
     (
    select cb_ProjectAccount.* from cb_ProjectAccount  
    union all
    select  t.* from cb_ProjectAccount t,test m  where m.ProjectAccountGUID=t.ParentProjectAccountGUID
     )
     select distinct * from  test order by  ProjectAccountGUID desc
  • 相关阅读:
    信息系统项目管理师沟通的四个好习惯
    Android 线程
    替换exe程序图标DLL
    Python 邮件类
    android自适应屏幕方向和大小
    sqlserver 存储过程
    FinalData 数据恢复工具[绿色版]
    Python Python 正则 取中括号值
    在Button任意位置加图片效果
    android GPRS
  • 原文地址:https://www.cnblogs.com/HTLucky/p/12427860.html
Copyright © 2011-2022 走看看