加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle – 获取较小数字的数字溢出错误,但较大数字成功

发布时间:2020-12-12 13:09:47 所属栏目:百科 来源:网络整理
导读:以下是我的测试功能: create or replace FUNCTION testRETURN NUMBERAS v_count number(15); v_msisdn number(15);BEGIN v_msisdn:= 225952 * 10000; -- v_msisdn:=50510060853 * 10000; return v_msisdn;END; 当我执行该功能时 select test() from dual;
以下是我的测试功能:
create or replace FUNCTION test
RETURN NUMBER
AS
  v_count   number(15);
  v_msisdn  number(15);
BEGIN
  v_msisdn:= 225952 * 10000;
  -- v_msisdn:=50510060853 * 10000;

  return v_msisdn;
END;

当我执行该功能时

select test() from dual;

我收到一个错误

1426. 00000 -  "numeric overflow"
*Cause:    Evaluation of an value expression causes an overflow/underflow.

但是,如果我更新

v_msisdn:=50510060853 * 10000;

我没有得到错误.

有人可以解释这个行为,因为第二个查询是一个更大的数字?

您可以通过将整数文字之一转换为整数或数字来解决此问题:
v_msisdn:= cast(225952 as integer) * 10000;

出于性能原因,小整数文字被视为pls_integer.不幸的是,这种类型会在溢出时引发异常:

A calculation with two PLS_INTEGER values that overflows the PLS_INTEGER range raises an overflow exception,even if you assign the result to a NUMBER data type For calculations outside the PLS_INTEGER range,use INTEGER,a predefined subtype of the NUMBER data type.

在第二个示例中,其中一个值(50510060853)不适合pls_integer的范围,因此被解释为整数.

这是AskTom上的类似问题:Datatype Number results in Numeric overflow although value is small enough

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读