<![CDATA[ORA-01722 invalid number]]>

本文详细解析了Oracle数据库中ORA-01722错误的原因及解决方法,包括无效数字错误的产生场景、示例及如何在INSERT、SELECT等操作中排除该错误。

ORA-01722

From Oracle FAQ

Jump to: navigation, search

ORA-01722: invalid number

Contents

[hide]
[ edit]

What causes this error?

An ORA-01722 error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number. Valid numbers contain the digits '0' thru '9', with possibly one decimal point, a sign (+ or -) at the beginning or end of the string, or an 'E' or 'e' (if it is a floating point number in scientific notation). All other characters are forbidden.

There are numerous situations where this conversion may occur. A numeric column may be the object of an INSERT or an UPDATE statement. Or, a numeric column may appear as part of a WHERE clause. It is even possible for this error to appear when there are no numeric columns appearing explicitly in the statement!

[ edit]

Examples

Here are some examples:

SQL> select to_number('3434,3333.000') from dual;
ERROR:
ORA-01722: invalid number
no rows selected

The above statement throws the error message, because it has found a character, in this case, a comma and the default format for TO_NUMBER does not contain a comma.

The same error can occur when you use arithmetic functions on strings:

SQL> select 'abc' - 124 from dual;
ERROR:
ORA-01722: invalid number
no rows selected

The error can occur when you add dates with string values:

SQL> select '01-JUN-01' - 'abc' from dual;
ERROR:
ORA-01722: invalid number
no rows selected
[ edit]

How to fix it

The fix depends upon the exact expression which caused the problem. The following guide lists the possible SQL expressions which can give this error, with their most likely cause. When addressing this error, keep in mind that it can indicate a simple keystroke problem with the query, or a deeper problem with the query logic, or even the presence of bad data in the database itself.

[ edit]

When doing an INSERT INTO ... VALUES (...)

One of the data items you are trying to insert is an invalid number. Locate and correct it.

If all of the numbers appear to be valid, then you probably have your columns out of order, and an item in the VALUES clause is being inserted into a NUMBER column instead of the expected VARCHAR2 column. This can happen when a table has columns added or removed.

You are doing an INSERT or UPDATE, with a sub query supplying the values. Obviously, the preceding considerations apply here as well. What makes this more complicated is that the offending character string is hidden as a row in a table. The fix is to identify the row (or rows) which has the non-numeric string, and either change the data (if it is in error) or add something to the sub query to avoid selecting it. The problem is in identifying the exact row.

Assuming that the errant datum is an alphabetic character, one can use the following query:

        SELECT ...  WHERE UPPER(col) != LOWER(col)

where col is the column with the bad data.

[ edit]

When doing a SELECT, rather than an INSERT or UPDATE

In this case, there is probably an implicit conversion happening between some predicate in the WHERE clause. Check for a numeric column being compared to a character column.

If you are using the to_number function, make sure the format mask fits all possible character strings in the table.

If you know that a column contains both valid numbers and character strings, make sure that all rows which do not contain valid numbers are being excluded in the WHERE clause.

[ edit]

Other Rare Situations

To expand on the previous comment, if you have a column in a table which contains both valid numbers and character strings, it is just barely possible to get an ORA-01722 even if no character strings are being returned by your query. Example: two tables must be joined. In table A, the column is VARCHAR2, and in table B it is NUMBER. Table A also has non-numeric data in that column in some rows, and has a type column to make it obvious which rows are which. It is possible for the optimizer to choose an access plan in which the join is attempted before the filtering, which will cause the ORA-01772. The fix is to add a hint which changes the plan enough to bypass the rows causing the error.

Doing an explicit conversion can sometimes make things worse. For example, '+17', '-17', & ' 17' all convert successfully implicitly. The last one will raise the error if the 'S99' mask is used in the to_number function.

A field containing only spaces will raise this error. One fix is to replace the spaces with nulls or zeroes.

If you are querying a view rather than a table, any of the above could apply, and be hidden from sight. The fix is to add a predicate to the WHERE clause which excludes the troublesome rows.

