oracle 01722. 00000 -“无效号码”:甲骨文

pdkcd3nj  于 2023-01-16  发布在  Oracle
关注(0)|答案(1)|浏览(153)

我有一个table1

t1id       NUMBER(10,0)
channel_id NUMBER(10,0)

另一个table2具有列

t1id        NUMBER(10,0)
channel2_id NVARCHAR2(100 CHAR)
cl2_id      NVARCHAR2(100 CHAR)

调用查询后

SELECT t1.id, t2.cl2_id
  FROM table1  t1 
  LEFT JOIN table2 t2 
    ON t1.channel_id  = c.channel2_id;

联接查询时收到以下错误。?是否由于两列的数据类型?如何解决此问题

01722. 00000 -  "invalid number"
*Cause:    The specified number was invalid.
*Action:   Specify a valid number.
6rvt4ljy

6rvt4ljy1#

如果你有一个数据类型不匹配,我们将(默默地)尝试纠正不匹配,例如

SQL> create table t ( num_col_stored_as_string  varchar2(10));

Table created.

SQL>
SQL> insert into t values ('123');

1 row created.

SQL> insert into t values ('456');

1 row created.

SQL> insert into t values ('789');

1 row created.

SQL>
SQL> explain plan for
  2  select * from t
  3  where num_col_stored_as_string = 456;

Explained.

SQL>
SQL> select * from dbms_xplan.display();

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     7 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |     7 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(TO_NUMBER("NUM_COL_STORED_AS_STRING")=456)

注意,我们在过滤器中添加了一个TO_NUMBER,以确保该列与输入值(456)匹配。
然后,这会导致问题的原因变得显而易见,因为:

SQL> insert into t values ('NOT A NUM');

1 row created.

SQL> select * from t
  2  where num_col_stored_as_string = 456;
ERROR:
ORA-01722: invalid number

正如其他人在评论中建议的那样,看看

  • 对齐数据类型
  • 使用TO_CHAR
  • 使用验证转换

但理想情况下,数据类型对齐是可行的方法

相关问题