设为首页 收藏本站
查看: 2102|回复: 0

[经验分享] oracle case ,loop,while ,for简单实例

[复制链接]

尚未签到

发表于 2016-8-11 07:15:14 | 显示全部楼层 |阅读模式
select * from employees emp  where emp.salary = 3000
if语句
begin
if (10>50) then
   dbms_output.put_line('da yu');
else
  dbms_output.put_line('bu da yu');
   
end if;
end;
select * from employees emp where emp.employee_id=119
where
emp.department_id=30 and
salary < 250

DECLARE
v_sal number(10);
v_empid number(4);
BEGIN
      v_empid := &nid ;   
      SELECT emp.salary into v_sal
      FROM employees emp
      WHERE emp.employee_id=v_empid;
      IF v_sal <= 2500 THEN
         UPDATE employees set salary=salary+200 WHERE employee_id=v_empid;
      ELSIF v_sal>2500 and v_sal<3000 then
         UPDATE employees set salary=salary+100 WHERE employee_id=v_empid;
      ELSE
         dbms_output.put_line('没有在加薪范围');      
      END IF;
EXCEPTION
    WHEN no_data_found THEN
         dbms_output.put_line('没有找到改员工!');
      
END;



------------case 单值 等值比较----------------------

declare
str number;
begin
  str:=&str;
  case str
    when 60 then
       dbms_output.put_line('不及格');
    when 70 then
       dbms_output.put_line('优良');
    when 80 then
       dbms_output.put_line('优秀');
    else
       dbms_output.put_line('其他');
  end case;
end;
------------case 范围 条件比较----------------------
declare
num number(6,2);
begin
  num:=&num;
  case
    when num<60 then
       dbms_output.put_line('不及格');
    when num<80 then
       dbms_output.put_line('优良');
    when num<100 then
       dbms_output.put_line('优秀');
  end case;
  exception
  when case_not_found then
   dbms_output.put_line('没有符合要求的case 语句:'||sqlerrm);
end;
--------------case 表达式---------------------------
--用在赋值语句中
declare
num number(5);
val varchar2(50);
begin
num:=&num;
val:=case num
     when 1 then  '第一组'
     when 2 then  '第二组'
     when 3 then  '第三组'
     end || '是好样的' ;
dbms_output.put_line(val);
end;

--用在select 语句当中
select * from employees where employee_id=109
declare
str varchar2(200);
begin
select case
       when salary between 2000 and 3000 then
           '普通白领'
       when salary between 6000 and 10000 then
           '公司金领'
       else
          '职员'
       end kkk into str from employees where employee_id=109;
  dbms_output.put_line(str);
end;


select emp.first_name,emp.phone_number, case
       when emp.salary between 2000 and 3000 then
           '普通白领'
       when emp.salary between 6000 and 10000 then
           '公司金领'
       else
          '职员'
       end as 职员类型
       from employees emp
--------------------goto  null---------------------------

declare
num number(5):=10;
begin
  if num>5 then
     goto label1;
  else
    -- dbms_output.put_line('nothing');
     null;--不做任何事,其主要目的是为了确保程序结构的完整性。
  end if;
   dbms_output.put_line('welcome to you!');
  <<label1>>
     dbms_output.put_line('大于5');
  
end;
--------------------loop ------------------------------------

/*
特点:循环至少运行一次
循环语句的共同点: 都是有 loop   end loop;构成
*/
create table tmp
(
  tid number(6) primary key,
  tname varchar2(10)
)

declare
i number(6):=1;
begin
  loop  
    insert into tmp values(i,'值'||i);
    exit when i!=0;  --循环终止语句
    i:=i+1;
  end loop;
  dbms_output.put_line('数据入库完毕');
end;

select * from tmp
--------------------while ---------------------------------

create table tmp
(
  tid number(6) primary key,
  tname varchar2(10)
)
delete from tmp;

declare
i number(6):=1;
begin
  while i<=10
  loop
    insert into tmp values(i,'值'||i);
    i:=i+1;
  end loop;
  commit;
end;
---------------------for----------------------------------

/*
循环次数是确定的
*/
declare
i number(5);
j number(5);
begin
  for i in reverse 1..10
  loop
      for j in 1..i
      loop
          dbms_output.put('*');
      end loop;
      dbms_output.put_line('');
  end loop;
end;
----------------------预定义异常-------------------------------

不需要定义,不需要手动抛出
  select *
    from employees emp
    where emp.employee_id=1
declare
sal char(1);
e_integrity EXCEPTION;
PRAGMA EXCEPTION_INIT(e_integrity,-01422);
begin
    select emp.salary  into sal
    from employees emp
    where emp.employee_id<10000;
    exception
          when e_integrity then
              dbms_output.put_line('值太多:'||sqlerrm);
          when no_data_found then
               dbms_output.put_line('没有值:'||sqlerrm);
        --  when others then
          --    dbms_output.put_line('赋值出错'||sqlerrm);   
end;
----------------------非预定义------------------------
declare
  e_integrity EXCEPTION;
  PRAGMA EXCEPTION_INIT(e_integrity,-2291);
BEGIN
  UPDATE employees SET employees.department_id=10
  WHERE employees.employee_id=101;
  EXCEPTION
    WHEN e_integrity THEN
       dbms_output.put_line('该部门不存在!'||sqlerrm);
    when others then
        dbms_output.put_line('该部门不存在--!'||sqlcode||' '||sqlerrm);
END;

select * from departments

----------------------自定义---------

--手动定义,手动抛出
select * from employees where employees.employee_id=1111
declare
ex exception;
begin
    update employees set employees.salary=10000
    where employees.employee_id=1111 ;
    if sql%notfound then
       raise ex;--注意:自定义异常一定要手动抛出
    end if;
    exception
          when ex then
              
              dbms_output.put_line('没有此条数据:'||sqlerrm);
              --抛出自定义异常
              --raise_application_error(-20001,'该雇员不存在!');
          when others then
              dbms_output.put_line('赋值出错');   
    end;
   
   
    begin
              raise_application_error(-20001,'该雇员不存在!');
    end;
   
create or replace procedure Pro_test_exep
as
ex exception;
begin
    update employees set employees.salary=10000
    where employees.employee_id=1111 ;
    if sql%notfound then
       raise ex;
    end if;
    exception
          when ex then
              
              dbms_output.put_line('没有此条数据:'||sqlerrm);
              --抛出自定义异常
              raise_application_error(-20001,'该雇员不存在!');
          when others then
              dbms_output.put_line('赋值出错');   
    end;
   
   
    declare
    e_integrity EXCEPTION;
    PRAGMA EXCEPTION_INIT(e_integrity,-20001);
    begin
         Pro_test_exep;
         exception
         when e_integrity then
              dbms_output.put_line('该雇员不存在');  
    end;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-256078-1-1.html 上篇帖子: oracle load data 导入数据方法总结 下篇帖子: oracle 函数,包,存储过程简单实例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表