ORACLE 1722 错误案例分析

-- ORACLE 1722 错误案例分析

-- 1. 创建表
drop table area;
create table area (id number(10, 0) primary key, areacode varchar2(20));

-- 2. 插入正常数据
insert into area values (1, '10');
insert into area values (2, '20');
insert into area values (3, '30');
insert into area values (5, '40');
insert into area values (6, '50');
commit;

select * from area;
select * from area where areacode = 20;

-- 3.插入异常数据
insert into area values (7, '20
');
commit;

-- 4.报错1722 INVAID NUMBER 无效数字
select * from area where areacode = 20;

-- 5.问题定位
-- 查看是否有换行符 10 回车符 13 制表符 9
select id , areacode from area where length(replace(replace(areacode, chr(10), ''), chr(13), '')) < length(areacode);


-- 6.处理
delete from area where id = 7;
commit;

-- 7.问题引入分析
-- 插入数据时意外的插入了换行符.查询时SQL不符合规范.
-- 此字段本来用来存储字符数据,但ORACLE隐式转换字符为数字去比较.
-- 规范写法:
select * from area where areacode = '20';





你可能感兴趣的:(oracle,sql)