PL/SQL 运算符(Operator)

-- Start

赋值运算符(Assignment Operator)

DECLARE
  X INT := 1; -- 赋值运算符 :=
 
BEGIN
    DBMS_OUTPUT.put_line(X);
END;
/

算术运算符(Arithmetic Operator)

DECLARE
  X INT := 2;
  Y INT := 3;
  Z INT;
 
BEGIN
  Z := X + Y; -- 加(Addition Operator)
  DBMS_OUTPUT.put_line('X + Y = ' || Z);
  
  Z := X - Y; -- 减(Subtraction Operator)
  DBMS_OUTPUT.put_line('X - Y = ' || Z);
  
  Z := X * Y; -- 乘(Multiplication Operator)
  DBMS_OUTPUT.put_line('X * Y = ' || Z);
  
  Z := X / Y; -- 除(Division Operator)
  DBMS_OUTPUT.put_line('X / Y = ' || Z);
  
  Z := X ** Y; -- 次方
  DBMS_OUTPUT.put_line('X ** Y = ' || Z);
  
END;
/


比较运算符(Comparison Operator)

DECLARE
  X INT := 2;
  Y INT := 3;
  Z INT;
  NAME CHAR(10) := 'Shang Bo';
 
BEGIN
  -- 比较 NULL
  IF Z IS NULL THEN
    DBMS_OUTPUT.put_line('Z IS NULL');
  END IF;
  
  -- 比较 NOT NULL
  IF X IS NOT NULL THEN
    DBMS_OUTPUT.put_line('Z IS NOT NULL');
  END IF;

  -- 比较相等(Equal)
  IF X = Y THEN
    DBMS_OUTPUT.put_line('X = Y');
  END IF;
  
  -- 比较不相等(Not Equal)
  IF X <> Y THEN
    DBMS_OUTPUT.put_line('X <> Y');
  END IF;
  
  -- 比较不相等(Not Equal)
  IF X != Y THEN
    DBMS_OUTPUT.put_line('X != Y');
  END IF;
  
  -- 比较不相等(Not Equal)
  IF X~=Y THEN
    DBMS_OUTPUT.put_line('X ~= Y');
  END IF; 
  
  -- 比较不相等(Not Equal)
  IF X ^= Y THEN
    DBMS_OUTPUT.put_line('X ^= Y');
  END IF; 
  
  -- 比较小于(Less Than)
  IF X < Y THEN
    DBMS_OUTPUT.put_line('X < Y');
  END IF; 
  
  -- 比较小于等于(Less Than or Equal)
  IF X <= Y THEN
    DBMS_OUTPUT.put_line('X <= Y');
  END IF; 

  -- 比较大于(Greater Than)
  IF X > Y THEN
    DBMS_OUTPUT.put_line('X > Y');
  END IF; 
  
  -- 比较大于等于(Greater Than or Equa)
  IF X >= Y THEN
    DBMS_OUTPUT.put_line('X >= Y');
  END IF;
  
  -- BETWEEN (Z>=X and Z<=Y)
  IF Z BETWEEN X AND Y THEN
    DBMS_OUTPUT.put_line('Z>=X and Z<=Y');
  END IF;
  
  -- IN
  IF Z IN (X, Y) THEN
    DBMS_OUTPUT.put_line('Z IN (X, Y)');
  END IF;
  
  -- LIKE(_ 匹配任一一个字符,% 匹配任意多个字符)
  IF NAME LIKE 'Sh_ng%o' THEN
    DBMS_OUTPUT.put_line('匹配成功');
  END IF;
END;
/

字符串连接(Concatenation Operator)

DECLARE
  FIRST_NAME  VARCHAR2(10) := 'Bo';
  LAST_NAME   VARCHAR2(10) := 'Shang';

BEGIN
  -- 字符串连接
  DBMS_OUTPUT.PUT_LINE(LAST_NAME || FIRST_NAME);
END;
/

逻辑操作符(Logical Operator)

DECLARE  
  X INT := 2;  
  Y INT := 3;  
   
BEGIN  
  -- 与
  IF X = 2 AND Y = 3 THEN  
    DBMS_OUTPUT.put_line('X = 2 AND Y = 3');  
  END IF;  
    
  -- 或 
  IF X = 2 OR Y = 3 THEN  
    DBMS_OUTPUT.put_line('X = 2 OR Y = 3');  
  END IF; 
  
  -- 非
  IF NOT X = 3 THEN  
    DBMS_OUTPUT.put_line('X != 2');  
  END IF; 
END;
/  


--更多参见:Oracle PL/SQL 精萃

-- 声明:转载请注明出处

-- Last Edited on 2015-01-25

-- Created by ShangBo on 2015-01-25

-- End


你可能感兴趣的:(oracle,sql,plsql)