MySQL.PROCEDURE.使用存储过程

mysql> DELIMITER //
mysql> CREATE PROCEDURE productpricing()
    -> BEGIN
    -> SELECT AVG(prod_price) AS priceaverage
    -> FROM products;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql> CALL productpricing();
+--------------+
| priceaverage |
+--------------+
|    16.133571 |
+--------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> DROP PROCEDURE productpricing;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //
mysql> CREATE PROCEDURE productpricing(
    -> OUT pl DECIMAL(8,2),
    -> OUT ph DECIMAL(8,2),
    -> OUT pa DECIMAL(8,2)
    -> )
    -> BEGIN
    -> SELECT MIN(prod_price)
    -> INTO pl
    -> FROM products;
    -> SELECT MAX(prod_price)
    -> INTO ph
    -> FROM products;
    -> SELECT AVG(prod_price)
    -> INTO pa
    -> FROM products;
    -> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL productpricing(@pricelow, @pricehigh, @priceaverage);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> SELECT @priceaverage;
+---------------+
| @priceaverage |
+---------------+
|         16.13 |
+---------------+
1 row in set (0.00 sec)

mysql> SELECT @pricehigh, @pricelow, @priceaverage;
+------------+-----------+---------------+
| @pricehigh | @pricelow | @priceaverage |
+------------+-----------+---------------+
|      55.00 |      2.50 |         16.13 |
+------------+-----------+---------------+

mysql> DROP PROCEDURE productpricing;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //
mysql> CREATE PROCEDURE ordertotal(
    -> IN onumber INT,
    -> OUT ototal DECIMAL(8,2)
    -> )
    -> BEGIN
    -> SELECT SUM(item_price * quantity)
    -> FROM orderitems
    -> WHERE order_num = onumber
    -> INTO ototal;
    -> END//
Query OK, 0 rows affected (0.00 sec)

or

mysql> DELIMITER //
mysql> CREATE PROCEDURE ordertotal(
    -> IN onumber INT,
    -> OUT ototal DECIMAL(8,2)
    -> )
    -> BEGIN
    -> SELECT SUM(item_price * quantity)
    -> INTO ototal
    -> FROM orderitems
    -> WHERE order_num = onumber;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql> CALL ordertotal(20005, @total);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @total;
+--------+
| @total |
+--------+
| 149.87 |
+--------+
1 row in set (0.00 sec)

mysql> CALL ordertotal(20009, @total);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @total;
+--------+
| @total |
+--------+
|  38.47 |
+--------+

mysql> DROP PROCEDURE ordertotal;
Query OK, 0 rows affected (0.00 sec)

mysql> -- Name; order total
mysql> -- Parameters: onumber = order number
mysql> --             taxable = 0 if not taxable, 1 if taxable
mysql> --             ototal  = order total variable
mysql>
mysql> DELIMITER //
mysql> 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
    ->    -- Yes, so add taxrate to the total
    ->    SELECT total + (total / 100 * taxrate) INTO total;
    -> END IF;
    ->    -- And finally, save to out variable
    ->    SELECT total INTO ototal;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL ordertotal(20005, 0, @total);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @total;
+--------+
| @total |
+--------+
| 149.87 |
+--------+

