Hallo,
ich brauche eine Stored Procedure, um eine Baumstruktur auszulesen. Dazu hab ich vor Jahren mal etwas in Firebird gemacht, das folgendermaßen aussah:
SQL-Anweisung
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
| CREATE PROCEDURE ARTIKELTREE ( ROOT INTEGER, LVL INTEGER) RETURNS ( AID INTEGER, RLVL INTEGER) AS DECLARE VARIABLE SUB_AID INTEGER; begin SELECT aid from artikel WHERE aid=:ROOT INTO :aid;
IF (aid IS NOT NULL) THEN BEGIN RLVL=LVL; SUSPEND; FOR SELECT aid FROM artikel WHERE parent=:root ORDER BY aid INTO :sub_aid DO BEGIN FOR SELECT aid, rlvl FROM artikeltree(:sub_aid,:lvl+1) INTO :aid,:rlvl DO SUSPEND; END END END |
Und das brauche ich in etwa im SQL Server. Das größte Problem ist, dass ich keine Ahnung habe, wie ich in einer Schleife alle Elemente einer Abfrage durchgehend und etwas damit machen kann, so wie es in dieser FB-Prozedur der Fall ist (siehe hervorgehobene Stellen), also eine Art "For each".
Hat da jemand Ahnung von?
Moderiert von
matze: Code- durch SQL-Tags ersetzt