Link to home
Start Free TrialLog in
Avatar of Baber Amin
Baber AminFlag for Canada

asked on

Convert Oracle SQL with Connect by prior to SQL Server SQL

Hi,
Please convert below Oracle SQL with Connect by prior to SQL Server SQL

select count(*)
from tree
connect by prior
dataid = parentid
start with dataid=%1
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

What output you are looking at?

Try.. this for MS SQL Server.. Please let me know if any changes are required..

WITH CTE AS
(
    SELECT 1 ID , NULL parentID 
    UNION ALL
    SELECT 2 ID , 1  UNION ALL
    SELECT 3 ID , 1  UNION ALL   
    SELECT 4 ID , 2
)
,CTE1 AS 
(
     Select E.Id, 0 Level              
     From CTE E Where E.parentid IS NULL
     UNION ALL
     Select E.Id, c.Level + 1 
     From CTE E INNER JOIN CTE1 c on c.Id = e.parentid 
)
SELECT COUNT(*) FROM CTE

Open in new window


Hope it helps!!
Avatar of Baber Amin

ASKER

Confusing.
My table name is TREE.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
its giving me zero result.
Hi,

Could you please few rows and the expected output ?
Hi baberamin,
Any luck with this :) ?

Regards,
Pawan