The Below recursive query is returning only one row , i am trying to find the manager hierarchies of all the employees, as part of this initially i tried the below query, its returning only the result of the first query (only 1 row ), could some one please help me in fixing this to get the result of all rows.
WITH emp_cte (empnum,ename,manager) AS (
SELECT emp.empno,emp.ename, cast(emp.mgr as varchar(20)) from emp where emp.mgr is null
union all
select emp_cte.empnum, emp_cte.ename, emp.ename from emp_cte join emp on
emp_cte.manager = cast(emp.empno as varchar(20))
)
SELECT * from emp_cte
when I execute the above query , getting the below result only one row, but not getting the all rows with their managers
Thank your for the reply, as you suggested I changed the query as below
WITH emp_cte (empnum,ename,manager) AS (
SELECT emp.empno,emp.ename, cast(emp.mgr as varchar(20)) from emp where emp.mgr is null
union all
select emp_cte.empnum, emp_cte.ename, emp.ename from emp_cte join emp on
**emp_cte.empnum= emp.mgr**
)
SELECT * from emp_cte
now I am getting the below error when I execute
ORA-32044: cycle detected while executing recursive WITH query
drop table if exists #Emp
create table #Emp ( empnum int , ENAME varchar(20) , manager int )
insert into #Emp select 7839,'KING ',null
insert into #Emp select 7698,'BLAKE ',7839
insert into #Emp select 7782,'CLARK ',7839
insert into #Emp select 7566,'JONES ',7839
insert into #Emp select 7654,'MARTIN ',7698
insert into #Emp select 7499,'ALLEN ',7698
insert into #Emp select 7844,'TURNER ',7698
insert into #Emp select 7900,'JAMES ',7698
insert into #Emp select 7521,'WARD ',7698
insert into #Emp select 7902,'FORD ',7566
insert into #Emp select 7369,'SMITH ',7902
insert into #Emp select 7788,'SCOTT ',7566
insert into #Emp select 7876,'ADAMS ',7788
insert into #Emp select 7934,'MILLER ',7782
;WITH Employee_CTE(empnum,ename,manager) AS
(
SELECT empnum,ename,manager FROM #Emp WHERE empnum=7839
UNION ALL
SELECT e.empnum,e.ename,e.manager FROM #Emp e INNER JOIN Employee_CTE c ON e.manager= c.empnum
)
SELECT
*
FROM
Employee_CTE
ORDER BY
manager