gpt4 book ai didi

postgresql - 数字类型 : "(0.0000000000000000,8)" 的输入语法无效

转载 作者:行者123 更新时间:2023-11-29 12:02:29 24 4
gpt4 key购买 nike

我在 PostgreSQL 中创建了这个 PL/pgSQL 函数:

CREATE OR REPLACE FUNCTION public.diploma_cal_grade(
amk integer)
RETURNS numeric
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$
DECLARE
a2 integer;
mo1_upox_sum double precision;
mo1_epil_sum double precision;
weig1 integer;
weig2 integer;
upox_sum numeric;
epil_sum numeric;
dipl_g integer ;
cour_g numeric ;
fin_g numeric ;
BEGIN

-----SUM(final_grade*weight)= double precition variable-------
mo1_upox_sum=(Select SUM(final_grade*weight)
FROM ("Course" c join "Register" r USING(course_code)) natural join "Diploma" d
where d.amka=amk and r.register_status='pass' and c.obligatory='true');

weig1=(Select SUM(weight)
FROM ("Course" c join "Register" r USING(course_code)) natural join "Diploma" d
where d.amka=amk and r.register_status='pass' and c.obligatory='true');

--------double precition/integer
upox_sum=(mo1_upox_sum/weig1):: numeric;


a2=(select distinct g.min_courses from "Graduation_rules" g); --for help

-----give me a double precition variable
mo1_epil_sum=(select_courses_maxgrad(amk));
-----give me an integer----
weig2=(select SUM(mk.we)
from(select weight as we
from "Course" c join "Register" r USING(course_code)
where r.amka=amk and c.obligatory='false' and r.register_status='pass'
order by r.final_grade DESC
Limit a2)as mk);


--------double precition/integer
epil_sum=(mo1_epil_sum/weig2):: numeric;

---give me an integer-----
dipl_g=(select thesis_grade from "Diploma" where amka=2);

cour_g=(((upox_sum+epil_sum)/2)*0,8);
fin_g=((dipl_g*0,2)+cour_g);
RETURN fin_g:: numeric(4,2);

END;
$BODY$;

尝试执行它时,出现此错误:

ERROR:  invalid input syntax for type numeric: "(0.0000000000000000,8)"
CONTEXT: PL/pgSQL function diploma_cal_grade(integer) line 45 at assignment
SQL state: 22P02

我不确定为什么;我多次将我的值转换为 numeric 并将我的变量更改为 numeric 类型,但无济于事。

我该如何解决这个问题?

最佳答案

什么 @Laurenz said .

但为什么会出现“奇怪”的错误信息呢?

您的语法错误(0,8 而不是cour_g=(((upox_sum+epil_sum)/2) 中的0.8) *0,8);) 被添加的无偿括号混淆了,它们被解释为 ROW constructors - 前导关键字 ROW 是可选噪音。

您在错误消息中看到的内容:

ERROR:  invalid input syntax for type numeric: "(0.0000000000000000,8)"

... 是一个行表达式,由以下字段组成:0.00000000000000008
没有误导性的括号,作业:

cour_g=((upox_sum+epil_sum)/2)*0,8;  -- still incorrect: 0,8

... 会产生不同的(更具启发性的)错误消息:

ERROR:  query "SELECT ((upox_sum+epil_sum)/2)*0,8" returned 2 columns

因为逗号 (,) 被解释为列分隔符。

此消息还揭示了 PL/pgSQL 在内部使用(基本且快速的)SELECT 语句评估每个裸表达式。该语言基本上是核心 SQL 引擎的包装器。这解释了为什么分配的费用比人们预期的来自其他 PL 的费用略高。

您的原始语句等同于更冗长的 ROW 语法:

cour_g=ROW(((upox_sum + epil_sum)/2)*0,8);  -- still incorrect: 0,8

...在功能上等同于:

cour_g=(SELECT ROW(((upox_sum + epil_sum)/2)*0,8));  -- still incorrect: 0,8

两者都会导致您观察到的相同错误消息。

或者:

cour_g=(SELECT ((upox_sum + epil_sum)/2)*0,8);  -- still incorrect: 0,8

带有不同的(已经更能说明问题的)错误消息:

ERROR:  subquery must return only one column

嵌套的、显式的 SELECT 两边的括号是必需的。如果您删除不需要的 SELECT,请删除环绕括号,否则它们将被解释为 ROW 构造函数(有或没有噪声词 ROW ).

所以使用:

cour_g := (upox_sum + epil_sum) / 2 * 0.8;

((upox_sum+epil_sum)/2) 两边的括号也是多余的,因为 */ 共享相同的 operator precedence并且无论如何都是从左到右评估的。但在这种情况下,添加的括号是无害的噪音(可能有助于也可能不会提高可读性)。

或者简化为:

cour_g := (upox_sum + epil_sum) * 0.4;

也就是说,由于 PL/pgSQL 中的赋值成本相对较高,请尝试采用赋值较少的编程风格。
并非所有不必要的括号都是无害的。

关于postgresql - 数字类型 : "(0.0000000000000000,8)" 的输入语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50049669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com