小编jia*_*ian的帖子

整数类型的输入语法无效:执行函数时具有复合数据类型的“(2,2)”

begin;
create type public.ltree as (a int, b int);
create  table public.parent_tree(parent_id int,l_tree ltree);
insert into public.parent_tree values(1,(2,2)),(2,(1,2)),(3, (1,28));
commit;
Run Code Online (Sandbox Code Playgroud)

尝试复制此答案中的解决方案:

对于复合类型的函数:

CREATE OR REPLACE FUNCTION public.get_parent_ltree
            (_parent_id int, tbl_name regclass , OUT _l_tree ltree)
  LANGUAGE plpgsql AS
$func$
BEGIN
   EXECUTE format('SELECT l_tree FROM %s WHERE parent_id = $1', tbl_name)
   INTO  _l_tree
   USING _parent_id;
END
$func$;
Run Code Online (Sandbox Code Playgroud)

执行的有效查询:

select l_tree from parent_tree where parent_id = 1;
Run Code Online (Sandbox Code Playgroud)

执行函数:

select get_parent_ltree(1,'parent_tree');
select get_parent_ltree(1,'public.parent_tree');
Run Code Online (Sandbox Code Playgroud)

我收到此错误

begin;
create type …
Run Code Online (Sandbox Code Playgroud)

postgresql plpgsql variable-assignment rowtype

4
推荐指数
1
解决办法
950
查看次数