PL / SQL:数值或值错误

时间:2016-06-12 08:43:18

标签: oracle plsql ora-06502

我收到错误,我不知道原因:

ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.STANDARD", line 394
ORA-06512: at "DOMINOS.DISTANCE", line 10
ORA-06512: at "DOMINOS.ZOEKWINKELVOORADRES", line 19
ORA-06512: at line 5

光标应包含145行。当我执行该过程时,我在54行之后收到上面的错误消息。

create or replace procedure zoekWinkelVoorAdres
    (v_postcode in postcode.postcode%type,
      v_huisnr in WINKEL.HUISNR%type,
      v_id out WINKEL.ID%type,
      v_afstand out number)
is
    type lat_array is varray(100000) of POSTCODE.LAT%type;
    type lon_array is varray(100000) of POSTCODE.LON%type;
    type id_array is varray(100000) of winkel.id%type;
    a_lat lat_array;
    a_lon lon_array;
    a_id id_array;
    latwin postcode.lat%type;
    lonwin postcode.lon%type;
    latklant postcode.lat%type;
    lonklant postcode.lon%type;
    vafstand number(38);
    cursor winkelafstand is
        select w.ID, p.lat, p.lon 
        from winkel w 
        join postcode p 
        on w.POSTCODE_ID_FK = p.POSTCODE_ID;
begin
    select lat, lon into latklant,lonklant 
    from postcode 
    where postcode = v_postcode;
    open winkelafstand;
    fetch winkelafstand bulk collect into a_id, a_lat, a_lon;
    close winkelafstand;
    for i in a_lat.first..a_lat.last loop
        vafstand := distance(a_lat(i),a_lon(i),latklant,lonklant);
        dbms_output.put_line(vafstand || ' ' || a_id(i));
        insert into winkel_afstand
             (Winkel_ID, afstand) values(a_id(i),vafstand);
    end loop;
end;
/

3 个答案:

答案 0 :(得分:5)

如果您为两组坐标设置相同的位置,则从bit of searching看起来会出现此错误。

假设您的distance函数的定义与该链接示例类似:

CREATE OR REPLACE FUNCTION DISTANCE 
( 
Lat1 IN NUMBER, 
Lon1 IN NUMBER, 
Lat2 IN NUMBER, 
Lon2 IN NUMBER 
) RETURN NUMBER IS 
DegToRad NUMBER := 57.29577951; 
BEGIN
RETURN(6387.7 * ACOS((sin(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad)) + 
(COS(NVL(Lat1,0) / DegToRad) * COS(NVL(Lat2,0) / DegToRad) * 
COS(NVL(Lon2,0) / DegToRad - NVL(Lon1,0)/ DegToRad)))); 
END; 
/

...然后,如果您传递两次相同的值,则计算结果会因为舍入误差而无效,例如与

select distance(53.8662, 10.68117, 53.8662, 10.68117) from dual

为组件添加调试(在函数中,在BEGINRETURN之间)显示:

dbms_output.put_line(lat1 ||','|| lon1);
dbms_output.put_line(sin(NVL(Lat1,0) / DegToRad));
dbms_output.put_line(SIN(NVL(Lat2,0) / DegToRad));
dbms_output.put_line(COS(NVL(Lat1,0) / DegToRad));
dbms_output.put_line(COS(NVL(Lat2,0) / DegToRad)); 
dbms_output.put_line(COS(NVL(Lon2,0) / DegToRad));
dbms_output.put_line(NVL(Lon1,0)/ DegToRad);

.8076421638813717679360124563997362950201
.8076421638813717679360124563997362950201
.5896729051949185735939828069514084977347
.5896729051949185735939828069514084977347
.9826737619730074300608748352929523713616
.1864215844752715888130518254292967904505

当这些相乘并加在一起时,结果为:

1.00000000000000000000000000000000000001

所以整个事情的评估结果为RETURN(6387.7 * ACOS(1.00000000000000000000000000000000000001)),而ACOS(1.00000000000000000000000000000000000001)会抛出相同的错误,至少在PL / SQL中是这样的:

declare
  result number;
begin
  result := acos(1.00000000000000000000000000000000000001);
end;
/

ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.STANDARD", line 394
ORA-06512: at line 4

SQL函数会出现不同的错误:

select acos(1.00000000000000000000000000000000000001) from dual;

SQL Error: ORA-01428: argument '1.00000000000000000000000000000000000001' is out of range

...但问题是同样的,将大于1的值传递给ACOS是没有意义的。

作为一种解决方法,您可以在调用ROUND()之前将函数更改为ACOS(),并使用足够高的参数来对其他计算没有显着影响,但与任何四舍五入的情况一样,它会赢得&#t; t是完美的(但显然不是这样!):

  RETURN(6387.7 * ACOS(ROUND((
    (SIN(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad))
      + (COS(NVL(Lat1,0) / DegToRad)
        * COS(NVL(Lat2,0) / DegToRad)
        * COS(NVL(Lon2,0) / DegToRad - NVL(Lon1,0)/ DegToRad)
        )
    ), 9))
  ); 

随着这一变化:

select distance(53.8662, 10.68117, 53.8662, 10.68117) from dual;

