开发者

How to create a recursive path from sybase-ase db?

开发者 https://www.devze.com 2023-02-11 17:06 出处:网络
I have a problem I\'ve previously solved with other DBMS but can\'t find a solution to run with Sybase-ASE 15.0. Is this possible开发者_JS百科 with SQL (or Views) without custom StoredProcedures?

I have a problem I've previously solved with other DBMS but can't find a solution to run with Sybase-ASE 15.0. Is this possible开发者_JS百科 with SQL (or Views) without custom StoredProcedures?

Input:

ID Name Parent
1  a
2  b    1
3  c    1
4  d    3

Output:

ID   PATH
1    a
2    a / b
3    a / c
4    a / c / d


Edit: this is only supported by Sybase SQL Anywhere, not by the "professional" Adaptive Server Enterprise.

According to the manual, Sybase supports recursive common table expressions.

So the following should work:

WITH RECURSIVE hierarchy_path (id, node_path) AS 
(
  SELECT id, 
         name as node_path
  FROM the_unknown_table
  WHERE id = 1

  UNION ALL

  SELECT c.id, 
         p.node_path || ' / ' || c.name
  FROM the_unknown_table c
    JOIN hierarchy_path p ON p.id = c.parent_id 
)
SELECT *
FROM path
ORDER BY id

Not sure if Sybase uses the standard SQL concatenation operator || or something different. As Microsoft is ignoring the standard there, I guess Sybase uses the + as well.


Simple answer - No.

How to get list of values in GROUP_BY clause?

0

精彩评论

暂无评论...
验证码 换一张
取 消