我如何 return table 的附加列而不必重复 return 定义中的所有列名称和类型?
How can I return an additional column of a table without having to repeat all column names and types in the return definition?
我 table 有很多列。我想创建一个函数,其中 returns 所有这些列,还有一个附加列。有没有一种方法可以做到这种类型安全(即不返回 record
)而不必重复所有列名和类型?
例如:
create table t
(
t1 int,
t2 int,
t3 text,
t4 boolean
);
create function extra_t() returns table(t1 int, t2 int, t3 text, t4 boolean, extra text) as
$$
select t.*, 'example'::text from t;
$$ language sql
很烦人,我不得不在函数定义中重复t1 int, t2 int, t3 text, t4 boolean
。
我和你一起奋斗,我不知道有什么好的方法。
这就是说,这是执行此操作的一种 hacky 方法。它确实将指定 return 类型中的各个字段的负担转移到了实际的函数文本上,但在我看来,它确实使整个事情更容易理解。
create table t2 (
extra_text text
) inherits (t);
create or replace function extra_t() returns setof t2 as
$$
select t.*, 'example'::text from t;
$$ language sql;
正如我所说,这不是一个好方法。只是一种方式。
我 table 有很多列。我想创建一个函数,其中 returns 所有这些列,还有一个附加列。有没有一种方法可以做到这种类型安全(即不返回 record
)而不必重复所有列名和类型?
例如:
create table t
(
t1 int,
t2 int,
t3 text,
t4 boolean
);
create function extra_t() returns table(t1 int, t2 int, t3 text, t4 boolean, extra text) as
$$
select t.*, 'example'::text from t;
$$ language sql
很烦人,我不得不在函数定义中重复t1 int, t2 int, t3 text, t4 boolean
。
我和你一起奋斗,我不知道有什么好的方法。
这就是说,这是执行此操作的一种 hacky 方法。它确实将指定 return 类型中的各个字段的负担转移到了实际的函数文本上,但在我看来,它确实使整个事情更容易理解。
create table t2 (
extra_text text
) inherits (t);
create or replace function extra_t() returns setof t2 as
$$
select t.*, 'example'::text from t;
$$ language sql;
正如我所说,这不是一个好方法。只是一种方式。