SQL Server递归查询以获取Top父级

时间:2013-02-15 14:48:11

标签: sql sql-server common-table-expression recursive-query

我需要你帮助在SQL Server中使用CTE进行递归查询。

我有订单号作为输入参数..我需要显示其顶级父发送详细信息。即使我搜索它的孩子..即如果我搜索第3号订单,我应该得到它的顶级父订单号。那是20。

这是我的表结构..

CREATE TABLE #TblSerialNo
    (
      [SRno] [char](20) NOT NULL ,
      [CustSrNo] [varchar](75) NULL
    )
CREATE TABLE #TblSerialReleation
    (
      [SRno] [char](20) NOT NULL ,
      [ChildSRno] [char](20) NOT NULL
    )
CREATE TABLE #TblDespatch
    (
      [SRno] [char](20) NOT NULL ,
      OrderNo INT NOT NULL
    )

INSERT  INTO #TblSerialNo   VALUES  ( 'TS1', 'DD123CV1' )
INSERT  INTO #TblSerialNo   VALUES  ( 'TS2', 'DD123CV2' )
INSERT  INTO #TblSerialNo  VALUES   ( 'TS3', 'DD123CV3' )

INSERT  INTO #TblSerialNo VALUES  ( 'BS1', 'DD12sfs3CV1' )
INSERT  INTO #TblSerialNo VALUES  ( 'BS2', 'DD1et23CV2' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS1', 'DD12e3CV1' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS2', 'DD12fe3CV2' )

INSERT  INTO #TblSerialNo VALUES  ( 'BS1aa', 'DD12d3CV1' )
INSERT  INTO #TblSerialNo VALUES  ( 'BS1ab', 'DDd123CV2' )
INSERT  INTO #TblSerialNo VALUES  ( 'BS1ac', 'DD1r23CV3' )
INSERT  INTO #TblSerialNo VALUES  ( 'BS2aa', 'DDs123CV4' )
INSERT  INTO #TblSerialNo VALUES  ( 'BS2ab', 'DD12d3CV1' )
INSERT  INTO #TblSerialNo VALUES  ( 'BS2ac', 'DD1s23CV2' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS1aa', 'DD1s23CV3' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS1ab', 'DD12s3CV4' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS1ac', 'DD123dCV1' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS2aa', 'DDa123CV2' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS2ab', 'DDa123CV3' )
INSERT  INTO #TblSerialNo VALUES  ( 'CS2ac', 'DDa123CV4' )

--================  Relation table   ==============

INSERT  INTO #TblSerialReleation  VALUES  ( 'TS1', 'BS1' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'TS1', 'BS2' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'TS2', 'CS1' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'TS2', 'CS2' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'BS1', 'BS1aa' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'BS1', 'BS1ab' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'BS1', 'BS1ac' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'BS2', 'BS2aa' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'BS2', 'BS2ab' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'BS2', 'BS2ac' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'CS1', 'CS1aa' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'CS1', 'CS1ab' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'CS1', 'CS1ac' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'CS2', 'CS2aa' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'CS2', 'CS2ab' )
INSERT  INTO #TblSerialReleation  VALUES  ( 'CS2', 'CS2ac' )
--===========  Despatch 
INSERT  INTO #TblDespatch  VALUES  ( 'CS2ac', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'CS2ab', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'CS2ac', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'CS1aa', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'CS1ac', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'CS2ac', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'CS2ac', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'TS1', 1 )
INSERT  INTO #TblDespatch  VALUES  ( 'TS3', 2 )
INSERT  INTO #TblDespatch  VALUES  ( 'TS2', 3 )
INSERT  INTO #TblDespatch  VALUES  ( 'BS2ab', 20 )

DROP TABLE #TblDespatch
DROP TABLE #TblSerialNo
DROP TABLE #TblSerialReleation

提前致谢。

1 个答案:

答案 0 :(得分:2)

查看您的数据,这种关系似乎反过来了(BS2ab(订单20)是TS1(订单3)到BS2的孩子。

如果是这种情况,从孩子(BS2ab)开始,您可以使用以下语句找到其顶级父母:

;with ParentOrders as(
  select 
    convert(char(20), 'BS2ab')as SRno,
    0 as Level 

  union  all

  select r.SRno, o.Level + 1
  from ParentOrders o
  join TblSerialReleation r
    on o.SRNo = r.ChildSRno 
)
select top 1 SRNO 
  from ParentOrders
  order by Level desc

以下是查询的工作示例:http://www.sqlfiddle.com/#!3/e253e/6