AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 307269
Accepted
Nir
Nir
Asked: 2022-02-10 23:39:33 +0800 CST2022-02-10 23:39:33 +0800 CST 2022-02-10 23:39:33 +0800 CST

NVL 的 Postgres 函数

  • 772

我正在尝试NVL在 postgres 中使用。

create or replace function nvl (anyelement, anyelement)
returns anyelement language sql as $$
    select coalesce(cast( $1 as decimal), cast( $2 as decimal))
$$;

但是,对于以下示例,这对我来说失败了:

testdb=> select nvl(1,2);
ERROR:  return type mismatch in function declared to return integer
DETAIL:  Actual return type is numeric.
CONTEXT:  SQL function "nvl" during inlining

testdb=> SELECT nvl( sum(balance), 0 ) as b FROM db.bank WHERE user = 123;
ERROR:  function nvl(numeric, integer) does not exist
LINE 1: SELECT nvl( sum(balance), 0 ) as b FROM db.bank...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

当我将其更改为:

create or replace function nvl (anyelement, anyelement)
returns anyelement language sql as $$
    select case when $1 is null then $2 else $1 END 
$$;

第一个例子有效。但我仍然有失败:

testdb=> SELECT nvl( sum(balance), 0 ) as b FROM db.bank WHERE user = 123;
ERROR:  function nvl(numeric, integer) does not exist
LINE 1: SELECT nvl( sum(balance), 0 ) as b FROM db.bank...
       

希望得到一些帮助来解决这个问题。

postgresql functions
  • 1 1 个回答
  • 3689 Views

1 个回答

  • Voted
  1. Best Answer
    klin
    2022-02-11T00:57:41+08:002022-02-11T00:57:41+08:00

    错误是由于对类型使用的误解造成的anyelement。阅读文档:

    声明为 anyelement 的每个位置(参数或返回值)都允许具有任何特定的实际数据类型,但在任​​何给定的调用中,它们都必须是相同的实际类型。

    因此,您应该以建议的形式使用该功能:

    create or replace function nvl (anyelement, anyelement)
    returns anyelement language sql as $$
        select coalesce($1, $2)
    $$;
    

    并确保参数的实际类型完全匹配。如果列balance是数字,则第二个参数也必须是numeric:

    select nvl(sum(balance), 0.0)
    -- or
    select nvl(sum(balance), 0::numeric)
    

    更新。OP 说:

    我无法更改 SQL。只调整功能。

    在这种情况下,您不能使用anyelement参数。您需要使用数字参数创建函数:

    drop function if exists nvl (anyelement, anyelement);
    
    create or replace function nvl (numeric, numeric)
    returns numeric language sql as $$
        select coalesce($1, $2)
    $$;
    

    缺点是该函数仅适用于数字和/或整数参数。Postgres 允许重载函数,因此您可以另外为其他类型创建函数,例如:

    create or replace function nvl (text, text)
    returns text language sql as $$
        select coalesce($1, $2)
    $$;
    
    • 0

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve