【发布时间】:2015-12-14 00:12:03
【问题描述】:
这是一个关于 Oracle PL/SQL 中字符串比较的问题。我正在测试一些涉及空白填充和非空白填充比较的简单 sn-ps,我遇到了一个特定情况,我无法了解 PL/SQL 引擎在做什么。
这是我的代码(我在 Oracle 12c 上运行此脚本)
set serveroutput on;
declare
l_char1 char(35) := 'Hello';
l_char2 char(30) := 'Hello ';
l_varchar21 varchar2(35) := 'Hello';
l_varchar22 varchar2(35) := 'Hello ';
begin
/* When a comparison is made between variables of type CHAR,
a blank-padding comparison takes place. */
if ( l_char1 = l_char2) then
dbms_output.put_line('First comparison is TRUE');
else
dbms_output.put_line('First comparison is FALSE');
end if;
/* When at least one of the involved variables is a VARCHAR2,
then a non-blank-padding comparison takes place. None of the
variables involved in the comparison is then modified. */
if ( l_char1 = l_varchar21) then
dbms_output.put_line('Second comparison is TRUE');
else
dbms_output.put_line('Second comparison is FALSE');
end if;
/* yet another non-blank-padding comparison */
if ( l_varchar21 = l_varchar22) then
dbms_output.put_line('Third comparison is TRUE');
else
dbms_output.put_line('Third comparison is FALSE');
end if;
/* Strange behaviour: I supposed both string literals to be
treated as VARCHAR2, so I expected to get FALSE. */
if ( 'MP' = 'MP ') then
dbms_output.put_line('Fourth comparison is TRUE');
else
dbms_output.put_line('Fourth comparison is FALSE');
end if;
end;
这是输出
Procedura PL/SQL completata correttamente.
First comparison is TRUE
Second comparison is FALSE
Third comparison is FALSE
Fourth comparison is TRUE ** I expected FALSE **
关于字符串文字比较,我希望得到Oracle documentation 上报告的内容;我引用关键句:
尾随空格在字符串文字中很重要,因此 'abc' 和 'abc ' 是不同的。在 PL/SQL 处理过程中,字符串文字中的尾随空格不会被修剪,但如果将该值插入到 CHAR 类型的表列中,它们会被修剪。有关其他信息,包括 NCHAR 字符串文字,请参阅“字符串文字”。
有什么想法可以解释我的发现吗?这是否取决于我的 Oracle 环境/会话中的特定设置?
【问题讨论】:
标签: oracle plsql string-comparison