Oracle——條件控制語句


PL/SQL有3種類型的條件控制語句:

   ▪IF語句

   ▪ELSIF語句

   ▪CASE語句

 

1.IF語句

    ▪if-then語句

           語法格式:

if condition then
     statement;
end if;

 

    ▪if-then-else語句

           語法格式:

if condition then
     statement1;
else
     statement2;
end if;

 

例:輸入一個數,判斷它的奇偶性

  

SQL> declare
  2   num number:=3;
  3  begin
  4   if mod(num,2)<>0 then
  5    dbms_output.put_line(num||'是奇數');
  6   else
  7    dbms_output.put_line(num||'是偶數');
  8   end if;
  9  end;
 10  /

注:mod(num1,num2)取num1除於num2的余數

 

2.ELSIF語句

  語法格式:

if condition1 then
    statement1;
elsif condition2 then
    statement2;
end;

3.CASE語句

    ▪CASE語句:

        語法格式:

case selector
     when expression1 then result1;
     when expression2 then result2;
     ...
     when expressionN then resultN;
     else resultN+1;
end case; 

   ▪搜索式表達式:

     語法格式:

case
     when condition1 then statement1;
     when condition2 then statement2;
     ...
     when conditionN then statementN;
     else resultN+1;
end case; 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM