powered by simpleCommunicator - 2.0.59     © 2025 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Функция: можно ли вернуть поле одного из типов?
2 сообщений из 2, страница 1 из 1
Функция: можно ли вернуть поле одного из типов?
    #39316476
Ролг Хупин
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Можно ли сделать фунцию, которая возвращает выборку, но одно из полей может быть или целым, или буль, или строкой, в зависимости от условий?

т.е. что-то типа

Код: sql
1.
2.
3.
4.
5.
if cond then
   return query select x,y, int
else
  return query select x,y, bool
end if;
...
Рейтинг: 0 / 0
Функция: можно ли вернуть поле одного из типов?
    #39316499
Фотография Maxim Boguk
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Ролг ХупинМожно ли сделать фунцию, которая возвращает выборку, но одно из полей может быть или целым, или буль, или строкой, в зависимости от условий?

т.е. что-то типа

Код: sql
1.
2.
3.
4.
5.
if cond then
   return query select x,y, int
else
  return query select x,y, bool
end if;



В несколько ограниченном виде. Функции так или иначе надо будет передать тип возвращаемого значения.
В документации есть примеры:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
When the return type of a PL/pgSQL function is declared as a polymorphic type (anyelement, anyarray, anynonarray, anyenum, or anyrange), a special parameter $0 is created. Its data type is the actual return type of the function, as deduced from the actual input types (see Section 35.2.5). This allows the function to access its actual return type as shown in Section 40.3.3. $0 is initialized to null and can be modified by the function, so it can be used to hold the return value if desired, though that is not required. $0 can also be given an alias. For example, this function works on any data type that has a + operator:

CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement)
RETURNS anyelement AS $$
DECLARE
    result ALIAS FOR $0;
BEGIN
    result := v1 + v2 + v3;
    RETURN result;
END;
$$ LANGUAGE plpgsql;

The same effect can be had by declaring one or more output parameters as polymorphic types. In this case the special $0 parameter is not used; the output parameters themselves serve the same purpose. For example:

CREATE FUNCTION add_three_values(v1 anyelement, v2 anyelement, v3 anyelement,
                                 OUT sum anyelement)
AS $$
BEGIN
    sum := v1 + v2 + v3;
END;
$$ LANGUAGE plpgsql;

https://www.postgresql.org/docs/9.5/static/plpgsql-declarations.html#PLPGSQL-DECLARATION-PARAMETERS


--
Maxim Boguk
www.postgresql-consulting.ru
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Функция: можно ли вернуть поле одного из типов?
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]