/ / SYS_CONNECT_BY_PATH no SQL Server - sql, oracle, sql-server-2008, recursão, expressão de tabela comum

SYS_CONNECT_BY_PATH no SQL Server - sql, oracle, sql-server-2008, recursão, expressão de tabela comum

Estou trabalhando em um projeto de migração e preciso converter a seguinte consulta Oracle no equivalente do SQL Server.

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

Estou usando CTEs para fazer isso. Até agora, eu vim com o seguinte:

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

A consulta traduzida fornecendo os seguintes erros:

Mensagem 467, nível 16, estado 1, linha 1
Funções GROUP BY, HAVING ou agregate não são permitidas na parte recursiva de uma expressão de tabela comum recursiva "h $ cte".

Mensagem 462, nível 16, estado 1, linha 1
A junção externa não é permitida na parte recursiva de uma expressão de tabela comum recursiva "h $ cte"

Como posso resolver isso? Qualquer ajuda de qualquer forma é muito apreciada. Desde já, obrigado.

Respostas:

2 para resposta № 1

Eu finalmente resolvi isso como:

WITH dummy AS(

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),

dummy2 as(

select count(p.refactionnr) as Childs

from zisjob.zj_action p inner join dummy b on p.refactionnr=b.actionnr

),

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,

c.Childs,

--cast (row_number() over (order by @@spid) as varchar(max)) as LEVEL,

b.*

from dummy b, dummy2 c

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,c.Childs,

b.*

from dummy b, dummy2 c, 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 * from h$cte

Como não me foi permitido escrever junções externas e GROUP BY, HAVING ou agregar funções dentro do membro recursivo do CTE, eu o mudei para fora sob um novo CTE. Isso funcionou como um encanto.

Felicidades !!!


0 para resposta № 2

Eu o resolvi criando uma função de banco de dados

Crie uma função como "ufn_GetParentPath" use-o em vez de "SYS_CONNECT_BY_PATH" Passagem ID como parâmetro.

CREATE FUNCTION [dbo].[ufn_GetParentPath] ( @pCurrentNodeID    INT )
RETURNS VARCHAR(1000)
AS
BEGIN

DECLARE @vCurrentNodeName     VARCHAR(50)
DECLARE @vParentID            INT

IF @pCurrentNodeID IS NULL OR @pCurrentNodeID = 0
RETURN NULL

SELECT @vCurrentNodeName = [Name], @vParentID = [ParentID]
FROM [dbo].[Hierarchy]
WHERE [ID] = @pCurrentNodeID

RETURN ISNULL([dbo].[ufn_GetParentPath] ( @vParentID ) + "/", "") + @vCurrentNodeName

END
GO