postgresql 双精度类型的输入语法无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45570075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:37:25  来源:igfitidea点击:

Invalid input syntax for type double precision

sqldatabasepostgresql

提问by jovicbg

I've created functions and one of them is:

我创建了函数,其中之一是:

CREATE OR REPLACE FUNCTION core.cal_status(
  er_v                 DOUBLE PRECISION,
  cell_v               DOUBLE PRECISION
) RETURNS DOUBLE PRECISION
  LANGUAGE plpgsql
AS $$
DECLARE out DOUBLE PRECISION;
  BEGIN
    IF er_v < 15                                THEN out := 'LOW_LOAD';
    ELSEIF cell_v < 60                          THEN out := 'LOW_LOAD';
    ELSEIF er_v > 40                            THEN out := 'CONGESTED';
    ELSEIF cell_v > 80                          THEN out := 'CONGESTED';
    ELSEIF cell_v >60 and cell_v < 80           THEN out := 'HIGH_LOAD';
  END IF;
  RETURN out;
  END;
$$;

When I call functions with this:

当我用这个调用函数时:

LEFT JOIN LATERAL core.cal_status(
            er_v,
            cell_v) status ON true

I got next error:

我得到下一个错误:

ERROR: invalid input syntax for type double precision: "LOW_LOAD" Where: PL/pgSQL function core.cal_status(double precision,double precision) line 4 at assignment

What is this about, I guess it's something with output type, but not sure.

这是关于什么的,我猜它与输出类型有关,但不确定。

回答by Kevin Miranda

The error happens because the variable outis of text type when you do out :='LOW_LOAD'and the function is expecting to return a DOUBLE PRECISION.

发生错误out是因为在您执行此操作时变量是文本类型,out :='LOW_LOAD'并且该函数期望返回 DOUBLE PRECISION。

Try

尝试

CREATE OR REPLACE FUNCTION core.cal_status(
  er_v                 DOUBLE PRECISION,
  cell_v               DOUBLE PRECISION
) RETURNS TEXT
  LANGUAGE plpgsql
AS $$
DECLARE out TEXT;
  BEGIN
    IF er_v < 15                                THEN out := 'LOW_LOAD';
    ELSEIF cell_v < 60                          THEN out := 'LOW_LOAD';
    ELSEIF er_v > 40                            THEN out := 'CONGESTED';
    ELSEIF cell_v > 80                          THEN out := 'CONGESTED';
    ELSEIF cell_v >60 and cell_v < 80           THEN out := 'HIGH_LOAD';
  END IF;
  RETURN out;
  END;
$$;