I am working on a migration project and need to convert following Oracle query into it’s SQL Server equivalent.
select SYS_CONNECT_BY_PATH (b.actionnr,'/') as FATHER, SYS_CONNECT_BY_PATH (b.actionnr,' | ') as REFPATH, LEVEL,
(select count(p.refactionnr) from zisjob.zj_action p where p.refactionnr=b.actionnr)
Childs, b.* from
( select NVL(x.ANZFiles,0) ANZFiles, act.actionnr, act.refactionnr, act.lfno, act.jobnr, act."TYPE", act.actiontype
from zisjob.zj_action act, zisjob.zj_actiontype t,
( select f.lno, count(f.filenr) as ANZFiles from zisjob.zj_file f where f.lno != f.lfno Group by f.lno )x
where act.lfno=10 and act.actiontype = t.typeid(+) and act.actionnr = x.lno(+) )
b start with b.actionnr in
(select b.actionnr from zisjob.zj_action b where b.lfno = 10 and b.refactionnr is null)
connect by nocycle prior b.actionnr=b.refactionnr order by 1 desc, 2 asc
I am using CTEs to do this. So far, I’ve come up with following :
with h$cte as
(
select
cast (convert(varchar,b.actionnr)+'/' as varchar(max)) as FATHER,
cast(convert(varchar,b.actionnr)+' | ' as varchar(max)) as REFPATH,
1 as LEVEL,
--cast (row_number() over (order by @@spid) as varchar(max)) as LEVEL,
(select count(p.refactionnr) from zisjob.zj_action p
where p.refactionnr = b.actionnr) Childs,
b.*
from
(select
isnull(x.ANZFiles, 0) ANZFiles, act.actionnr, act.refactionnr, act.lfno,
act.jobnr, act."TYPE", act.actiontype
from zisjob.zj_action act
left outer join zisjob.zj_actiontype t on act.actiontype = t.typeid
left outer join
(select f.lno, count(f.filenr) as ANZFiles
from zisjob.zj_file f
where f.lno != f.lfno Group by f.lno ) x on act.actionnr = x.lno
where act.lfno = 10) b
where
b.actionnr in
(select b.actionnr from zisjob.zj_action b
where b.lfno = 10 and b.refactionnr is null)
UNION ALL
select
CAST(FATHER + '/'+ b.ACTIONNR as varchar(max)) as FATHER,
CAST(REFPATH + '|'+b.ACTIONNR AS VARCHAR(MAX)) as REFPATH,
h$cte.LEVEL + 1 as LEVEL,
(select count(p.refactionnr) from zisjob.zj_action p
where p.refactionnr = b.actionnr) Childs,
b.*
from
(select isnull(x.ANZFiles, 0) ANZFiles, act.actionnr, act.refactionnr,
act.lfno, act.jobnr, act."TYPE", act.actiontype
from zisjob.zj_action act
left outer join zisjob.zj_actiontype t on act.actiontype = t.typeid
left outer join
(select f.lno, count(f.filenr) as ANZFiles from zisjob.zj_file f
where f.lno != f.lfno Group by f.lno) x on act.actionnr = x.lno
where act.lfno = 10) b,
h$cte
where
b.actionnr in
(select b.actionnr from zisjob.zj_action b
where b.lfno = 10 and b.refactionnr is null)
and h$cte.ACTIONNR = h$cte.REFACTIONNR
)
select <columns> from h$cte
The translated query giving following errors:
Msg 467, Level 16, State 1, Line 1
GROUP BY, HAVING, or aggregate functions are not allowed in the recursive part of a recursive common table expression ‘h$cte’.Msg 462, Level 16, State 1, Line 1
Outer join is not allowed in the recursive part of a recursive common table expression ‘h$cte’
How can I work around this? Any help in any form is greatly appreciated. Thanks in advance.
I’ve finally solved this as:
Since I was not allowed to write outer joins and GROUP BY, HAVING, or aggregate functions inside recursive member of CTE, I’ve moved it outside under a new CTE. That worked like a charm.
Cheers !!!