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
前もって感謝します。