手记

流程控制语句【循环、条件】

--简单循环

declare                    --声明  mynum number(3) := 0;    begin  loop    mynum := mynum + 1;    dbms_output.put_line(mynum);    exit when mynum = 200; --退出循环条件  end loop;end;

 

--while循环

declare  i number := 1;begin  while i <= 100 loop  --执行循环条件           dbms_output.put_line(i);    i := i + 1;  end loop;end;

 

--for循环

begin  for i in 1 .. 10 loop    dbms_output.put_line(i);  end loop;end;

 

--goto循环

declare   i number := 1;begin   loop      dbms_output.put_line('i:'||i);     i := i + 1;   if i > 100 then       goto biaoji;   end if;   end loop;   <<biaoji>>     dbms_output.put_line('i:'||i);end;

 

--if条件语句

declare  v_value1 number := 20;begin  if v_value1 < 10 then    dbms_output.put_line('v_value1小于10');  elsif v_value1 < 20 then  --注意elsif 不是 else if    dbms_output.put_line('v_value1小于20');  else    dbms_output.put_line('v_value1大于或等于20');  end if;end;

 

--case when

declare  v_value1 varchar2(2) := 'A';  v_value2 varchar2(100);begin  v_value2 := case v_value1                when 'A' then                 '优秀'                when 'B' then                 '一般'                else                 '差劲'              end;  dbms_output.put_line(v_value2);end;

declare  v_value1 varchar2(2) := 'A';  v_value2 varchar2(100);begin  v_value2 := case                when v_value1 = 'A' then                 '优秀'                when v_value1 = 'B' then                 '一般'                else                 '差劲'              end;  dbms_output.put_line(v_value2);end;

以上两种方式都是可以的,这里注意了,case when语句 始终都都返回值(这点和if条件语句不一样)。

也可以用于查询结果和查询条件

select case t.c_type         when 'B' then          '一般'         when 'A' then          '优秀'         else          '差劲'       end,       case         when t.c_case_no = '4090003002115000012' then          '4090003002015000012'       end  from tbl_claim t where t.c_case_no = case         when t.c_case_no = '4090003002015000012' then          '4090003002015000012'       end;

 

--decode

decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)
select decode('A', 'B', '一般', 'A', '优秀', '差劲') from dual;

可用于查询结果和查询条件。(但是不能像case when一样赋值)

select decode(t.type, 'B', '一般', '差劲')  from mytable t where t.c_no = decode(t.c_no, '111', '111', '222');

 

0人推荐
随时随地看视频
慕课网APP