《MySQL必知必会》[05] 存储过程和游标
1、存储过程
- 获得基本的订单总额
- 将营业税有条件地添加到合计中
- 返回合计
1.1 基本语句
--创建存储过程
CREATE PROCEDURE <存储过程的名称>(<变量的类型定义>)
BEGIN
<执行操作>
END;
--执行存储过程
CALL <存储过程的名称>(<@变量名>);
--删除存储过程
DROP PROCEDURE <存储过程的名称>;
--创建存储过程
CREATE PROCEDURE <存储过程的名称>(<变量的类型定义>)
BEGIN
<执行操作>
END;
--执行存储过程
CALL <存储过程的名称>(<@变量名>);
--删除存储过程
DROP PROCEDURE <存储过程的名称>;
1.2 创建
-- Name: ordertotal
-- Parameters: onumber = order number
-- taxable = 0 if not taxable, 1 if taxable
-- ototal = order total variable
CREATE PROCEDURE ordertotal(
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8, 2)
) COMMENT 'Obtain order total, optionally adding tax'
BEGIN
--Declare variable for total
DECLARE total DECIMAL(8, 2);
--Declare tax percentage
DECLARE taxrate INT DEFAULT 6;
--GET the order total
SELECT Sum(item_price*quantity)
FROM orderitems
WHERE order_num = onumber
INTO total;
--Is this taxable
IF taxable THEN
SELECT total+(total/100*taxrate) INTO total;
END IF;
SELECT total INTO ototal;
END;
-- Name: ordertotal
-- Parameters: onumber = order number
-- taxable = 0 if not taxable, 1 if taxable
-- ototal = order total variable
CREATE PROCEDURE ordertotal(
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8, 2)
) COMMENT 'Obtain order total, optionally adding tax'
BEGIN
--Declare variable for total
DECLARE total DECIMAL(8, 2);
--Declare tax percentage
DECLARE taxrate INT DEFAULT 6;
--GET the order total
SELECT Sum(item_price*quantity)
FROM orderitems
WHERE order_num = onumber
INTO total;
--Is this taxable
IF taxable THEN
SELECT total+(total/100*taxrate) INTO total;
END IF;
SELECT total INTO ototal;
END;
CREATE PROCEDURE ordertotal(
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8, 2)
) COMMENT 'Obtain order total, optionally adding tax'
CREATE PROCEDURE ordertotal(
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8, 2)
) COMMENT 'Obtain order total, optionally adding tax'
- 使用CREATE PROCEDURE关键词创建了名为ordertotal的存储过程
- 该存储过程定义了三个变量,IN表示要求输入的参数,OUT表示输出的结果。INT、BOOLEAN等表示变量的数据类型
- COMMENT非必需,如果有,那么在SHOW PROCEDURE STATUS的结果时会显示(简单说,类似于方法的说明)
BEGIN
...
END;
BEGIN
...
END;
- BEGIN和END用来界定存储过程操作执行的语句
--Declare variable for total
DECLARE total DECIMAL(8, 2);
--Declare tax percentage
DECLARE taxrate INT DEFAULT 6;
--GET the order total
SELECT Sum(item_price*quantity)
FROM orderitems
WHERE order_num = onumber
INTO total;
--Declare variable for total
DECLARE total DECIMAL(8, 2);
--Declare tax percentage
DECLARE taxrate INT DEFAULT 6;
--GET the order total
SELECT Sum(item_price*quantity)
FROM orderitems
WHERE order_num = onumber
INTO total;
- DECLARE用来定义存储过程中的局部变量
- INTO表示赋值到变量
--Is this taxable
IF taxable THEN
SELECT total+(total/100*taxrate) INTO total;
END IF;
--Is this taxable
IF taxable THEN
SELECT total+(total/100*taxrate) INTO total;
END IF;
- IF <boolean> THEN <do something> END IF 为条件执行语句,记得END IF结尾
public void ordertotal(int onumber, boolean taxable, double ototal) {
double total;
int taxrate = 6;
total = getOrderTotal(onumber);
if (taxable) {
total += total / (100 * taxrate);
}
ototal = total;
}
public void ordertotal(int onumber, boolean taxable, double ototal) {
double total;
int taxrate = 6;
total = getOrderTotal(onumber);
if (taxable) {
total += total / (100 * taxrate);
}
ototal = total;
}
1.3 执行
--不含营业税
CALL ordertotal(20005, 0, @total);
SELECT @total
+----------+
| @total |
+----------+
| 149.87 |
+----------+
--包含营业税
CALL ordertotal(20005, 1, @total);
SELECT @total
+-----------------+
| @total |
+-----------------+
| 158.862200000 |
+-----------------+
--不含营业税
CALL ordertotal(20005, 0, @total);
SELECT @total
+----------+
| @total |
+----------+
| 149.87 |
+----------+
--包含营业税
CALL ordertotal(20005, 1, @total);
SELECT @total
+-----------------+
| @total |
+-----------------+
| 158.862200000 |
+-----------------+
1.4 检查
SHOW CREATE PROCEDURE ordertotal;
SHOW CREATE PROCEDURE ordertotal;
1.5 删除
DROP PROCEDURE ordertotal;
DROP PROCEDURE ordertotal;
2、游标
2.1 创建游标
CREATE PROCEDURE processorders()
BEGIN
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
END;
CREATE PROCEDURE processorders()
BEGIN
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
END;
2.2 打开和关闭游标
CREATE PROCEDURE processorders()
BEGIN
--Declare
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Open
OPEN ordernumbers;
--Close
CLOSE ordernumbers;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Open
OPEN ordernumbers;
--Close
CLOSE ordernumbers;
END;
2.3 使用游标数据
CREATE PROCEDURE processorders()
BEGIN
--Declare
DECLARE o INT;
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Open
OPEN ordernumbers;
--Get
FETCH ordernumbers INTO o;
--Close
CLOSE ordernumbers;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare
DECLARE o INT;
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Open
OPEN ordernumbers;
--Get
FETCH ordernumbers INTO o;
--Close
CLOSE ordernumbers;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare local variables
DELCARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
--Declare cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
--Create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals(order_num INT, total DECIMAL(8,2));
--Open the cursor
OPEN ordernumbers;
--Loop through all rows
REPEAT
FETCH ordernumbers INTO o;
CALL ordertotal(o, 1, t);
INSERT INTO ordertotals(order_num, total) VALUES(o, t);
UNTIL done END REPEAT;
--Close the cursor
CLOSE ordernumbers;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare local variables
DELCARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
--Declare cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
--Create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals(order_num INT, total DECIMAL(8,2));
--Open the cursor
OPEN ordernumbers;
--Loop through all rows
REPEAT
FETCH ordernumbers INTO o;
CALL ordertotal(o, 1, t);
INSERT INTO ordertotals(order_num, total) VALUES(o, t);
UNTIL done END REPEAT;
--Close the cursor
CLOSE ordernumbers;
END;
- 以上存储过程,游标不断读取订单号,并以此为参调用另一个存储过程,将最终的值填入到表ordertotals中
- CONTINUE HANDLER 是在条件出现时执行的代码,SQLSTATE '02000' 表没有找到更多的行(MySQL错误代码)
SELECT * FROM ordertotals;
+---------+---------+
| 20005 | 158.86 |
| 20006 | 58.30 |
| 20007 | 1060.00 |
| 20008 | 132.50 |
| 20009 | 40.78 |
+---------+---------+
SELECT * FROM ordertotals;
+---------+---------+
| 20005 | 158.86 |
| 20006 | 58.30 |
| 20007 | 1060.00 |
| 20008 | 132.50 |
| 20009 | 40.78 |
+---------+---------+
《MySQL必知必会》[05] 存储过程和游标的更多相关文章
- 《mysql必知必会》读书笔记--存储过程的使用
以前对mysql的认识与应用只是停留在增删改查的阶段,最近正好在学习mysql相关内容,看了一本书叫做<MySQL必知必会>,看了之后对MySQL的高级用法有了一定的了解.以下内容只当读书 ...
- 《MySQL必知必会》整理
目录 第1章 了解数据库 1.1 数据库基础 1.1.1 什么是数据库 1.1.2 表 1.1.3 列和数据类型 1.1.4 行 1.1.5 主键 1.2 什么是SQL 第2章 MySQL简介 2.1 ...
- MySQL使用和操作总结(《MySQL必知必会》读书笔记)
简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...
- mysql学习--mysql必知必会1
例如以下为mysql必知必会第九章開始: 正則表達式用于匹配特殊的字符集合.mysql通过where子句对正則表達式提供初步的支持. keywordregexp用来表示后面跟的东西作为正則表達式 ...
- 《MySQL必知必会》[01] 基本查询
<MySQL必知必会>(点击查看详情) 1.写在前面的话 这本书是一本MySQL的经典入门书籍,小小的一本,也受到众多网友推荐.之前自己学习的时候是啃的清华大学出版社的计算机系列教材< ...
- mysql学习--mysql必知必会
上图为数据库操作分类: 下面的操作參考(mysql必知必会) 创建数据库 运行脚本建表: mysql> create database mytest; Query OK, 1 row ...
- mysql 必知必会总结
以前 mysql 用的不是很多, 2 天看了一遍 mysql 必知必会又复习了一下基础. 200 页的书,很快就能看完, 大部分知识比较基础, 但还是了解了一些以前不知道的知识点.自己做一个备份,随 ...
- 读《MySql必知必会》笔记
MySql必知必会 2017-12-21 意义:记录个人不注意的,或不明确的,或不知道的细节方法技巧,此书250页 登陆: mysql -u root-p -h myserver -P 9999 SH ...
- MySql必知必会内容导图
<MySQL必知必会>从介绍简单的数据检索开始,逐步深入一些复杂的内容,包括联结的使用.子查询.正则表达式和基于全文本的搜索.存储过程.游标.触发器.表约束,等等.通过重点突出的章节,条理 ...
- 读《MySQL必知必会》我学到了什么?
前言 最近在写项目的时候发现自己的SQL基本功有些薄弱,遂上知乎查询MYSQL关键字,期望得到某些高赞答案的指点,于是乎发现了 https://www.zhihu.com/question/34840 ...
随机推荐
- HTML中令人惊喜的全局属性
1.accesskey 属性 : 规定激活元素的快捷键. 浏览器支持:几乎所有浏览器均 accesskey 属性,除了 Opera. 定义和用法 accesskey 属性规定激活(使元素获得焦点)元素 ...
- Autoit3操作网页实现自动化
Autoit3 本身有内置的用户自定义函数IE.au3,只限于IE浏览器,如果是Firefox浏览器需要另外自定义函数. 找了很多资料发现有个FF.au3的自定义函数,下载地址 http://www. ...
- Salesforce的基础用户界面定制
Salesforce的用户界面 Salesforce的用户界面易于定制.本文将讲述如何定制: 主菜单和选项卡 自定义按钮和链接 视图列表 页面布局 定制主菜单和选项卡 Salesforce的主菜单默认 ...
- mybatis学习系列一(mybatis简介/使用)
1mybatis简介(1) 1.1工具:jbbc,jdbctemplate 功能简单,sql语句编写在java代码里面,硬编码高耦合的方式 1.2 框架:整体解决方案 1.2.1 Hibernate: ...
- Linux Mint Mate 常用
Linux Mint基于Ubuntu Mate 桌面版据说资源占用较少 http://mirrors.tuna.tsinghua.edu.cn/linuxmint-cd/stable/17.3/li ...
- [20180123]测试SQLNET.EXPIRE_TIME参数.txt
[20180123]测试SQLNET.EXPIRE_TIME参数.txt --//曾经写过一篇linux内核网络参数测试tcp_keepalive,链接http://blog.itpub.net/26 ...
- python自动化报告的输出
1.设计简单的用例 2.设计用例 以TestBaiduLinks.py命名 # coding:utf-8 from selenium import webdriver import unittest ...
- js中var的有或无--重复声明和以后的声明
js中var的有或无--重复声明和以后的声明 使用var语句多次声明一个变量不仅是合法的,而且也不会造成任何错误. 如果重复使用的一个声明有一个初始值,那么它担当的不过是一个赋值语句的角色. 如果重复 ...
- nodejs在spawn中执行npm报错 [Error: spawn ENOENT]” errors
描述: 上代码 var ps = require('child_process').spawn("npm", ['install'], { stdio: 'inherit', cw ...
- lua保留n位小数方法
time:2015/04/21 1. string.format() function GetPreciseDecimal(nNum, n) if type(nNum) ~= "number ...