类型为numeric的输入语法无效:“(0.0000000000000000,8)”

时间:2018-04-26 18:22:19

标签: postgresql variable-assignment plpgsql

我在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类型,但无济于事。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

@Laurenz said

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

您的语法错误0,8而不是0.8中的cour_g=(((upox_sum+epil_sum)/2)*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中的作业相对较贵,因此尝试采用编程方式较少的作业。
并非所有无偿的括号都是无害的。

答案 1 :(得分:1)

问题在于这一行:

cour_g=(((upox_sum+epil_sum)/2)*0,8);

小数点分隔符必须是句点,因此您必须使用0.8而不是0,8