CTEでSPを書きました。
CREATE PROC [dbo].[CategoryListShow]
@id AS INT
AS
WITH CategoryList
AS
(
SELECT parent.Categoryid, CONVERT(varchar(50),parent.CategoryName)
as
Name, parent.CategoryParentid
FROM Category as parent
WHERE parent.CategoryParentid IS NULL
UNION ALL
SELECT child.Categoryid, CONVERT(varchar(50),CL.Name + ' > ' + child.CategoryName)
as Name, child.CategoryParentid
FROM Category as child
INNER JOIN CategoryList as CL ON child.CategoryParentid = CL.Categoryid
WHERE child.CategoryParentid IS NOT NULL
)
SELECT Name from CategoryList option (maxrecursion 0)
どうすれば目的の出力を達成できますか? たとえば、ユーザーが入力した場合id = 14111
、出力は次のようになります。 Everything Else > Test Auctions > General
私のテーブル構造:
ありがとう