我在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中获得相同的查询。
此查询适用于级别,数据记录很多,我需要一棵具有不同级别的树。
请大家帮我吗?
最好的问候
亚历山德罗
答案 0 :(得分:0)
您需要将其转换为递归公用表表达式:
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