oracle_day3_

来源:互联网 发布:mysql安装版 64位 编辑:程序博客网 时间:2024/06/06 12:55
--3:使用PL/SQL增加部门信息(加入异常处理)




select * from dept;
declare 
v_id dept.deptno%type:=&请输入编号;
v_dname dept.dname%type:='&请输入部门名称';
v_loc dept.loc%type:='&请输入办公地点';


begin
  insert into dept values(v_id,v_dname,v_loc);
 commit;
  exception
  when DUP_VAL_ON_INDEX then
   dbms_output.put_line('主键重复');
  
  when  others then
      dbms_output.put_line('其他异常');
  
  end;
  
  
  select * from dept;
declare 
v_id dept.deptno%type:=&请输入编号;
v_dname dept.dname%type:='&请输入部门名称';
v_loc dept.loc%type:='&请输入办公地点';
del_inse exception;
pragma exception_init(del_inse,-1);




begin
  insert into dept values(v_id,v_dname,v_loc);
 commit;
  exception
  when del_inse then
   dbms_output.put_line('主键重复惹');
    
  when  others then
      dbms_output.put_line('其他异常');
  
  end;
  --4:购买商品时,如果库存不足,则报异常(自定义异常)




select * from es_product
declare 
v_id2 es_product.id%type:=&请输入编号;
v_unm es_product.stockcount%type:=&请输入购买数量;
va_stockcount es_product.stockcount%type;
kuncun exception;


begin 
  select stockcount  into va_stockcount   from  es_product where id=v_id2;
  if  v_unm>va_stockcount then
   raise kuncun;
   end if;
    update es_product set stockcount=va_stockcount-v_unm where id=v_id2;
  commit;
    exception
  
    when kuncun then
  dbms_output.put_line('库存只有'||va_stockcount);
  when  others then
      dbms_output.put_line('其他异常');
  
  end;
  
  --5:循环插入10个员工信息(名字可以为 员工1,员工2)
     使用隐式游标判断是否执行成功












  
  declare




  
  begin
    for eno in 1..10 loop
      
       INSERT INTO es_user VALUES(10||eno,'员工'||eno,'admin','张管','13801000100','北京市中关村1号','100100','zadmin@aptech.com',0);
       if sql%found then
            dbms_output.put_line('执行成功');
            else
                dbms_output.put_line('执行bu成功');
            end if;
       
       commit;
        end loop;
        exception
          when DUP_VAL_ON_INDEX then
            dbms_output.put_line('主键重复了');
            when others then
              dbms_output.put_line('其他异常');
    end;
  
   --6:删除用户,如果违反完整性约束,使用非预定义异常


 select * from es_user;
    
declare
v_id4 es_user.id%type:=&请输入编号;
pk_del_exception  exception;
pragma exception_init(pk_del_exception,-2292);
 begin
   delete from es_user where id=v_id4;
   exception
      when pk_del_exception then
             dbms_output.put_line('违反完整性约束');
            when others then
              dbms_output.put_line('其他异常');
    end;


--7:用数据泵到处scott下的员工表和部门表,然后再导入 


create directory  dupt_dir4 as 'D:\2017';
grant read,write on directory dupt_dir4 to scott;




expdp scott/123456@orcl directory=dupt_dir4 dumpfile=scotttab.dmp
 tables=emp,dept;
 
impdp scott/123456@orcl directory=dupt_dir4 dumpfile=scotttab.dmp
 tables=emp,dept;
 
 drop table emp;
 drop table dept;select * from emp
 
 s