我有一个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.
如果您有数据类型不匹配,我们将(默默地)尝试纠正该不匹配,例如
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
- 使用VALIDATE_CONVERSION
但理想情况下,数据类型对齐是正确的