1
請複製並運行以下腳本。從自我加入表中獲取父母的父母
DECLARE @Locations TABLE
(
LocationId INT,
LocationName VARCHAR(50),
ParentId INT
)
INSERT INTO @Locations SELECT 1, 'Europe', NULL
INSERT INTO @Locations SELECT 2, 'UK', 1
INSERT INTO @Locations SELECT 3, 'England', 2
INSERT INTO @Locations SELECT 4, 'Scotland', 2
INSERT INTO @Locations SELECT 5, 'Wales', 2
INSERT INTO @Locations SELECT 6, 'Cambridgeshire', 3
INSERT INTO @Locations SELECT 7, 'Cambridge', 6
INSERT INTO @Locations SELECT 8, 'North Scotland', 4
INSERT INTO @Locations SELECT 9, 'Inverness', 8
INSERT INTO @Locations SELECT 10, 'Somerset', 3
INSERT INTO @Locations SELECT 11, 'Bath', 10
INSERT INTO @Locations SELECT 12, 'Poland', 1
INSERT INTO @Locations SELECT 13, 'Warsaw', 12
我需要以下一種結果
致謝。
Thanks @Marc_s。使用你的代碼。我設法得到所需的輸出使用臨時表和更新該臨時表。 – Kashif 2011-02-01 12:13:12