DISTANCE(53.8662,10.68117,53.8662,10.68
---------------------------------------
                                      0

如果您无法更改此功能,则必须比较这些值以确定是否可以安全地调用它。

答案 1 :(得分:1)

解析错误堆栈:

ORA-06502: PL/SQL: numeric or value error

尝试将非数字字符串转换为数字数据类型或其他一些数据转换错误。

ORA-06512: at "SYS.STANDARD", line 394

调用堆栈的底部,抛出异常的程序。由于它是Oracle STANDARD包,这意味着它是内置函数之一,例如TO_NUMBER()

ORA-06512: at "DOMINOS.DISTANCE", line 10

调用上一个抛出错误的函数的过程。

ORA-06512: at "DOMINOS.ZOEKWINKELVOORADRES", line 19

调用上一个函数的过程。

ORA-06512: at line 5 

调用堆栈的顶部,启动它的代码。也许是一个匿名的街区?

因此,您发布的代码ZOEKWINKELVOORADRES()非常有用,因为错误是由DISTANCE()的第10行生成的。我们可以看出,光标的第54行存在值错误。所以你必须调试你的数据集。

基本上您需要将输入记录到DISTANCE()。对于开发,您可以在致电dbms_output.put_line()之前使用DISTANCE()获取a_lat(i)a_lon(i)latklantlonklantUri 。为了在生产中进行稳健的诊断,您应该在持久性存储(日志表或文件)中记录错误和上下文信息,例如输入参数。

答案 2 :(得分:1)

Oracle拥有自己的Spatial库,其中包含处理纬度/经度点之间距离的函数。

Oracle安装程序

CREATE TABLE Postcode (
  postcode_id NUMBER(8,0),
  postcode    VARCHAR2(9),
  location SDO_GEOMETRY
);

INSERT INTO USER_SDO_GEOM_METADATA (
  TABLE_NAME, COLUMN_NAME, DIMINFO, SRID
) VALUES (
  'POSTCODE',
  'LOCATION', 
  SDO_DIM_ARRAY(
    SDO_DIM_ELEMENT('LAT', -90.0, 90.0, 0.5),
    SDO_DIM_ELEMENT('LONG', -180.0, 180.0, 0.5)
  ), 
  8307
);

CREATE INDEX Postcode_SIDX ON Postcode( location )
  INDEXTYPE IS MDSYS.SPATIAL_INDEX;

CREATE TABLE winkel ( id INT, postcode_id INT );

CREATE TABLE winkel_afstand ( id INT, distance NUMBER(10,5) );

测试数据

INSERT INTO winkel
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 2 FROM DUAL UNION ALL
SELECT 3, 3 FROM DUAL UNION ALL
SELECT 4, 4 FROM DUAL;

INSERT INTO Postcode
-- Buckingham Palace, London, England
SELECT 1, 'SW1A 1AA', SDO_GEOMETRY( 2001, 8307, SDO_POINT_TYPE(51.5014, -0.1419,NULL), NULL, NULL) FROM DUAL UNION ALL
-- Big Ben, London, England
SELECT 2, 'SW1A 0AA', SDO_GEOMETRY( 2001, 8307, SDO_POINT_TYPE(51.5007, -0.1246,NULL), NULL, NULL) FROM DUAL UNION ALL
-- Edinburgh CAstle, Edinburgh, Scotland
SELECT 3, 'EH1 2NG',  SDO_GEOMETRY( 2001, 8307, SDO_POINT_TYPE(55.9486, -3.1999,NULL), NULL, NULL) FROM DUAL UNION ALL
-- Snowdon, Llanberis, Wales
SELECT 4, 'LL55 4TY', SDO_GEOMETRY( 2001, 8307, SDO_POINT_TYPE(53.0685, -4.0763,NULL), NULL, NULL) FROM DUAL;

<强>查询

您可以将过程重写为单个INSERT语句(不需要游标,变量或循环):

INSERT INTO winkel_afstand
SELECT w.id,
       sdo_geom.sdo_distance( p.location, q.loc, 0.005, 'unit=mile' )
FROM   winkel w
       INNER JOIN
       postcode p
       ON w.postcode_id = p.postcode_id
       CROSS JOIN
       ( SELECT location AS loc
         FROM   postcode
         WHERE  postcode = 'SW1A 0AA' ) q;

<强>输出

SELECT * FROM winkel_afstand;

        ID   DISTANCE
---------- ----------
         1    1.18963 
         2          0 
         3  373.09907 
         4  292.33809

但是,即使不使用Oracle的空间数据,您仍然可以大大简化您的程序:

CREATE PROCEDURE zoekWinkelVoorAdres (
  v_postcode in  postcode.postcode%type,
  v_huisnr   in  WINKEL.HUISNR%type,
  v_id       out WINKEL.ID%type,
  v_afstand  out number
)
IS
BEGIN
  INSERT INTO winkel_afstand
  SELECT w.id,
         distance(
           lat,
           lon,
           lt,
           ln
         )
  FROM   winkel w
         INNER JOIN
         ( SELECT p.*,
                  FIRST_VALUE( CASE WHEN postcode = v_postcode THEN lat END )
                    IGNORE NULLS OVER () AS lt,
                  FIRST_VALUE( CASE WHEN postcode = v_postcode THEN lon END )
                    IGNORE NULLS OVER () AS ln
           FROM   postcode p ) p
         ON w.postcode_id = p.postcode_id;
END;
/