PL/SQL關係運算子

2019-10-16 22:54:20

關係運算子比較兩個表示式或值,並返回一個布林結果。 下表顯示了PL/SQL支援的所有關係運算子。假設變數A=10,變數B=20,則 -

關係運算子範例

運算子 範例 範例
= 檢查兩個運算元的值是否相等,如果是,則條件成立。 (A = B)為假
!=,<>~= 檢查兩個運算元的值是否相等,如果兩個值不相等則條件成為真。 (A != B)為真
> 檢查左運算元的值是否大於右運算元的值,如果是,則條件成為真。 (A > B) 為假
< 檢查左運算元的值是否小於右運算元的值,如果是,則條件成為真。 (A < B) 條件為真。
>= 檢查左運算元的值是否大於或等於右運算元的值,如果是,則條件成為真。 (A >= B) 為假
<= 檢查左運算元的值是否小於或等於右運算元的值,如果是,則條件成為真。 (A <= B) 為真

範例

DECLARE 
   a number (2) := 21; 
   b number (2) := 10; 
BEGIN 
   IF (a = b) then 
      dbms_output.put_line('Line 1 - a is equal to b'); 
   ELSE 
      dbms_output.put_line('Line 1 - a is not equal to b'); 
   END IF;  
   IF (a < b) then 
      dbms_output.put_line('Line 2 - a is less than b'); 
   ELSE 
      dbms_output.put_line('Line 2 - a is not less than b'); 
   END IF; 

   IF ( a > b ) THEN 
      dbms_output.put_line('Line 3 - a is greater than b'); 
   ELSE 
      dbms_output.put_line('Line 3 - a is not greater than b'); 
   END IF;  
   -- Lets change value of a and b 
   a := 5; 
   b := 20; 
   IF ( a <= b ) THEN 
      dbms_output.put_line('Line 4 - a is either equal or less than b'); 
   END IF; 
   IF ( b >= a ) THEN 
      dbms_output.put_line('Line 5 - b is either equal or greater than a'); 
   END IF;
   IF ( a <> b ) THEN 
      dbms_output.put_line('Line 6 - a is not equal to b'); 
   ELSE 
      dbms_output.put_line('Line 6 - a is equal to b'); 
   END IF;  
END; 
/

當上述程式碼在SQL提示下執行後,會產生以下結果 -

Line 1 - a is not equal to b 
Line 2 - a is not less than b 
Line 3 - a is greater than b 
Line 4 - a is either equal or less than b 
Line 5 - b is either equal or greater than a 
Line 6 - a is not equal to b  

PL/SQL procedure successfully completed