mysql> CALL ordertotal(20005, 1, @total);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> SELECT @total;
+--------+
| @total |
+--------+
| 158.86 |
+--------+
SHOW CREATE PROCEDURE ordertotal;
+------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Procedure  | sql_mode                                   | Create Procedure                                                                                                                                                                                                                                                                                                                                                                                                                                                      | character_set_client | collation_connection | Database Collation |
+------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| ordertotal | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` PROCEDURE `ordertotal`(
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8,2)
)
    COMMENT ‘Obtain order total, optionally adding tax‘
BEGIN

DECLARE total DECIMAL(8,2);

DECLARE taxrate INT DEFAULT 6;

SELECT SUM(item_price * quantity)
FROM orderitems
WHERE order_num = onumber
INTO total;

IF taxable THEN

   SELECT total + (total / 100 * taxrate) INTO total;
END IF;

   SELECT total INTO ototal;
END | utf8                 | utf8_general_ci      | utf8_unicode_ci    |
+------------+--------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

mysql> SHOW PROCEDURE STATUS LIKE ‘ordertotal‘;
+------------+------------+-----------+----------------+---------------------+---------------------+---------------+-------------------------------------------+----------------------+----------------------+--------------------+
| Db         | Name       | Type      | Definer        | Modified            | Created             | Security_type | Comment                                   | character_set_client | collation_connection | Database Collation |
+------------+------------+-----------+----------------+---------------------+---------------------+---------------+-------------------------------------------+----------------------+----------------------+--------------------+
| learnmysql | ordertotal | PROCEDURE | root@localhost | 2015-06-09 11:42:48 | 2015-06-09 11:42:48 | DEFINER       | Obtain order total, optionally adding tax | utf8                 | utf8_general_ci      | utf8_unicode_ci    |
+------------+------------+-----------+----------------+---------------------+---------------------+---------------+-------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.01 sec)
时间: 2024-11-14 12:20:10

MySQL.PROCEDURE.使用存储过程的相关文章

八、mysql视图、存储过程、函数以及时间调度器

1.create or replace view emp_view as select * from t4 ;给t4表创建一个名为emp_view的视图 2.drop view emp_view 删除视图 ======================================= 1.创建一个存储过程(查询所有数据) create procedure p1 () READS SQL DATA BEGIN select * from t4; END 2.创建一个存储过程(查询传参数据) cre

MySql中创建存储过程

MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库时(例如 PHP),要组织很多 SQL 语句.特别是业务逻辑复杂的时候,一大堆的 SQL 和条件夹杂在 PHP 代码中,让人不寒而栗.现在有了 MySQL 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高. 一.MySQL 创建存储过程 "pr_add" 是个简单的

JAVA如何调用mysql写的存储过程

存储过程是干什么的,自己百度,百度上讲的比我讲的好.为什么要用存储过程,这样可以提高效率.废话少说,直接上代码: 首先说一下语法:在mysql中写存储过程 DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ PROCEDURE `test`.`text_d`(in param1 varchar(10),in param2 varchar(20)) /*LANGUAGE SQL | [NOT] DETERMINISTIC | {

MySQL数据库的存储过程(一)

MySQL数据库的存储过程常规create routinealterexecutesecuritydefiner 在创建存储过程的时候默认是选中相应的数据库(use db_name)所以在编写存储过程时候就不能在存储过程之中编写此语句 delimiter客户端 使用delimiter客户端设置结束符delimiter //主要是为了避免与存储过程中的SQL语句的结束符冲突 创建一个程序或者一个函数create procedure | function 学习存储过程首先我们应该学会最基本的几个操作

jdbc 处理mysql procedure返回的多个结果集

1:测试数据库表user mysql> desc user$$ +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_in

mysql 5.0存储过程学习总结

mysql存储过程的创建,删除,调用及其他常用命令 本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29LoD19)  mysql 5.0存储过程学习总结 一.创建存储过程 1.基本语法: create procedure sp_name()begin………end 2.参数传递 二.调用存储过程 1.基本语法:call sp_name()注意:存储过程名称后面必

mysql procedure返回多数据集

返回多数据集写法:第一种 DROP PROCEDURE IF EXISTS `p_query_user` ; DELIMITER // CREATE PROCEDURE p_query_user( p_inout_user_id INT ) begin declare c varchar(100) default '0'; SELECT * FROM T_SD_USER WHERE USER_ID = p_inout_user_id ; SET p_inout_user_id =2222; en

mysql触发器与存储过程

外键 外键:foreign key,(键不在自己表中):如果一张表中有一个字段(非主键)指向另外一张表的主键,那么将该字段称之为外键. 增加外键 外键可以在创建表时候,或者创建表之后增加.(但是要考虑数据的问题),一张表可以有多个外键. 创建表的时候增加外键 -- 在所有的表字段之后,使用foreign key(外键字段) references 外部表(主键字段) 下面为之前的my_class表(表结构如下图)增加一个外键 create table my_foreignone( id int p

查询MySQL中的存储过程和函数

select name from mysql.proc where db = '' and `type` = 'PROCEDURE' //查询存储过程 select name from mysql.proc where db = '' and `type` = 'FUNCTION' //查询函数 select name,body_utf8 from mysql.proc where db = '' and `type` = 'PROCEDURE' and name='';