SQL 在 Postgres 中将 Const Integer 转换为 Bigint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9793422/
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-09-01 14:55:45  来源:igfitidea点击:

Cast Const Integer to Bigint in Postgres

sqlpostgresqlcastingtype-conversion

提问by Nick Vaccaro

I'm getting the below error while running the below script. My goal is to create a function in Postgres to return 1 as a bigint. Help please!

运行以下脚本时出现以下错误。我的目标是在 Postgres 中创建一个函数以将 1 作为 bigint 返回。请帮忙!

hashtagpostgresnoobie

标签postgresnoobie

ERROR: function result type must be bigint because of OUT parameters

错误:由于 OUT 参数,函数结果类型必须是 bigint

CREATE OR REPLACE FUNCTION GetNumberOne(
    OUT numberone bigint)
  RETURNS SETOF record AS
$BODY$

    SELECT CAST(1 AS BIGINT) AS "NUMBERONE";

$BODY$
  LANGUAGE sql VOLATILE;

回答by Timur Sadykov

You've suddenly encountered the feature ) Record needs two and more fields. So when you have only one out variable, then result must be scalar.

您突然遇到了功能 ) 记录需要两个或更多字段。因此,当您只有一个输出变量时,结果必须是标量。

So, you can simply do what compilers ask )

因此,您可以简单地执行编译器要求的操作)

CREATE OR REPLACE FUNCTION GetNumberOne(
        OUT numberone bigint)
      RETURNS bigint AS
    $BODY$

        SELECT CAST(1 AS BIGINT) AS "NUMBERONE";

    $BODY$
      LANGUAGE sql VOLATILE;

plpgsql example:

plpgsql 示例:

CREATE OR REPLACE FUNCTION NumberOne()
      RETURNS bigint AS
    $BODY$
      DECLARE num bigint;
      BEGIN
        num := 1;
        RETURN num;
      END
    $BODY$
      LANGUAGE plpgsql VOLATILE;
      select * from NumberOne()