简体   繁体   English

ORA-06502: PL/SQL: 连接时出现数字或值错误

[英]ORA-06502: PL/SQL: numeric or value error when concatenating

DECLARE
    a NUMBER;
    b NUMBER;
BEGIN
    a :=: a;
    b :=: b;
    DBMS_OUTPUT.PUT_LINE('sum = '|| a+b);
END;

I am getting error as numeric or value error我收到错误为数字或值错误

A simplified version to clarify the issue:澄清问题的简化版本:

begin
    dbms_output.put_line('Result ' || 2 + 2);
end;
/

ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2

This fails because the expression is built left to right, and you can't add 2 to the string 'Result: 2' .这失败了,因为表达式是从左到右构建的,您不能将2添加到字符串'Result: 2'

It would work with multiplication or division, because arithmetic precedence causes those operations to be evaluated first.它适用于乘法或除法,因为算术优先级导致首先评估这些操作。

begin
    dbms_output.put_line('Result ' || 2 / 2);
end;
/

Result 1

PL/SQL procedure successfully completed.

To allow this to work for any operation, you need to bracket the expression:要使其适用于任何操作,您需要将表达式括起来:

begin
    dbms_output.put_line('Result ' || (2 + 2));
end;
/

Result 4

PL/SQL procedure successfully completed.

Just to add, it is clearer to write host/bind variables without any space after : , for example :myvar rather than : myvar .只是要补充一点,在:之后写没有任何空格的主机/绑定变量更清楚,例如:myvar而不是: myvar

The problem is with operator precedence :问题在于运算符优先级

Oracle evaluates operators with equal precedence from left to right within an expression. Oracle 在表达式中从左到右计算具有相同优先级的运算符。

... and as the table shows + and || ...并且如表所示+|| have equal precedence.具有同等优先权。 So in this statement:所以在这个声明中:

DBMS_OUTPUT.PUT_LINE('sum = '|| a+b);

this is interpreted as 'sum = '|| a这被解释为'sum = '|| a 'sum = '|| a which gives you a string, then <result string> + b , and string + number will cause the error you see if the string cannot be implicitly converted to a number - and 'sum = 1' can't be. 'sum = '|| a给你一个字符串,然后<result string> + b和 string + number 将导致你看到的错误,如果字符串不能隐式转换为数字 - 并且'sum = 1'不能。

You can add parentheses to override the default precedence:您可以添加括号来覆盖默认优先级:

DBMS_OUTPUT.PUT_LINE('sum = '|| (a+b));

db<>fiddle db<>小提琴

You're not seeing a sqldeveloper error, you're getting an error running a sql statement in sqldeveloper, that is something very different.您没有看到 sqldeveloper 错误,您在 sqldeveloper 中运行 sql 语句时遇到错误,这是非常不同的。 For simplicity I'm declaring the variables in my example instead of using bind variables like you do:为简单起见,我在示例中声明变量,而不是像您那样使用绑定变量:

DECLARE
    a NUMBER;
    b NUMBER;
BEGIN
    a := 1;  
    b := 2;    
    DBMS_OUTPUT.PUT_LINE('sum = '||a+b);
END;

Returns ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at line 7 The important information is the "line 7".返回 ORA-06502:PL/SQL:数字或值错误:字符到数字的转换错误 ORA-06512:在第 7 行 重要信息是“第 7 行”。 Something is failing in that dbms_output statement and the cause is the operator precedence.该 dbms_output 语句出现故障,原因是运算符优先级。 Best is to tell the database engine that you want the number concatenated to the string, the error indicates that there is an implicit conversion happening.最好的方法是告诉数据库引擎您希望将数字连接到字符串,错误表明发生了隐式转换。

The following works just fine:以下工作正常:

DECLARE
    a NUMBER;
    b NUMBER;
BEGIN
    a := 1;  
    b := 2;    
    DBMS_OUTPUT.PUT_LINE('sum = '|| TO_NUMBER(a+b));
END;

One additional remark.补充一句。 This syntax这种语法

    a :=: a;

works but is very confusing.有效,但非常混乱。 I would use a more descriptive name for your bind variable and separate the assignment operator := from the bind variable.我将为您的绑定变量使用更具描述性的名称,并将赋值运算符:=与绑定变量分开。 Writing it like this is a lot more readable:像这样写它更具可读性:

   a := :variablea;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL错误:ORA-06502:PL / SQL:数字或值错误 - SQL Error: ORA-06502: PL/SQL: numeric or value error 奇怪的ORA-06502:PL / SQL:数字或值错误 - Strange ORA-06502: PL/SQL: numeric or value error ORA-06502: PL/SQL: 将 TO_BINARY_DOUBLE 与 JDBC 一起使用时出现数值或值错误 - ORA-06502: PL/SQL: numeric or value error when using TO_BINARY_DOUBLE with JDBC ORA-06502: PL/SQL: 数字或值错误: 字符串缓冲区太小: 构建时 model - ORA-06502: PL/SQL: numeric or value error: character string buffer too small: When building a model ORA-06502: PL/SQL: ora_sql_txt 出现数字或值错误 - ORA-06502: PL/SQL: numeric or value error by ora_sql_txt 获取ORA-06502:PL / SQL:数字或值错误:SQL触发器中的字符到数字转换错误 - Getting ORA-06502: PL/SQL: numeric or value error: character to number conversion error in SQL trigger ORA-06502:PL / SQL:数字或值错误:Oracle SQL查询中的字符串缓冲区太小 - ORA-06502: PL/SQL: numeric or value error: character string buffer too small in Oracle sql query ORA-06502 PL / SQL:数字或值错误:字符到数字的转换错误; - ORA-06502 PL/SQL: numeric or value error: character to number conversion error; Oracle数据库错误:ORA-06502:PL / SQL:数字或值错误:字符串缓冲区太小 - Oracle Database Error: ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小 - ORA-06502: PL/SQL: numeric or value error: character string buffer too small
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM