본문 바로가기

Database/PLSQL

006 - [Oracle PL/SQL] IF Statement

<샘플코드에서 사용한 데이터는 HR 스키마이고, 오라클 설치시 생성할 수 있는 기본 스키마 입니다>

 

 

# simple if statement with else

DECLARE
v_no NUMBER := &v;
BEGIN
   IF v_no >=10 THEN
   dbms_output.put_line('the number you enterd is >=10');
   ELSE
   dbms_output.put_line('number is is less than 10');
   END IF;
   
END;

 

 

 

# IF ELSIF ELSE

DECLARE
v_grade NUMBER := &v;
BEGIN
    IF v_grade between 90 and 100 THEN
    dbms_output.put_line('the grade is A');
    
    ELSIF v_grade between 80 and 89 THEN
    dbms_output.put_line('the grade is B');
    
    ELSIF v_grade between 70 and 79 THEN
    dbms_output.put_line('the grade is C');
    
    ELSIF v_grade between 60 and 69 THEN
    dbms_output.put_line('the grade is D');
    
    ELSE
    dbms_output.put_line('the grade is F');
    
    END IF;
    
END;

 

 

 

DECLARE
v_grade NUMBER := &v;
BEGIN
  IF v_grade BETWEEN 0 AND 100 THEN
  
     IF v_grade between 90 and 100 THEN
     dbms_output.put_line('the grade is A');
     
     ELSIF v_grade between 80 and 89 THEN
     dbms_output.put_line('the grade is B');
     
     ELSIF v_grade between 70 and 79 THEN
     dbms_output.put_line('the grade is C');
     
     ELSIF v_grade between 60 and 69 THEN
     dbms_output.put_line('the grade is D');
     
     ELSE
     dbms_output.put_line('the grade is F');
     
     END IF;
     
  ELSE
  dbms_output.put_line('the grade should be number between 0 and 100');

  END IF;

END;

 

 

 

# null 인 경우는 nvl을 이용해서 오류를 필해야 한다.

declare
x number:=5;
y number;
begin

  if nvl(x,0)<>nvl(y,0) then
  dbms_output.put_line('welcome');
  else
  dbms_output.put_line('Operator with null value always =null');
  end if;

end;

 

 

 

# null 인 경우를 정확히 판단하고 싶다면 "is null" 을 이용해서 비교한다.

declare
x number;
y number;
begin

  if x is null and y is null then
  dbms_output.put_line('welcome');
  else
  dbms_output.put_line('Operator with null value always =null');
  end if;

end;