【问题标题】:Error at create function创建函数时出错
【发布时间】:2016-12-20 13:06:07
【问题描述】:

我尝试创建函数

CREATE OR REPLACE FUNCTION public.my_sql_function3(IN inputval integer)
  RETURNS TABLE("ID" integer, name character varying, cnt integer) AS
$BODY$
  select t.id, t.name, CAST(count(*) AS INTEGER)
  from test t
  where t.id < inputval
  group by t.id, t.name $BODY$
  LANGUAGE plpgsql VOLATILE;

并得到错误:

错误:“选择”处或附近的语法错误

第 4 行:选择 t.id、t.name、CAST(count(*) AS INTEGER)

如何解决?

【问题讨论】:

标签: postgresql plpgsql


【解决方案1】:

您已将函数定义为 PL/pgSQL 函数,但您的语法是针对普通 SQL 函数的。

你需要使用

LANGUAGE sql

【讨论】:

  • 添加的备注不正确。 integer 的演员表是必要的。 count(*) 返回 bigint,这会引发错误。
  • @ErwinBrandstetter:由于返回列在returns table() 部分中定义为integer,这种转换不会“自动”发生吗?
  • CREATE FUNCTION 接受至少注册了implicit 转换的数据类型(例如在varchartext 之间)。但是从bigintint 的演员只有assignment(可能溢出!)。详情:stackoverflow.com/a/21051215/939860
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
  • 2014-03-05
  • 1970-01-01
  • 2017-06-08
  • 2013-05-27
  • 1970-01-01
相关资源
最近更新 更多