SQL 递归查询示例

SQL 递归查询示例

ID:37713006

大小:58.50 KB

页数:19页

时间:2019-05-29

SQL 递归查询示例_第1页
SQL 递归查询示例_第2页
SQL 递归查询示例_第3页
SQL 递归查询示例_第4页
SQL 递归查询示例_第5页
资源描述:

《SQL 递归查询示例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、SQL递归查询示例:createtable#EnterPrise( Departmentnvarchar(50),--部门名称 ParentDeptnvarchar(50),--上级部门 DepartManagenvarchar(30)--部门经理)insertinto#EnterPriseselect'技术部','总经办','Tom'insertinto#EnterPriseselect'商务部','总经办','Jeffry'insertinto#EnterPriseselect'商务一部','商务部','ViVi'insert

2、into#EnterPriseselect'商务二部','商务部','Peter'insertinto#EnterPriseselect'程序组','技术部','GiGi'insertinto#EnterPriseselect'设计组','技术部','yoyo'insertinto#EnterPriseselect'专项组','程序组','Yue'insertinto#EnterPriseselect'总经办','','Boss'--查询部门经理是Tom的下面的部门名称;withhgoas(  select*,0asrankfro

3、m#EnterPrisewhereDepartManage='Tom'  unionall  selecth.*,h1.rank+1from#EnterPrisehjoinhgoh1onh.ParentDept=h1.Department)select*fromhgo/*Department          ParentDept               DepartManage     rank------------------------------------------------------------------

4、---技术部              总经办                   Tom              0程序组              技术部                   GiGi             1设计组              技术部                   yoyo             1专项组              程序组                   Yue              2*/--查询部门经理是GiGi的上级部门名称;withhgoas(  se

5、lect*,0asrankfrom#EnterPrisewhereDepartManage='GiGi'  unionall  selecth.*,h1.rank+1from#EnterPrisehjoinhgoh1onh.Department=h1.ParentDept)select*fromhgo/*Department              ParentDept         DepartManage   rank------------------------------------------ ----------

6、- -----------程序组                  技术部                GiGi          0技术部                  总经办                Tom           1总经办                                         Boss          2*/--==================================================================================

7、=========================createtable#tt(idint,parentidint,namevarchar(20))insert#ttselect1,0,'上海市'unionallselect2,1,'虹口区'unionallselect3,1,'徐汇区'unionallselect4,1,'浦东新区'unionallselect5,2,'江湾镇'unionallselect6,2,'虹口足球场'unionallselect7,5,'吉买盛'unionallselect8,5,'易卜莲花'union

8、allselect9,5,'农工商'unionallselect10,6,'吉买盛'withcteas(select*,0aslevlfrom#ttwhereid=2unionallselecta.*,b.levl+1from#tta,ctebwh

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。