比较运算符用于一个表达比较到另一个。结果总是 TRUE,FALSE或NULL。下表列出了所有PL/SQL支持的比较运算符:
操作实例源码:
-- Created on 2018/3/22 by E.WANG
declare
--声明以字符常量
const_char constant char(15):='hello,world!';
--声明一个字符串变量
var_char varchar(15);
--声明两个数字变量
var_int int;
var_num number(4,2);
begin
/*
LIKE操作一个字符,
字符串或CLOB值进行比较匹配模式则返回TRUE,
如果不匹配模式则FALSE
*/
var_char:='hello';
--字符串是否包含lo
if var_char like '%%lo' then
dbms_output.put_line(var_char);
end if;
--%表示匹配任何字符串,_表示任意字符
--匹配字符串中带有l_o格式的字符
if const_char like '%l_o%' then
dbms_output.put_line(const_char);
end if;
/*
BETWEEN 运算符测试一个值是否位于规定的范围内.
x BETWEEN a AND b 意思就是 x >= a and x <= b.
*/
var_int:=20;
--20是否在[10,20]范围内
if var_int between 10 and 20 then
dbms_output.put_line(var_int || ' between 10 and 20');
end if;
--20不在在[21,25]范围内
if var_int not between 21 and 25 then
dbms_output.put_line(var_int || 'not between 21 and 25');
end if;
/*
IN运算符的测试设置成员.
x IN (set) 意味着x等于集合中的某一个成员
*/
var_char:='hello';
--'hello是否包含在集合中
if var_char in ('hello','Hello','bad boy') then
dbms_output.put_line(var_char || ' in (''hello'',''Hello'',''bad boy'') ');
end if;
--'hello’不包含在集合中
if var_char not in ('Hello','bad boy') then
dbms_output.put_line(var_char || ' not in (''Hello'',''bad boy'') ');
end if;
/*
IS NULL运算符返回布尔值true,
如果它的操作数是NULL或FALSE(如果它不为NULL)。
包括NULL值的比较总能取得NULL
*/
--如果var_char为null
if var_char is null then
dbms_output.put_line('var_char is null');
else
dbms_output.put_line('var_char is not null.it is ' || var_char);
end if;
--如果var_char不为为null
if var_char is not null then
dbms_output.put_line('var_char is not null');
end if;
end;
窗口截图:
运行结果截图: