掌握数据库PL/SQL编程语言,以及数据库存储过程的设计和使用方法。
存储过程定义,存储过程运行,存储过程更名,存储过程删除,存储过程的参数传递。掌握PL/SQL编程语言和编程规范,规范设计存储过程。
①定义一个存储过程,更新所有订单的(含税折扣价)总价。
CREATE PROCEDURE update_all_orderaccount()
BEGIN
UPDATE order_form,
(SELECT Order_number,SUM(Order_Detail_Price*(1 - Discount)*(1 + Tax)) AS sum_account
FROM order_form_detail
GROUP BY Order_number) AS temp
SET order_form.Order_account = sum_account
WHERE order_form.Order_number = temp.Order_number;
END;
②执行存储过程
CALL update_all_orderaccount();
①定义一个存储过程,更新选定订单的(含税折扣价)总价。
CREATE PROCEDURE update_a_orderaccount(number char(20))
BEGIN
UPDATE order_form
SET order_form.Order_account =
(SELECT SUM(Order_Detail_Price*(1 - Discount)*(1 + Tax)) AS sum_account
FROM order_form_detail
GROUP BY Order_number
HAVING number = Order_number)
WHERE order_form.Order_number = number;
END;
②执行存储过程。
#更新’00001’号订单的总价
CALL update_a_orderaccount('00001');
①定义一个存储过程,更新某个顾客的所有订单的(含税折扣价)总价。
CREATE PROCEDURE update_customer_account(cname char(20))
BEGIN
DECLARE cnumber INTEGER;
SELECT Customer_number INTO cnumber
FROM customer
WHERE Customer_name = cname;
UPDATE order_form,
(SELECT Order_number,SUM(Order_Detail_Price*(1 - Discount)*(1 + Tax)) AS sum_account
FROM order_form_detail
GROUP BY Order_number) AS temp
SET order_form.Order_account = sum_account
WHERE order_form.Order_number = temp.Order_number AND order_form.Customer_number = cnumber;
END;
②执行存储过程。
#更新顾客小明的订单总价
CALL update_customer_account('小明');
①定义一个存储过程,更新某个顾客的所有订单的(含税折扣价)总价。
CREATE PROCEDURE update_customer_out_account(cname char(20),OUT account INTEGER)
BEGIN
DECLARE cnumber INTEGER;
SELECT Customer_number INTO cnumber
FROM customer
WHERE Customer_name = cname;
UPDATE order_form,
(SELECT Order_number,SUM(Order_Detail_Price*(1 - Discount)*(1 + Tax)) AS sum_account
FROM order_form_detail
GROUP BY Order_number) AS temp
SET order_form.Order_account = sum_account
WHERE order_form.Order_number = temp.Order_number AND order_form.Customer_number = cnumber;
SELECT SUM(order_form.Order_account) INTO account
FROM order_form
GROUP BY Customer_number
HAVING order_form.Customer_number = cnumber;
END;
②执行存储过程。
CALL update_customer_out_account('小明',@account);
③查看存储过程执行结果。
SELECT @account;
学习到了如何创建不同类型的存储过程。
经过试验,存储过程类似于编程中的“函数”,可以通过传递参数或传出参数,并且执行存储过程中的所有“函数语句”,使用方法也类似于函数。而对于局部变量的使用与赋值也类似与普通的编程语言,可以说只要做出相关查询更新方法,整体实验难度不高。
在实验过程中,对于数据的处理较为麻烦,但是也学习到了SELECT
语句的结果形成的临时表是可以作为UPDATE
的更新表来使用,方便了对于数据的处理和选择。
对于存储过程的删除语句为:
DROP PROCEDURE update_all_orderaccount;#DROP PROCEDURE procedure_name;