DECLARE @NodeHierarchy AS TABLE ( PK_NodeID INT NOT NULL ...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

DECLARE @NodeHierarchy AS TABLE (
  PK_NodeID INT NOT NULL PRIMARY KEY,
  FK_ParentNodeID INT NULL,
  Name NVARCHAR(255) NOT NULL
);

INSERT INTO @NodeHierarchy(PK_NodeID, FK_ParentNodeID, Name)
VALUES
  (1, NULL, N'Scotland'),
  (2, 1, N'Glasgow'),
  (3, 1, N'Edinburgh'),
  (4, 1, N'St Andrews'),
  (5, 2, N'The Barrowlands'),
  (6, 2, N'The Cathouse'),
  (7, 2, N'Carling Academy'),
  (8, 2, N'SECC'),
  (9, 2, N'King Tut''s Wah-Wah Hut'),
  (10, 3, N'Henry''s Cellar Bar'),
  (11, 3, N'The Bongo Club'),
  (12, 3, N'Sneaky Pete''s'),
  (13, 3, N'The Picture House'),
  (14, 3, N'Potterrow'),
  (15, 4, N'Aikman''s'),
  (16, 4, N'The Union'),
  (17, 4, N'Castle Sands');

-- Sandwich this in "digraph G { }"
SELECT 'node' + CONVERT(varchar, PK_NodeID) + ' -- node' + CONVERT(varchar, FK_ParentNodeID) + ';'
FROM @NodeHierarchy
WHERE FK_ParentNodeID IS NOT NULL;

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached