SQLserverデータベースでの再帰クエリーの使用について

1281 ワード

メトリック快速開発プラットフォームデータベースはsqlserverデータベースをサポートし、私たちが以前慣れていたoracle再帰クエリー用のstart with dept_id=1000 connect by prior dept_id=upper_idの仕方が効かなくなった.
例えば、私たちの組織機関には多くの下級機関や部門があり、登録者がいる機関を検索し、その機関の下のすべての機関と部門をリストします.Sqlserverの書き方は以下の通りです.
 with NewTable as
(
select a.dept_id,a.dept_name,a.upper_id,a.tree_code,a.extend_type,a.DEPT_LEVEL,a.SORT_ORDER from  SA_DEPT_DICT a where a.DEPT_ID=       ID
union all
select b.dept_id,b.dept_name,b.upper_id,b.tree_code,b.extend_type,b.DEPT_LEVEL,b.SORT_ORDER from SA_DEPT_DICT b  inner join NewTable c on b.DEPT_ID=c.UPPER_ID
where b.dept_TYPE=2 or b.DEPT_TYPE=6
) 
,
 NewTable1 as
(
select a.dept_id,a.dept_name,a.upper_id,a.tree_code,a.extend_type,a.DEPT_LEVEL,a.SORT_ORDER from  SA_DEPT_DICT a , (select  right(MAX(tree_code),4) dept_ID from NewTable where EXTEND_TYPE='  ' ) c where a.DEPT_ID=c.dept_ID
union all
select b.dept_id,b.dept_name,b.upper_id,b.tree_code,b.extend_type,b.DEPT_LEVEL,b.SORT_ORDER from SA_DEPT_DICT b  inner join NewTable1 c on c.DEPT_ID=b.UPPER_ID
where b.dept_TYPE=2 or b.DEPT_TYPE=6
) 
select DEPT_ID,DEPT_NAME,UPPER_ID,SORT_ORDER,EXTEND_TYPE,TREE_CODE from NewTable1

原文住所:http://bbs.delit.cn/thread-153-1-1.html
転載は出典を明記してください.
作成者:メトリックテクノロジーwww.delit.cn