在PL/SQL中,IF、THEN、ELSE、ELSIF和END IF等关键字用于执行条件逻辑:

IF condition1 THEN
 statements1
 ELSIF condition2 THEN
 statements2
 ELSE
 statements3
 END IF;


其中:
condition1和condition2是布尔表达式,其值为真或假。
statements1、statements2、statements3是PL/SQL语句。
条件逻辑的流程如下:
如果condition1为真,就执行statements1.
如果condition1为假而condition2为真,就执行statement2.
如果condition1和condition2都为假,就执行statement3.
也可以在一条IF语句中嵌入另一条IF语句,如下所示:

IF v_count > 0 THEN
	v_message := 'v_count is positive';
	IF v_area > 0 THEN
		v_message := 'v_count and v_area are positive';
	END IF
ELSIF v_count = 0 THEN
	v_message := 'v_count is zero';
ELSE
	v_message := 'v_count is negative';
END IF;

如果Smith工资小于1000 结果输出 努力工资

declare
v_sal number;
begin
  select sal into v_sal from emp where ename = 'SMITH';
  if v_sal < 1000
    then  dbms_output.put_line('Smith加油工作');
  end if;
  end;

如果Smith工资小于600结果输出 努力工作 否则输出Smith很棒

declare
v_sal number;
begin
  select sal into v_sal from emp where ename = 'SMITH';
  if v_sal < 600
    then  dbms_output.put_line('Smith加油工作');
    else  dbms_output.put_line('Smith很棒');
  end if;
  end;

多个分支 如果Smith的工资小于500 输出Smith加油工作,工资[500,2000)输出Smith可以 工资大于等于2000输出Smith很棒

declare
v_sal number;
begin
  select sal into v_sal from emp where ename = 'SMITH';
  if v_sal < 600
    then   dbms_output.put_line('Smith加油工作');
  elsif v_sal >= 600 and v_sal < 2000
    then   dbms_output.put_line('Smith很棒');
  elsif v_sal >= 2000
    then dbms_output.put_line('Smith很bang棒');
  end if;
  end;