---
title: "数据库语句-递归树"
author: "simple_hui"
date: "2009-11-16T15:10:53Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2010-11-20T20:11:16Z"
id: 518206
url: "https://simple-hui.iteye.com/blog/518206"
---

oracle
-----------------------------------------------
1.用递归的方法
public List recursion(String departId,List list){
		StringBuffer sb=new StringBuffer();
		sb.append("from Department dept where dept.departParent='"+departId+"'");
		List<Department> deptList=dao.find(sb.toString());
		if(deptList.size()>0){
			list.add(deptList);
			for(int i=0;i<deptList.size();i++){
				deptList.get(i).setDept_list(recursion(deptList.get(i).getDepartId(),list));
			}
		}
		return list;
	}
2.直接用sql语句去查询
 select *
from oms_dept 
start with dept_id='1409' 
connect by prior dept_id = dept_parent


DB2
--------------------------------------------
with emp_temp(id,name)as
( select id,name from emp where name ='xxx'
  union all
  select e.id ,e.name from emp e ,emp_temp t where t.id = e.id
)select name from emp_temp