oracle我以前的资料(一些基本存储过程)
程序员文章站
2022-03-28 23:22:03
...
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题包中的存储过程的调用
要求用游标完成操作:取出职员表中工资大于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题包中的存储过程的调用