天天看點

PL/SQL比較運算符操作執行個體講解

比較運算符用于一個表達比較到另一個。結果總是 TRUE,FALSE或NULL。下表列出了所有PL/SQL支援的比較運算符:

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;      

視窗截圖:

PL/SQL比較運算符操作執行個體講解

運作結果截圖:

PL/SQL比較運算符操作執行個體講解