Einzelnen Beitrag anzeigen

omata

Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
 
Delphi 7 Enterprise
 
#3

Re: [SQL] Daten nach Nachfolger sortieren

  Alt 8. Dez 2008, 23:04
SQL-Code:
EXECUTE('
DECLARE @done INT
DECLARE @depth INT

SELECT @depth = 1, @done = 0
SELECT *, @depth AS depth, CONVERT(VARCHAR, id) AS nodepath
INTO #temp
FROM tabelle t
WHERE NOT EXISTS (SELECT *
FROM tabelle
WHERE id_danach = t.id)

WHILE @done = 0 BEGIN
SET @depth = @depth + 1
INSERT INTO #temp
SELECT t.*, @depth, x.nodepath +
''|'' + CONVERT(VARCHAR, t.id)
FROM #temp x
INNER JOIN tabelle t
ON x.id_danach = t.id
WHERE x.depth = @depth - 1
IF @@ROWCOUNT = 0 SET @done = 1
END

SELECT *
FROM #temp
ORDER BY nodepath

DROP TABLE #temp
')
Dein Vorgehen ist recht merkwürdig. Normalerweise weist man einem Datensatz seinen Parent zu und nicht seine Children.
Schau auch mal hier
  Mit Zitat antworten Zitat