mssql rekurencja podczas tworzenia funkcji

0

Chce stworzyć funkcje która wywołuje w sobie samą siebie(a bardziej przepisać funkcje z postgresa), na wzór coś takiego:

    CREATE FUNCTION dbo.guests_recursive_select_helper(@value BIGINT)
    RETURNS table
AS
return
            (
                SELECT c.child_value
                FROM dbo.guest_table_links c
                WHERE c.parent_guest_id = @value
                  AND c.valid_to_timestamp IS NULL
                UNION ALL
                SELECT dbo.guests_recursive_select_helper(guest_table_links.child_guest_id) as f
                FROM dbo.guest_table_links d
                WHERE d.parent_guest_id = @value
                  AND d.valid_to_timestamp IS NULL
            );

Leci mi tutaj:

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.guests_recursive_select_helper", or the name is ambiguous

Nie za bardzo miałem styczność wcześniej z mssql, hmmm... nie może rozpoznać drugiego selecta przy wywoływaniu funkcji?

2

to jest funkcja tabelaryczna więc musisz ją potraktować jak zwykłą tabelę i wywołać ją w sekcji FROM, mniej więcej coś takiego:

SELECT f.*
FROM dbo.guest_table_links d cross apply dbo.guests_recursive_select_helper(d.child_guest_id) f
WHERE d.parent_guest_id = @value
AND d.valid_to_timestamp IS NULL
0

Problemem może być to, że w definicji funkcji, używasz funkcji, która nie jest zdefiniowana. Nie wiem jednak jak MSSQL sobie (nie)radzi z taką sytuacją.

0

@Paweł Dmitruk: dzięki, to jest ten kierunek

    CREATE FUNCTION dbo.guests_recursive_select_helper(@value BIGINT)
    RETURNS table
AS
return
            (
                SELECT c.child_value
                FROM dbo.guest_table_links c
                WHERE c.parent_guest_id = @value
                  AND c.valid_to_timestamp IS NULL
                UNION ALL
                SELECT f.*
                FROM dbo.guest_table_links d
                    cross apply dbo.guests_recursive_select_helper(d.child_guest_id) as f
                WHERE d.parent_guest_id = @value
                  AND d.valid_to_timestamp IS NULL
            );

Przy poprawionym zapytaniu wyrzuca mi nadal problem:

Invalid object name 'dbo.guests_recursive_select_helper'

Troche truje, ale na co dzień nie mam styczności z mssql...

0

Jeśli to jest cała funkcja, to zrób z tego po prostu rekursywne CTE. Będzie bez wywoływania funkcji w funkcji.

No i trochę nie wiem, po co coś takiego opakowywać w funkcję, skoro można widokiem temat rozwiązać. Ale to temat, jakby, poboczny i pewnie znajdzie się ktoś, kto powie, że nie mam racji.

1
create function dbo.guests_recursive_select_helper(@value bigint)
returns @tab table (child_value bigint)
as
begin
  insert into @tab
  select c.child_value
  from dbo.guest_table_links c
  where c.parent_guest_id = @value
    and c.valid_to_timestamp is null
  union all
  select f.*
  from dbo.guest_table_links d
    cross apply dbo.guests_recursive_select_helper(d.child_guest_id) as f
  where d.parent_guest_id = @value
    and d.valid_to_timestamp is null

  return
end

1 użytkowników online, w tym zalogowanych: 0, gości: 1