Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server中的级别,并从_Sql_Sql Server_Oracle_Levels - Fatal编程技术网

SQL Server中的级别,并从

SQL Server中的级别,并从,sql,sql-server,oracle,levels,Sql,Sql Server,Oracle,Levels,我对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 pri

我对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中获得相同的查询

此查询与级别一起工作,数据位于大量记录中,我需要一个具有不同级别的树

大家能帮帮我吗

致意


Alessandro

您需要将其转换为递归公共表表达式:

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

您需要将其转换为递归公共表表达式:

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

您必须使用不同的方法在sql server中执行层次结构<代码>连接方式特定于ORacle。您可以使用递归CTE实现相同的效果。更好的方法是使用字段,而不是像这里那样使用连接父ID和子ID或表名。
hierarchyID
类似于库代码,这意味着层次结构搜索本质上是范围搜索,可以通过索引进行加速。要获取记录的级别,只需调用
node.GetLevel()
,其中
node
是层次结构字段的名称。您必须使用不同的方法在sql server中执行层次结构<代码>连接方式特定于ORacle。您可以使用递归CTE实现相同的效果。更好的方法是使用字段,而不是像这里那样使用连接父ID和子ID或表名。
hierarchyID
类似于库代码,这意味着层次结构搜索本质上是范围搜索,可以通过索引进行加速。要获取记录的级别,只需调用
node.GetLevel()
,其中
node
是层次结构字段的名称。