2

私は移行プロジェクトに取り組んでおり、次の Oracle クエリを 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

これを行うためにCTEを使用しています。これまでのところ、私は次のことを思いつきました:

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

次のエラーを与える翻訳されたクエリ:

メッセージ 467、レベル 16、状態 1、行 1
GROUP BY、HAVING、または集計関数は、再帰共通テーブル式 'h$cte' の再帰部分では使用できません。

メッセージ 462、レベル 16、状態 1、行 1
外部結合は、再帰共通テーブル式 'h$cte' の再帰部分では許可されていません

どうすればこれを回避できますか? どんな形であれ、どんな助けも大歓迎です。前もって感謝します。

4

2 に答える 2

2

私は最終的にこれを次のように解決しました:

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

CTE の再帰メンバー内に外部結合と GROUP BY、HAVING、または集計関数を記述することは許可されていなかったので、新しい CTE の下に移動しました。それは魅力のように機能しました。

乾杯 !!!

于 2012-10-22T07:26:06.633 に答える
2

データベース関数を作成して解決しました

「ufn_GetParentPath」のような関数を作成し、パラメーターとして「SYS_CONNECT_BY_PATH」を 渡す代わりにそれを使用IDします。

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
于 2017-05-03T04:28:12.583 に答える