存储过程

-- 七、存储过程
/* 格式
create [or replace]procedure存储过程名[(参数列表)]
is/as
[变量声明]
begin
执行语句
[异常处理]
end;
注意:
参数列表中的参数可以传入in,也可以传出out,
还可以传入传出in out
如果是传入,in 可以省略
 */
-- 1、没有参数,没有声明,没有异常处理的存储过程
-- 定义:
create or replace procedure p_hello
is
begin
 dbms_output.put_line('HELLO WORLD');
end;
调用:
begin
p_hello;
end;

-- 2、没有参数,有声明和异常处理
-- 用存储过程实现7369这名员工的姓名并输出
create or replace procedure p_find_emp
is
xm varchar2(20);
begin
select ename into xm from emp
where empno=7369;
dbms_output.put_line(xm);
exception
when no_data_found then 
dbms_output.put_line('没有找到您要找的数据');
when others then
dbms_output.put_line('其他异常');
end;
调用
begin
p_find_emp;
end;

-- 3、给某个指定员工工资累加指定金额
create or replace procedure p_update_emp(eno number,salary number)
is
begin
update emp set sal=sal+ salary
where empno =eno;
if sql%notfound then
dbms_output.put_line('没有您要修改的数据');
else
dbms_output.put_line('更新成功');
end if;
end;
调用
declare
bh number;
jiangjin number;
begin
bh:=&请输入编号;
jiangjin:=&请输入奖金;
p_update_emp(bh,jiangjin);

end;

-- 4、根据指定的部门编号显示员工人数,员工人数在调用里输出
create or replace procedure p_number(dno number)
is
n number;
begin
select count(empno)into n from emp
where deptno=dno;
dbms_output.put_line('人数:'||n);
exception
when no_data_found then 
dbms_output.put_line('没有找到您要找的数据');
when others then
dbms_output.put_line('其他异常');
end;

-- 调用
declare
dno number;
begin
dno:=&请输入一个部门编号;
p_number(dno);
end;

-- -改造-
create or replace procedure p_number(dno in number,n out number)
is
begin
select count(empno)into n from emp
where deptno=dno;
end;

-- 调用
declare
dno number;
tongji number;
begin
dno:=&请输入一个部门编号;
p_number(dno,tongji);
dbms_output.put_line('人数:'||tongji);
end;

-- 6、做两个数的交换 ,交换后的结果在调用中输出
create or replace procedure swap(a in out number,b in out number)
is
c number;
begin
 c:=a;
 a:=b;
 b:=c;
end;

-- 调用
declare
m number;
n number;
begin
m:=&请输入第一个数;
n:=&请输入第二个数;
swap(m,n);
dbms_output.put_line(m||' '||n);
end;

-- 用存储过程求在某地工作的员工的姓名及工资
create or replace procedure p_emp2(locat varchar2)
is
游标声明
cursor c_emp(loca varchar2)
is 
select ename,sal from emp
where deptno =(select deptno from dept
 where loc=loca);
begin
for jr in c_emp(locat)
loop
dbms_output.put_line('姓名:'||jr.ename||'工资:'||jr.sal);
end loop;
end;

-- 调用
declare
locat varchar2(20);
begin
locat:='&请输入一个工作地点';
p_emp2(locat);
end;


/*
创建一个名为course_sum的存储过程,
可查询某门课程考试的总成绩、考生人数。
总成绩可以输出,进一步加调用。
*/
create or replace procedure course_sum(kno number,total out number)
is
coun number;
begin
select sum(grade),count(sno)into total,coun from sc
where cno=kno;
dbms_output.put_line(total);
end;

/*
创建一执行该存储过程的批处理,要求当总成绩小于300时,
显示信息为:某课程名+“的总成绩为:“+总成绩,
其总分未达300分。大于300时,显示信息为:
某课程名+“的总成绩为:“+总成绩。
*/
declare
total number;
kno number;
km varchar2(20);
begin
kno:=&请输入一个课程编号;
course_sum(kno,total);
select cname into km from course
where cno=kno;
if total<300  then
--...
else
--...
end if;
end;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值