0

我收到一个错误,我不知道为什么:

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;
/
4

3 回答 3

5

通过一些搜索,如果您为两组坐标提供相同的位置,您似乎会收到此错误。

假设您的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

为组件添加调试(在函数中,介于BEGIN和之间RETURN)显示:

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(),并使用足够高的参数来不会显着影响其他计算,尽管与任何舍入一样,它不会是完美的(但显然不是!):

  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

如果您无法更改该函数,那么您将不得不比较这些值以确定调用它是否安全。

于 2016-06-12T12:29:00.987 回答
1

解析错误堆栈:

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

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

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

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

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

调用前一个引发错误的函数的过程。

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

调用前一个函数的过程。

ORA-06512: at line 5 

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

因此,您发布的代码ZOEKWINKELVOORADRES()不是很有帮助,因为错误是由DISTANCE(). 我们可以看出,游标的第 54 行存在值错误。所以你必须调试你的数据集。

基本上,您需要将输入记录到DISTANCE(). 对于开发,您可以dbms_output.put_line()在调用DISTANCE()which shoes a_lat(i)a_lon(i)和. 为了在生产中进行可靠的诊断,您应该在持久存储(日志表或文件)中记录错误和上下文信息,例如输入参数。latklantlonklant

于 2016-06-12T11:35:36.087 回答
1

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;
/
于 2016-06-12T22:25:32.793 回答