您的位置:首页 > 数据库 > Oracle

ORACLE MAX对CHAR类型的特殊处理(V10.2.0.1.0)

2014-01-16 22:20 357 查看
        ORACLE使用MAX函数处理CHAR类型【指CHAR(1)类型】时,将其返回值INTO到某变量【变量类型也为CHAR(1)类型】,运行时会报错,需要使用字符处理函数对MAX返回值进行处理(例如TO_CHAR,NVL函数)或将变量类型设置为VARCHAR2(4000)【如果将变量类型设置为VARCHAR2(3999),运行时也会报错】,才能正常运行。 下面查询公司最高职位类型为例进行分析。

        建立职位信息表(position),脚本如下:

-- Create table position_type:职位类别 position_name:职位名称
create table position
(
position_type   char(1)      default ' ' not null,
position_name   varchar2(40) default ' ' not null
)
;
-- Create indexes
create unique index idx_position on position (position_type);

        准备数据,脚本如下:

insert into position(position_type, position_name) values('0', '初级软件工程师');
insert into position(position_type, position_name) values('1', '中级软件工程师');
insert into position(position_type, position_name) values('2', '高级软件工程师');

        在Command窗口下进行测试,直接赋值的测试脚本如下:

set serveroutput on
declare
v_position_type char(1);
v_position_name varchar2(40);
begin
select max(position_type) into v_position_type from position;

if trim(v_position_type) is not null then
select position_name into v_position_name
from position
where position_type = v_position_type;
end if;

DBMS_OUTPUT.PUT_LINE('MOST POSITION IS:'||v_position_type||'-'||v_position_name);
end;
/

        以上脚本会产生如下错误:

        ORA-06502: PL/SQL: 数字或值错误 :  字符串缓冲区太小

        ORA-06512: 在 line 6

        将查询position_type最大值改成如下语句:

select nvl(max(position_type), ' ') into v_position_type from position;

        则能正常运行,运行结果如下:

        MOST POSITION IS:2-高级软件工程师

        PL/SQL procedure successfully completed
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  csdn 博客 oracle