---
title: "SQL SERVER WTIH AS"
author: "CHEEN"
date: "2013-05-03T16:20:54Z"
tags: []
categories: ["数据库"]
slug: "sql-server-wtih-as"
draft: false
lastmod: "2013-05-03T16:20:38Z"
id: 1859394
url: "https://cheen.iteye.com/blog/1859394"
---

```java
with result  
as
(
 select id, parentid,name from syst_dept where id = 'b649a10a3fa8017b003165fea703fa81'
 union all
 select a.id,a.parentid,a.name from syst_dept a join result b on
                                                      -- b.parentid = a.id  -- 向上查询
                                                      a.parentid = b.id  --向下查询
)
select * from result
```