Question

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 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
Was it helpful?

Solution

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top