Hallo!
Ich habe hier einen L�sungsansatz mit einer tempor�ren Tabelle. Idealerweise
w�rde man sowas mit rekursion l�sen, aber das ist wohl in SQL nicht m�glich.
Also:
DROP TABLE dbo.relations
GO
DROP procedure dbo.GetRelations
GO
create table dbo.relations
(
vater int NOT NULL,
kind int NOT NULL
)
GO
INSERT INTO relations VALUEs(3,7)
INSERT INTO relations VALUEs(3,8)
INSERT INTO relations VALUEs(4,9)
INSERT INTO relations VALUEs(7,12)
INSERT INTO relations VALUEs(12,13)
GO
CREATE procedure dbo.GetRelations
@P_child int
AS
SET NOCOUNT ON
DECLARE @L_count int
DECLARE @L_count2 int
CREATE TABLE #temp
(
relationID INT NOT NULL
)
INSERT INTO #temp VALUES(@P_child)
SELECT @L_count = 0
SELECT @L_count2 = count(*) FROM #temp
WHILE @L_count < @L_count2
BEGIN
SELECT @L_count = @L_count2
INSERT INTO #temp
SELECT vater
FROM relations A
INNER JOIN #temp B ON A.kind = B.relationID
LEFT OUTER JOIN #temp C ON A.vater = C.relationID
WHERE C.relationID IS NULL
SELECT @L_count2 = count(*) FROM #temp
END
SELECT * FROM #temp
GO
GetRelations 13
HTH,
Christoph
> -----Original Message-----
> From: Mayer, Stefan [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, October 18, 2001 3:23 PM
> To: ASP Datenbankprogrammierung
> Subject: [aspdedatabase] Feststellen, ob a ein Kind von b ist....
>
>
> ich habe in einer Tabelle Vater-Kind-Beziehungen abgebildet. Wie kann
> ich ganz leicht (mit m�glichste einer einzigen Abfrage)
> feststellen, ob
> ein beliebiger Wert ein Vorfahre von a ist.
>
>
> Wert - Kind
> 3 | 7
> 3 | 8
> 4 | 9
> ...
> 7 | 12
> 12 | 13
>
>
> Wenn ich nun den Wert "13" habe, m�chte ich als Ergebnis der Abfrage
> folgendes bekommen:
>
> 13
> 12
> 7
> 3
>
> Stefan.....
>
> | [aspdedatabase] als [EMAIL PROTECTED] subscribed
> | http://www.aspgerman.com/archiv/aspdedatabase/ = Listenarchiv
> | Sie k�nnen sich unter folgender URL an- und abmelden:
> | http://www.aspgerman.com/aspgerman/listen/anmelden/aspdedatabase.asp
>
| [aspdedatabase] als [email protected] subscribed
| http://www.aspgerman.com/archiv/aspdedatabase/ = Listenarchiv
| Sie k�nnen sich unter folgender URL an- und abmelden:
| http://www.aspgerman.com/aspgerman/listen/anmelden/aspdedatabase.asp