2、if、case when和循环语句

来源:互联网 发布:考研政治知乎 编辑:程序博客网 时间:2024/05/16 20:29

一、IF条件语句

     set serverout on;  (//此句作用:在控制台输出结果)
    declare aj_count number;
    begin
       select count(*) into aj_count from tb_st_ajjbxx;
       if aj_count> 10000 then
       dbms_output.put_line('有' || aj_count || '个');
     else
        dbms_output.put_line('有' || aj_count || '个');
     end if;
    end;

二、CASE WHEN

    set serverout on;
    declare aj_count number;
    begin
      select count(*) into aj_count from tb_st_ajjbxx;
      case when aj_count <10000 then dbms_output.put_line('有aa' || aj_count || '个');
               when aj_count >=10000 then dbms_output.put_line('有bb' || aj_count || '个');
       end case;
     end;

三、循环语句

(1)无条件循环 loop

   set serverout on;
declare g_id number:=1;
             losal number;
             hisal number;
begin
   loop
      if(g_id>3) then exit;
      end if;  (if then exit end if;   此处为loop循环必须的)
      
      select   username,password into losal,hisal from t_user where id=g_id; 
      dbms_output.put_line(g_id || '数值为' || losal || 'aaaa' || hisal);
      g_id := g_id+1;
      
    end loop;
end;


(2)while 循环

set serverout on;
declare g_id number:=1;
        losal number;
        hisal number;
begin
    while g_id<3 loop
      select username,password into losal,hisal from t_user where id=g_id; 
      dbms_output.put_line(g_id || '数值为' || losal || 'bbb' || hisal);
      g_id := g_id+1;
    end loop;
end;


(3)FOR 循环

set serverout on;
declare losal number;
        hisal number;
begin
   for g_id in 1..4 loop
      select username,password into losal,hisal from t_user where id=g_id; 
      dbms_output.put_line(g_id || '数值为' || losal || 'bbb' || hisal);
   end loop;
end;

原创粉丝点击