Autor Beitrag
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Mi 23.01.08 17:51 
Hallo,

ich brauche eine Stored Procedure, um eine Baumstruktur auszulesen. Dazu hab ich vor Jahren mal etwas in Firebird gemacht, das folgendermaßen aussah:
ausblenden 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 user profile iconmatze: Code- durch SQL-Tags ersetzt
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Do 24.01.08 09:12 
Ich weiss nicht, wies in SQL-Server ist, aber vielleicht probierst du die Oracle-Syntax dafür mal:

ausblenden SQL-Anweisung
1:
2:
3:
for variable in (select ...) loop
...
end loop;


variable ist dann ein record mit allen Feldern der Tabelle.

Vielleicht gehts in SQL-Server ja ähnlich.

_________________
Bravery calls my name in the sound of the wind in the night...
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Do 24.01.08 10:20 
Das macht man mit einem Cursor.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
declare cursor mycursor [local static forward_only] for select * from foobar
open mycursor

fetch next from mycursor into @var1, @var2, @var2
while (@@fetch_status = 0) begin
  // do something 
  fetch next from mycursor into @var1, @var2, @var2
end

close mycursor
deallocate mycursor

Ich würd's nicht rekursiv machen. SQL2005 kann aber auch rekursive Queries. such mal danach, vor nem Jahr oder so hab ich bei SQLCentral einen Artikel darüber gelesen

_________________
Na denn, dann. Bis dann, denn.
raiguen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 374

WIN 2000prof, WIN XP prof
D7EP, MSSQL, ABSDB
BeitragVerfasst: Do 24.01.08 14:23 
Hier ist das schon mal 'erörtert' worden ;)
UGrohne Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Do 24.01.08 15:07 
user profile iconraiguen hat folgendes geschrieben:
Hier ist das schon mal 'erörtert' worden ;)

Und da hat sogar alzaimer was dazu geschrieben :)

Danke, das hilft uns auf jeden Fall weiter!