测试 Oracle 中对 char 和 varchar2 两种类型的等值比较
2007-06-08 15:24
621 查看
我们用 Oracle 的 SQL*Plus 执行 SQL 语句。首先看看 Oracle 的版本号:
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Prod
PL/SQL Release 10.1.0.2.0 - Production
CORE 10.1.0.2.0 Production
TNS for 32-bit Windows: Version 10.1.0.2.0 - Production
NLSRTL Version 10.1.0.2.0 - Production
其次打开服务器端的输出信息:
set serveroutput on;
--
-- column type is char, but parameter type is varchar.
--
drop table foo;
create table foo(fno number(4), fname char(20));
insert into foo values(1, 'smith');
insert into foo values(2, 'john');
SQL> select * from foo where fname = 'smith';
FNO FNAME
---------- --------------------
1 smith
SQL> select * from foo where fname = cast('smith' as char(20));
FNO FNAME
---------- --------------------
1 smith
注意,varchar2 找不出结果:
SQL> select * from foo where fname = cast('smith' as varchar2(20));
no rows selected
现在,我们换成存储过程试试。
create or replace procedure foop(bname varchar2)
as
v_fno number(4);
v_fname char(20);
begin
dbms_output.put_line('in procedure');
select fno, fname into v_fno, v_fname from foo where fname = bname;
dbms_output.put_line('results: ' || v_fno || ',' || v_fname );
exception
when no_data_found then
dbms_output.put_line('employee ' || bname || ' not found');
when others then
dbms_output.put_line('the following is sqlerrm:');
dbms_output.put_line(sqlerrm);
dbms_output.put_line('the following is sqlcode:');
dbms_output.put_line(sqlcode);
end;
/
call foop('smith');
用 varchar2 做存储过程的参数也是找不出结果。
Expected result:
<result>
in procedure
employee smith not found
Call completed.
</result>
create or replace procedure foop2(bname char)
as
v_fno number(4);
v_fname char(20);
begin
dbms_output.put_line('in procedure');
select fno, fname into v_fno, v_fname from foo where fname = bname;
dbms_output.put_line('results: ' || v_fno || ',' || v_fname );
exception
when no_data_found then
dbms_output.put_line('employee ' || bname || ' not found');
when others then
dbms_output.put_line('the following is sqlerrm:');
dbms_output.put_line(sqlerrm);
dbms_output.put_line('the following is sqlcode:');
dbms_output.put_line(sqlcode);
end;
/
call foop2('smith');
用 char 作为存储过程的参数,还是能够找出结果的。
Expected results:
<result>
in procedure
results: 1,smith
Call completed.
</result>
下面我们测试一下列的类型是 varchar2 的情况。
--
-- column type is varchar, but parameter type is char.
--
drop table foo;
create table foo(fno number(4), fname varchar2(20));
insert into foo values(1, 'smith');
insert into foo values(2, 'john');
SQL> select * from foo where fname = 'smith';
FNO FNAME
---------- --------------------
1 smith
SQL> select * from foo where fname = cast('smith' as varchar2(20));
FNO FNAME
---------- --------------------
1 smith
注意,用 char 是找不出结果的:
SQL> select * from foo where fname = cast('smith' as char(20));
no rows selected
create or replace procedure foop(bname char)
as
v_fno number(4);
v_fname varchar2(20);
begin
dbms_output.put_line('in procedure');
select fno, fname into v_fno, v_fname from foo where fname = bname;
dbms_output.put_line('results: ' || v_fno || ',' || v_fname );
exception
when no_data_found then
dbms_output.put_line('employee ' || bname || ' not found');
when others then
dbms_output.put_line('the following is sqlerrm:');
dbms_output.put_line(sqlerrm);
dbms_output.put_line('the following is sqlcode:');
dbms_output.put_line(sqlcode);
end;
/
call foop('smith');
存储过程的参数为 char 时,居然也可以找出该记录。
Expected result:
<result>
in procedure
results: 1,smith
Call completed.
</result>
create or replace procedure foop2(bname varchar2)
as
v_fno number(4);
v_fname varchar2(20);
begin
dbms_output.put_line('in procedure');
select fno, fname into v_fno, v_fname from foo where fname = bname;
dbms_output.put_line('results: ' || v_fno || ',' || v_fname );
exception
when no_data_found then
dbms_output.put_line('employee ' || bname || ' not found');
when others then
dbms_output.put_line('the following is sqlerrm:');
dbms_output.put_line(sqlerrm);
dbms_output.put_line('the following is sqlcode:');
dbms_output.put_line(sqlcode);
end;
/
call foop2('smith');
存储过程参数为 varchar2 时当然更应该能找出该记录了。
Expected results:
<result>
in procedure
results: 1,smith
Call completed.
</result>
1. http://www.psoug.org/reference/dbms_output.html
相关文章推荐
- 关于 char(10) 与 varchar(20) 两个类型的字段的比较 (oracle 数据库)
- oracle 数据类型varchar2长度测试
- 在Oracle中CHAR,NCHAR,VARCHAR,VARCHAR2,NVARCHAR2这五种类型的区别
- Oracle里Varchar的参数(byte和char)的区别测试
- Oracle CHAR,VARCHAR,VARCHAR2,nvarchar类型的区别与使用(转载)
- Oracle字段类型char、varchar2、nvarchar2的区别与使用
- char和varchar类型的字段比较注意
- 在Oracle中CHAR,NCHAR,VARCHAR,VARCHAR2,NVARCHAR2这5种类型的区别
- Oracle CHAR,VARCHAR,VARCHAR2类型的区别与使用
- mysql列类型char,varchar,text,tinytext,mediumtext,longtext的比较与选择
- ORACLE CHAR,VARCHAR,VARCHAR2,NVARCHAR类型的区别与使用
- Oracle里Varchar的参数(byte和char)的区别测试
- oracle :测试char与varchar存储长度
- oracle中n/varchar、n/varchar2、n/char几种类型的区别与使用
- ORACLE字符类型详解----char、nchar、varchar、varchar2、nvarchar2
- char、varchar与nvarchar三种数据类型比较
- ORACLE字符类型详解----char、nchar、varchar、varchar2、nvarchar2
- Oracle CHAR,VARCHAR,VARCHAR2,nvarchar类型的区别与使用
- Oracle CHAR,VARCHAR,VARCHAR2类型的区别与使用
- MySQL数据类型char、varchar、text的比较