`
黄兆廷
  • 浏览: 8527 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

oracle基本存储过程

阅读更多
oracle基本存储过程

1.假设有两个表,职员表emp(工号eno,姓名ename,工资esal)和津贴表comm(工号eno,津贴ecomm)
要求用游标完成操作:取出职员表中工资大于1000元的记录,将其工资的30%作为津贴插入
津贴表中.

declare
      --定义游标;
      cursor c is select eno,esal*0.3 from emp where esal>1000;
      vno number;  
      vsal number;
begin
      open c;
      fetch c into vno,vsal;
      while c%found
           loop            
              insert into comm values(vno,vsal);
              fetch c into vno,vsal;
           end loop;
      close c;
end;

2.职员表emp(工号eno,姓名ename,工资esal),用pl/sql匿名块实现输入三个员工的姓名,输出此
3人的平均工资,若平均工资低于800,则所有员工加10%工资,保证此3人平均工资不低于800

declare
  vname1 varchar2(20);
  vname2 varchar2(20);
  vname3 varchar2(20);
  vsal number;
begin
  vname1:='&第1个员工姓名';
  vname2:='&第2个员工姓名';
  vname3:='&第3个员工姓名';

  select avg(esal) into vsal from emp where ename=vname1 or ename=vname2 or ename=vname3;
 
  dbms_output.put_line('此3人平均工资是'||vsal);

  while vsal<800
  loop
     update emp set esal=esal*1.1;
     select avg(esal) into vsal from emp where ename=vname1 or ename=vname2 or ename=vname3;
  end loop;

end;

3.职员表emp(工号eno,姓名ename,出生日期birthday),创建一个函数,实现根据员工编号判断
某员工是否是童工(年龄<18岁)

create or replace function isTonggong(vno varchar2) return boolean
as
  vage number;
begin
  select sysdate-birthday into vage from emp where eno=vno;
  if vage<18 then
    return true;
  else
    return false;
  end if;
end isTonggong;

4.假设有两个表,职员表emp(工号eno,姓名ename,工资esal)和津贴表comm(工号eno,津贴ecomm),要求
在删除职员表中记录时,自动实现津贴表中对应记录的删除

create or replace trigger delemp
after delete on emp
for each row
begin
  delete from comm where eno=:old.eno;
end;

5.职员表emp(工号eno,姓名ename,出生日期birthday),
工号 varchar2(10) 主键 由入职年份和编号构成,如'2003000001'
姓名 varchar2(20)
出生日期 date
创建一个存储过程,实现插入一个新员工

create sequence empseq
start with 1
increment by 1
maxvalue 999999
cycle
cache 20;

create or replace procedure
addemp(vno out varchar2,vname varchar2,vbirth date,result out number)
as
begin
  insert into emp
  values(extract(year from sysdate)||lpad(empseq.nextval,6,0),vname,vbirth);
  select eno into vno from emp where ename=vname;
  result:=1;
exception
  when others then
    result:=0;
end addemp;

6.创建包,里面放入第3题的函数和第5题的存储过程,再添加一个存储过程,完成按员工姓名模糊
查询员工信息
type mycur is ref cursor;

create or replace procedure
selemp(vname varchar2,vcur out mycur,result out number)
as
  vcount int;
begin
  select count(*) into vcount from emp where ename like '%'||vname||'%';
  if vcount>0 then
    open vcur for select * from emp where ename like '%'||vname||'%';
    result:=1;
  else
    result:=0;
  end if;
exception
  when others then
    result:=0;
end selemp;

7.创建java项目或web项目,实现对6题包中的存储过程的调用

追加1:
/**********************题目:新建10个用户***************/
declare
    no_max number;
begin
    select max(empno) into no_max from nemp;
    dbms_output.PUT_LINE(no_max);
    for i in no_max..no_max+9
    loop
      insert into nemp values(i,'newEmploee'||i,'养猪',2000);
    end loop;
end;
/****************题目:做个除零异常并处理******************/
declare
  dividend number;
  divisor number;
begin
  dividend:=&被除数;
  divisor:=&除数;
  dbms_output.put_line(dividend/divisor);
exception
  when zero_divide then
  dbms_output.put_line('ERROR:divisor cannot be zero!');
  when others then
  dbms_output.put_line('ERROR:plesse input again or contact the Adminisrator!');
end;



追加2:
/********************1用循环计算工资总和******************************/
declare
  type emp_cur is ref cursor;
  v_cur emp_cur;
  v_type naemp%rowtype;
  v_sum naemp.EMPSAL%type;
begin
  open v_cur for select * from naemp where empdeptno=30;
  fetch v_cur into v_type;
    while v_cur%found
      loop
        v_sum:=v_sum+v_type.empsal;
      end loop; 
  close v_cur;
end;



/***************************2用游标列出所有信息******************************/
declare
  type emp_cur is ref cursor;
  v_cur emp_cur;
  v_type naemp%rowtype;
begin
  open v_cur for select * from naemp;
  fetch v_cur into v_type;
  while v_cur%found
    loop
      dbms_output.PUT_LINE(v_type.empdeptno||' '||v_type.empno);
    end loop;
 
end;
/**********************************3用联合数组打出所有信息**********************/
declare
  type t1 is table of naemp%rowtype index by binary_integer;
  tt t1;
begin
  select empno,empname,empdeptno,empsal,empmanager  bulk collect into tt from naemp;
  for i in 1..tt.count
    loop
      dbms_output.PUT_LINE(tt(i).empno||' '||tt(i).empname||' '||tt(i).empdeptno||' '||tt(i).empsal||' '||tt(i).empmanager);
    end loop; 
end;



追加3

/******************************1.根据部门编号查询部门信息*****************************/
declare
  type empcur is ref cursor;
  v_cur empcur;
  v_type naemp%rowtype;
  v_no int;
begin
  v_no:=&请输入部门编号;
  open v_cur for select * into v_type from naemp where empno=v_no;
  fetch v_cur into v_type; 
  while v_cur%found
    loop
      dbms_output.PUT_LINE(v_type.empno||' '||v_type.empdeptno||' '||v_type.empname||' '||v_type.empsal||' '||v_type.empmanager);
      fetch v_cur into v_type; 
    end loop;
  close v_cur;
end;
/****************************2.根据编号查信息和确定工资等级*************************/
declare
  type emp_cur is ref cursor;
  v_cur emp_cur;
  v_sal naemp.EMPSAL%type;
  v_no naemp.empno%type;
  v1 nanemp%rowtype;
  v_grade varchar2(4);
begin
  /*根据编号查工资*/
  v_no:=&请输入部门编号;
  open v_cur for select empsal  from naemp where empno=v_no;
  fetch v_cur into v_sal;
  dbms_output.PUT_LINE(v_sal);
  close v_cur;
  /*确定工资等级*/
  open v_cur for select * from nanemp;
  fetch v_cur into v1;
  while v_cur%found
    loop
       v_grade:=case
                     when  v1.empsal>=10000 then 'A'
                     when  v1.empsal>=8000 then 'B'
                     when  v1.empsal>=5000 then 'C'
                     when  v1.empsal>=2000 then 'D'
                     else 'E'
                end;
      dbms_output.PUT_LINE(v1.empname||'的工资等级为'||v_grade);
      fetch v_cur into v1;
    end loop;
  close v_cur;
end;
/*****带有异常处理的工资等级查询***********/
declare
  type v_cursor is ref cursor;
  v_cur v_cursor;
  v_row naemp%rowtype;
  v_level varchar2(2);
  v_no number;
begin
  v_no:=&请输入部门编号;
  open v_cur for select empno,empname,empdeptno,empsal,empmanager from naemp where empno=v_no;
  fetch v_cur into v_row;
  if v_cur%notfound then
     raise no_data_found;
  end if;
  dbms_output.PUT_LINE(v_row.empsal);
  v_level:=case
              when v_row.empsal>=10000 then 'A'
              when v_row.empsal>=8000 then 'B'
              when v_row.empsal>=5000 then 'C'
              when v_row.empsal>=2000 then 'D'
              else 'E'
            end;
    dbms_output.PUT_LINE('该雇员目前的工资等级为:'||v_level);
    close v_cur;
exception
   when no_data_found then
     dbms_output.PUT_LINE('对不起!没有这个雇员!');
   when others then
     dbms_output.PUT_LINE('未知错误!');
end;
分享到:
评论

相关推荐

    oracle存储过程学习经典入门

    Oracle 存储过程学习目录是 Oracle 存储过程学习的基础知识,了解 Oracle 存储过程的基本语法、基础知识和一些常见问题的解决方法是非常重要的。本文将从 Oracle 存储过程的基础知识开始,逐步深入到 Oracle 存储...

    Oracle存储过程基本语法

    Oracle 存储过程基本语法 Oracle 存储过程是一种可以在 Oracle 数据库中创建和执行的程序单元,它可以完成多种操作,如数据处理、数据报表、数据统计等。下面是 Oracle 存储过程的基本语法。 创建存储过程 CREATE...

    Oracle存储过程最基本的开发规范

    行业内Oracle存储过程最基本的开法规范,适合oracle入门小白学习

    Oracle_存储过程的基本语法

    3.1 oracle 存储过程的基本语法 3.1.1 基本结构 CREATE OR REPLACE PROCEDURE 存储过程名字 ( 参数1 IN NUMBER, 参数2 IN NUMBER ) IS 变量1 INTEGER :=0; 变量2 DATE; BEGIN END 存储过程名字 3.1.2 ...

    oracle 存储过程的基本语法

    oracle 存储过程的基本语法, 介绍了oracle 中PL/sql里面 存储过程的基本语法~

    Oracle数据库存储过程技术文档.doc

    第一章 oracle存储过程概述 2 1.1 存储过程基本结构(PROCEDURE) 3 1.1.1创建存储过程 3 1.1.2 存储过程删除 5 1.1.3 调用存储过程 5 1.2存储函数(FUNCTIONE) 6 1.2.1 创建存储函数 6 1.2.2 删除存储函数 7 1.3 包...

    oracle存储过程基本语法.txt

    oracle存储过程基本语法 .txt

    Oracle存储过程的基本语法

    以下是Oracle存储过程中涉及的一些基本语法元素的详细解释: 1. **定义存储过程**: 使用`CREATE OR REPLACE PROCEDURE`语句来创建或替换一个存储过程。例如: ```sql CREATE OR REPLACE PROCEDURE proc_name ( ...

    Oracle存储过程开发的要点

    Oracle存储过程是数据库管理系统Oracle中的一种重要特性,用于封装一系列SQL和PL/SQL代码,以实现特定的功能。在Oracle中,存储过程可以提高应用程序的性能,因为它减少了与数据库的交互次数,并允许在数据库级别...

    oracle存储过程常用技巧

    在本文中,我们将介绍 Oracle 存储过程的基本结构、参数传递方式、游标处理、异常处理等常用技巧。 一、存储过程的基本结构 一个 Oracle 存储过程通常由以下几个部分组成: 1. 创建语句:create or replace ...

    Oracle PlSql 存储过程

    Oracle 存储过程基础知识包括了解 Oracle 存储过程的基本语法和结构。Oracle 存储过程的基本语法包括 PROCEDURE、FUNCTION 和 PACKAGE 等。PROCEDURE 是最基本的存储过程单元,FUNCTION 是带返回值的存储过程,而 ...

    oracle的存储过程学习资料

    oracle的存储过程学习资料,包含基本操作和进一步的资料

    oracle存储过程语法

    创建存储过程的基本语法 CREATE OR REPLACE PROCEDURE 存储过程名 IS BEGIN NULL; END; 存储过程的组成部分 * `CREATE OR REPLACE PROCEDURE`:创建或替换存储过程的语句。 * `IS`:关键词,表明后面将跟随一个...

    Oracle数据库中存储过程的异步调用

    本文讲述如何在Oracle通过任务和管道的应用,异步调用存储过程的方法。并且介绍了其基本原理和具体实现测试。

    Oracle存储过程基本语法及示例

    Oracle存储过程基本语法及示例,都是些基础,我喜欢基础扎实

    Oracle 分页的存储过程

    首先,我们来看创建分页存储过程的基本步骤: 1. 创建包规范(Package Specification): 包规范定义了存储过程的接口,包括输入参数、输出参数和返回类型。在示例中,`package_page` 包定义了一个名为 `proc_page...

    oracle存储过程的基本用法

    结合实例,介绍了oracle存储过程的用法,包括定义,变量类型,游标,流程分支语句的使用

    oracle_存储过程的基本语法_及注意事项

    oracle_存储过程的基本语法_及注意事项,很好很不错的资源哦

    关于oracle存储过程的基本语法

    在存储过程中,select某一字段时,后面必须紧跟into,如果select整个记录,利用游标的话就另当别论了;在利用select...into...语法时,必须先确保数据库中有该条记录,否则会报出"no data found"异常;在存储过程中,...

Global site tag (gtag.js) - Google Analytics