Oracle PL/SQL 对字符串文字进行字符串比较:意外结果
Oracle PL/SQL string comparison on string literals: unexpected result
这是一个关于 Oracle PL/SQL 中字符串比较的问题。我正在测试一些涉及空白填充和非空白填充比较的简单片段,并且我 运行 进入了一个我无法了解 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;我引用关键句:
Trailing blanks are significant within string literals, so 'abc' and 'abc ' are different. Trailing blanks in a string literal are not trimmed during PL/SQL processing, although they are trimmed if you insert that value into a table column of type CHAR. For additional information, including NCHAR string literals, see "String Literals".
有什么想法可以解释我的发现吗?这可能取决于我的 Oracle environment/session 中的特定设置吗?
这是因为 'MP' 和 'MP ' 是 CHAR
值,并且 oracle 会自动将您分配给该变量的任何值填充到指定的最大长度的空格
If you declare a CHAR variable with a length greater than 1, Oracle
Database automatically pads whatever value you assign to that variable
with spaces to the maximum length
查看与您的示例类似的 OraMag 文章
http://www.oracle.com/technetwork/issue-archive/2011/11-sep/o51plsql-453456.html
BEGIN
IF 'Logic' = 'Logic '
THEN
DBMS_OUTPUT.put_line ('Equal');
ELSE
DBMS_OUTPUT.put_line ('Not Equal');
END IF;
END;
显示 Equal
因为 "Logic" 用空格填充到 "Logic "
这是一个关于 Oracle PL/SQL 中字符串比较的问题。我正在测试一些涉及空白填充和非空白填充比较的简单片段,并且我 运行 进入了一个我无法了解 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;我引用关键句:
Trailing blanks are significant within string literals, so 'abc' and 'abc ' are different. Trailing blanks in a string literal are not trimmed during PL/SQL processing, although they are trimmed if you insert that value into a table column of type CHAR. For additional information, including NCHAR string literals, see "String Literals".
有什么想法可以解释我的发现吗?这可能取决于我的 Oracle environment/session 中的特定设置吗?
这是因为 'MP' 和 'MP ' 是 CHAR
值,并且 oracle 会自动将您分配给该变量的任何值填充到指定的最大长度的空格
If you declare a CHAR variable with a length greater than 1, Oracle Database automatically pads whatever value you assign to that variable with spaces to the maximum length
查看与您的示例类似的 OraMag 文章 http://www.oracle.com/technetwork/issue-archive/2011/11-sep/o51plsql-453456.html
BEGIN
IF 'Logic' = 'Logic '
THEN
DBMS_OUTPUT.put_line ('Equal');
ELSE
DBMS_OUTPUT.put_line ('Not Equal');
END IF;
END;
显示 Equal
因为 "Logic" 用空格填充到 "Logic "