【问题标题】:Oracle: why does the SQL code work in a view, but not in a materialized view?Oracle:为什么 SQL 代码在视图中有效,但在物化视图中无效?
【发布时间】:2022-12-15 23:09:49
【问题描述】:

我需要将视图中的一些 SQL 代码更改为物化视图。

但是当我复制并使用它时,出现错误:

ORA-32480: 只能为递归 WITH 子句元素指定 SEARCH 和 CYCLE 子句

我不明白为什么在视图中有效的东西在垫子中也有效。看法。

有没有人知道我如何摆脱错误?调用另一个查询的联接会出现问题。我也尝试了递归,但不知何故不起作用。

with
  z1  (einheit_id, ancestor_einheit_id, ueb_einheit_id, is_root, kiste_id, nodepath)  as (
       select     e.id as einheit_id, e.id as ancestor_einheit_id, e.ueb_einheit_id, 0 as is_root,  e.kiste_id, cast(to_char(e.id) as varchar2(1024)) as nodepath
       from       r_be_einheit e
       where      e.kiste_id = -2
       union all
       select     z1.einheit_id, coalesce(e1.id,e2.id) as ancestor_einheit_id, coalesce(e1.ueb_einheit_id, e2.ueb_einheit_id) as ueb_einheit_id,
                  0 as is_root, coalesce(e1.kiste_id,e2.kiste_id) as kiste_id,
                  z1.nodepath ||  '/' || cast(to_char(coalesce(e1.id,e2.id)) as varchar2(1024)) as nodepath                 
       from       z1
       left join  r_be_einheit e1 on e1.id = z1.ueb_einheit_id
       left join  r_be_einheit e2 on e2.merge_einheit_id = z1.ancestor_einheit_id
       where      z1.is_root = 0 and (e1.id is not null or e2.id is not null) and instr(z1.nodepath, '/' || to_char(coalesce(e1.id,e2.id))) = 0
       )  cycle nodepath set is_cycle to 1 default 0
    ,
    einheiten as (
    select      e.id as be_einheit_id,
                e.barcode,
                e.objektart_id
    from        r_be_einheit e
    left join   z1 on e.id = z1.einheit_id
  )
  ,
  og_zuo0 as (
    select e.barcode
    from  einheiten e
  )
  ,
  og_zuo1 as (
    select *
    from einheiten e
    join og_zuo0 on og_zuo0.barcode = e.barcode
  )
  select * from og_zuo1

我得到的代码很深,错误仍然出现。我得到的代码很深,错误仍然出现。 这是最后一次加入。如果我删除 og_zuo1 并在最后选择 og_zuo0 则不会出现错误。但我不明白为什么。

【问题讨论】:

    标签: sql oracle recursion view materialized-views


    【解决方案1】:

    您可以将代码重写为:

    with z1  (einheit_id, ancestor_einheit_id, ueb_einheit_id, is_root, kiste_id, nodepath) as (
      select id,
             id,
             ueb_einheit_id,
             0,
             kiste_id,
             cast(to_char(e.id) as varchar2(1024))
      from   r_be_einheit
      where  kiste_id = -2
    union all
      select z1.einheit_id,
             coalesce(e1.id,e2.id),
             coalesce(e1.ueb_einheit_id, e2.ueb_einheit_id),
             0,
             coalesce(e1.kiste_id,e2.kiste_id) as kiste_id,
             z1.nodepath ||  '/' || to_char(coalesce(e1.id,e2.id))
      from   z1
             left join  r_be_einheit e1
             on e1.id = z1.ueb_einheit_id
             left join  r_be_einheit e2
             on e2.merge_einheit_id = z1.ancestor_einheit_id
      where  z1.is_root = 0
      and    (e1.id is not null or e2.id is not null)
      and    instr(z1.nodepath, '/' || to_char(coalesce(e1.id,e2.id))) = 0
    ) cycle nodepath set is_cycle to 1 default 0,
    einheiten as (
      select e.id as be_einheit_id,
             e.barcode,
             e.objektart_id
      from   r_be_einheit e
             left join z1
             on e.id = z1.einheit_id
    )
    select e.*,
           e0.barcode
    from   einheiten e
           join einheiten e0
           on e0.barcode = e.barcode
    

    虽然我不确定最终查询中自连接的值是多少;但它在那里是因为它复制了og_zuo1 中的连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-10
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多