<think>我们正在讨论XML中的注释符号<!--XXX-->。根据用户提供的引用和问题,我将解释XML注释的含义、用法以及相关注意事项。 引用[1]中提到:注释的语法是<!--在其中写明注释内容-->,并且XML的基本规则包括每个元素都必须有关闭标签、元素命名规则、大小写敏感等。 引用[2]中给出了一个XML示例,其中包含了注释:<?xml version=&#39;1.0&#39; ?>,并在<users>元素中包含了一些数据。 因此,针对用户的问题“xml格式中注释符号<!--XXX-->的含义”,回答如下: XML注释用于在XML文档中添加说明性文字,这些内容不会被XML解析器解析或作为文档内容处理。注释的基本语法是: <!-- 这里是注释内容 --> 具体说明: 1. 注释以`<!--`开头,以`-->`结尾,中间的内容就是注释文本。 2. 注释可以出现在XML文档中除标记内部(例如,在开始标签的尖括号内)以外的任何位置。 3. 注释不能嵌套。也就是说,在一个注释内部不能再出现另一个注释。例如:<!-- 外层注释 <!-- 内层注释 --> 外层注释结束 --> 这样的写法是错误的,因为第一个`-->`就会结束注释。 4. 注释中不要出现连续的两个连字符(--),除了作为注释的结束标记的一部分。因为XML规范中规定,注释内容中不能包含连续的两个连字符(--)。例如:<!--这里是注释--中间不能有两个连字符--> 是不允许的,因为注释内容中包含了“--”。 5. 注释的主要目的是给开发人员或读者提供说明,不影响文档的数据结构和解析结果。 示例: 以下XML文档中使用了注释: <?xml version="1.0" encoding="UTF-8"?> <!-- 这是一个用户列表 --> <users> <user id="1"> <name>张三</name> <age>20</age> </user> <!-- 这是第二个用户 --> <user id="2"> <name>李四</name> <age>22</age> </user> </users> 在上面的例子中,第一行是XML声明,紧接着是一个注释,说明这是一个用户列表。在第二个<user>元素之前也有一个注释。 注意事项: - 注释不能出现在XML声明之前。XML声明必须是文档的第一行(如果存在声明的话)。 - 在XML中,注释并不是一个元素,因此不需要关闭标签(它有自己的结束标记-->)。 - 注释内容中不能包含“--”字符串,因为解析器会将其误认为是注释结束标记的一部分。 引用说明: 根据引用[1]中关于XML注释的语法规则以及引用[2]中的XML示例,我们可以确认注释的使用方法[^1][^2]。 相关问题: 1. XML注释中可以使用哪些字符?有哪些字符是禁止使用的? 2. 如何正确地在XML文档中插入多行注释? 3. XML解析器在处理注释时有哪些行为?注释会被保留吗? </think>### XML注释符号 `<!--XXX-->` 的含义详解 在XML格式中,符号 `<!--XXX-->` 表示**注释(Comment)**,用于在XML文档中添加说明性文字。这些文字不会被XML解析器处理,也不会影响文档的数据结构和解析结果。 #### 核心特性 1. **语法结构** - 起始标记:`<!--` - 注释内容:`XXX`(任意文本) - 结束标记:`-->` 2. **功能作用** - 代码说明:解释XML元素或属性的用途(如 `<!-- 用户信息开始 -->`) - 临时屏蔽:禁用部分XML代码而不删除(调试时常用) - 文档标注:添加作者、版本、更新时间等元信息 3. **使用规则** - **位置灵活**:可出现在XML声明后、元素之间或文档末尾(不能在元素标签内部) ```xml <?xml version="1.0"?> <!-- 用户数据库 --> <users> <user> <!-- 禁止在此处写注释 --> <name>张三</name> </user> </users> <!-- 最后更新:2023-10-01 --> ``` - **禁止嵌套**:注释内部不能包含另一个注释 ```xml <!-- 外层注释 <!-- 内层注释 --> ✘ 错误写法 --> ``` - **禁用连续横线**:内容中避免使用 `--`(可能被误判为结束标记) ```xml <!-- 数值范围:10--20 --> ✘ 错误 <!-- 数值范围:10至20 --> ✓ 正确 ``` #### 与CDATA的区别 | 特性 | 注释 `<!-- -->` | CDATA `<![CDATA[]]>` | |--------------|----------------------------------|-------------------------------| | **用途** | 添加说明文本 | 包裹特殊字符(如 `<`, `&`) | | **解析行为** | 完全忽略 | 内容按原样输出 | | **嵌套能力** | 不可嵌套 | 可嵌套注释 | | **示例** | `<!-- 临时停用 -->` | `<![CDATA[ if(a<b) {...} ]]>` [^5] | #### 实际应用示例 ```xml <?xml version="1.0" encoding="UTF-8"?> <!-- 用户配置文件 - 版本1.2 [^2] --> <users> <!-- 管理员账户 --> <user id="101" role="admin"> <name>Admin</name> <status>active</status> </user> <!-- 以下用户待验证 注释可跨多行书写 --> <!-- <user id="102"><name>Test</name></user> --> </users> ``` #### 注释的处理原则 1. **解析器行为**:XML解析器会完全跳过注释内容,不将其纳入文档对象模型(DOM)[^1] 2. **设计建议**: - 关键数据结构避免依赖注释说明 - 正式交付的XML可删除非必要注释减小文件体积 - 团队协作时建议规范注释格式(如Javadoc风格)[^4] > 引用说明:XML注释语法规则及示例参考自用户提供的引用内容[^1][^2][^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值