Уровень в SQL Server и начать с - PullRequest
       24

Уровень в SQL Server и начать с

0 голосов
/ 15 февраля 2019

У меня проблема с запросом в SQL Server.

У меня есть этот запрос в ORACLE SQL.

select 
    LEVEL, serial_number, table_, position, description, article, next_table, root_table 
from
    bill_of_material 
where 
    serial_number = ABC.123.ZXC  
start with table_ = root_table
connect by prior next_table=table_
order by level, table_, position

Мне нужно получить тот же запрос в SQL Server.

Этот запрос работает с уровнем, данные находятся в большом количестве записей, и мне нужно дерево с другим уровнем.

Может ли каждый орган помочь мне, пожалуйста?

С наилучшими пожеланиями

Алессандро

1 Ответ

0 голосов
/ 15 февраля 2019

Вам нужно перевести это в рекурсивное общее табличное выражение:

with rcte(level, serial_number, table_, position, description, article, next_table, root_table)
as (
  select 1, serial_number, table_, position, description, article, next_table, root_table
    from bill_of_material
   where serial_number = ABC.123.ZXC
     -- Start with
     and table_ = root_table
  union all
  select prev.level+1
       , curr.serial_number
       , curr.table_
       , curr.position
       , curr.description
       , curr.article
       , curr.next_table
       , curr.root_table
    from rcte prev
    join bill_of_material curr
      on prev.next_table = curr.table_
)
select * from rcte
 order by level, table_, position
...