存储过程实验

实验任务:
存储过程定义,存储过程运行,存储过程运行,存储过程更名,存储过程删除,存储过程的参数传递。掌握PL/SQL编程语言和编程规范,规范设计存储过程。
环境:Navicat premiun,以及使用实验一的表格
结构:
(1)无参数的存储过程。定义一个存储过程,更新所有订单的(含税折扣价)总价。
(2)有参数的存储过程。定义一个存储过程,更新所有订单的(含税折扣价)总价。
(3)有局部变量的存储过程。定义一个存储过程,更新某个顾客的所有订单的(含税折扣价)总价。
(4)有输出参数存储过程。定义一个存储过程,更新某个顾客的所有订单的(含税折扣价)总价。
具体的程序功能(核心代码)

use TPCH;
#(1)根据订单明细表,计算每个订单的总价,更新Orders表#
create procedure Proc_CalTotalPrice()
begin
update orders 
set totalprice=
(select SUM(extendedprice*(1-discount)*(1+tax)) from lineitem)
where orderskey=orderskey;
end;
#查看存储过程或函数定义#
show create procedure Proc_CalTotalPrice;
#执行存储过程Proc_CalTotalPrice()#
call Proc_CalTotalPrice();
//(2)
create procedure Proc_CalTotalPrice4Order(okey integer)
begin
update Orders 
set totalprice=
(select sum(extendedprice * (1-discount)*(1+tax)) from Lineitem)
where orderskey= orderskey 
and okey=orderskey;
end;
#执行存储过程Proc_CalTotalPrice4Order()#
call Proc_CalTotalPrice4Order(1854);
//(3)
create procedure Proc_CalTotalPrice4Customer(p_name char(25))
begin
declare L_custkey integer;
select custkey INTO L_custkey/*查找给定客户名对应的客户编号*/
from Customer
where name=TRIM(p_name);/*TRIM是系统函数,截去字符串前后空格*/update Orders set totalprice=(select sum(extendedprice * (1-discount)*(1+tax)) from Lineitem)where orderskey=orderskey and custkey=L_custkey;
end;
#执行存储过程 Proc_CalTotalPrice4Customer()#
call  Proc_CalTotalPrice4Customer('zheng');
#查看存储过程执行结果#
select * from Orders
where custkey=(select custkey from Customer where name='feiyan');
//(4)
create procedure Proc_CalTotalPrice4Customer1(
IN p_custname char(25),OUT p_totalprice real)
begin
declare L_custkey integer;
select custkey into L_custkey
from Customer
where name=TRIM(p_custname);
update Orders set totalprice=
(select sum(extendedprice * (1-discount)*(1+tax)) from Lineitem)
where orderskey=orderskey 
and custkey=L_custkey;
select sum(totalprice) into p_totalprice
from Orders where custkey=L_custkey;
end;
#执行存储过程 Proc_CalTotalPrice4Customer2()#
call Proc_CalTotalPrice4Customer1('feiyan',@totalprice);
select @totalprice;
#查看存储过程执行结果#
/*检查下列sql语句执行结果与上述存储过程执行结果是否一致*/
select sum(totalprice)
from Orders 
where custkey= (select custkey from Customer where name='feiyan'); 
#修改存储过程Proc_CalTotalPrice4Order为CalTotalPrice4Order#
ALTER PROCEDURE Proc_CalTotalPrice4Order RENAME TO CalTotalPrice4Order;
#编译存储过程#
alter procedure CalTotalPrice4Orde(okey integer) compile;
#删除存储过程#
drop procedure Proc_CalTotalPrice;

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45275526/article/details/108694652