码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-30 22:57:05      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:number

阅读目录

--简单循环

技术分享

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‘);


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

标签:number

原文地址:http://zhanglida66.blog.51cto.com/12834458/1920863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!