如何使CONNECT BY参数可选

时间:2011-09-21 18:18:05

标签: sql oracle connect-by

我有一个使用Connect By

的程序
SELECT <lots of fields>
FROM Group g
  <joins>
WHERE <where>
CONNECT BY PRIOR g.ID = g.ParentID
START WITH g.ID = 1337
ORDER SIBLINGS BY g.Name
;

数字1337是此过程的参数,如果此值为0,我想忽略connect by代码并执行其他所有操作。

我该如何处理?

1 个答案:

答案 0 :(得分:2)

最明显的答案是在connect by子句中测试异常值:

SELECT <lots of fields>
FROM Group g
  <joins>
WHERE <where>
CONNECT BY PRIOR g.ID = g.ParentID and :param <> 0
START WITH g.ID = :param or :param = 0
ORDER SIBLINGS BY g.Name;