Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have used sys_connect_by_path in ORACLE query.Now I want to convert it to sql server.

SQL
(select type_id,id,cast(name,'\') obs_name  from  prj_obs_units 
start with  id in ( select id  from prj_obs_units  where parent_id is  null )
connect  by  prior id=parent_id) 

can you tell me how to convert this into sql server query , Thanks in Advance.
Posted

1 solution

You will have to use a recursive Common Table Expression (CTE). I don't have your schema, but I believe from your Oracle query that this will yield the same result:

SQL
WITH MyCTE(type_id, id, obs_name)AS
(SELECT type_id, id, CAST([name] AS varchar(1024)) 
FROM prj_obs_units 
WHERE id parent_id IS NULL
UNION ALL
SELECT c.type_id, c.id, CAST(p.obs_name + '\' + c.[name] AS varchar(1024))
FROM prj_obs_units AS c INNER JOIN MyCTE AS p ON c.parent_id = p.id)

SELECT type_id, id, obs_name
FROM MyCTE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900