Types 如何在postgresql类型中插入到类型中

Types 如何在postgresql类型中插入到类型中,types,insert,postgresql-9.4,Types,Insert,Postgresql 9.4,我有一个表类型newIntList CREATE TYPE newIntList AS ( id bigint ); 要在类型变量中插入整数值 尝试了下面的代码,不工作 CREATE OR REPLACE FUNCTION insertintoType(n1 integer) RETURNS table(id integer) AS $$ declare list newIntList[]; BEGIN insert into l

我有一个表类型newIntList

CREATE TYPE newIntList AS
(
    id bigint
);
要在类型变量中插入整数值

尝试了下面的代码,不工作

  CREATE OR REPLACE FUNCTION insertintoType(n1 integer) 
    RETURNS table(id integer) AS $$
    declare
    list newIntList[];
    BEGIN
    
    insert into list
    select n1;    //looking for a code for inserting into Type "**newIntList**"
    
    
    return query 
    select unnest(list );
    END; $$
    LANGUAGE PLPGSQL;
请帮助

如果要创建“类型实例”,则需要使用

要将一个元素放入数组,只需将其放入,而不需要使用
insert

此外,返回的
id
列的类型不匹配-它必须是
bigint
,才能与类型中的列匹配

您的最终选择与函数结果的定义不匹配
unnest(list)
将返回类型为
newintlist
的单个列,而不是整数(或bigint)。您需要使用
select*from unnest(…)
来实现这一点

因此,函数应该如下所示:

CREATE OR REPLACE FUNCTION insertintoType(n1 integer) 
  RETURNS table(id bigint) --<< match the data type in newintlist
AS $$
declare
  list newintlist[];
BEGIN
  list[0] := row(n1); --<< create a new "instance" of the type and assign it to the first element
  
  return query 
    select * 
    from unnest(list) as l(id);
END; $$
LANGUAGE PLPGSQL;
select *
from insertintotype(1);


但是我不明白为什么不在函数中使用整数或bigint数组。自定义类型似乎没有用处。

在您的示例中,该类型似乎完全没有用处。您能为没有名字的@a_horse_提供帮助吗