MySQL存储过程

2018-08-15  23:00:06

1、存储过程介绍


(1) 定义:存储过程是存储在数据库目录中的一段声明性SQL语句。 触发器,其他存储过程以及java,python,php等应用程序可以调用存储过程。

递归存储过程:自身的存储过程。大多数数据库管理系统支持递归存储过程。 但是,MySQL不支持它。

(2)优点:

1️⃣通常存储过程有助于提高应用程序的性能。当创建,存储过程被编译之后,就存储在数据库中。 但是,MySQL实现的存储过程略有不同。 MySQL存储过程按需编译。 在编译存储过程之后,MySQL将其放入缓存中。 MySQL为每个连接维护自己的存储过程高速缓存。 如果应用程序在单个连接中多次使用存储过程,则使用编译版本,否则存储过程的工作方式类似于查询。

2️⃣存储过程有助于减少应用程序和数据库服务器之间的流量,因为应用程序不必发送多个冗长的SQL语句,而只能发送存储过程的名称和参数。

3️⃣存储的程序对任何应用程序都是可重用的和透明的。 存储过程将数据库接口暴露给所有应用程序,以便开发人员不必开发存储过程中已支持的功能。

4️⃣存储的程序是安全的。 数据库管理员可以向访问数据库中存储过程的应用程序授予适当的权限,而不向基础数据库表提供任何权限。

(3)缺点:

1️⃣如果使用大量存储过程,那么使用这些存储过程的每个连接的内存使用量将会大大增加。 此外,如果您在存储过程中过度使用大量逻辑操作,则CPU使用率也会增加,因为数据库服务器的设计不当于逻辑运算。

2️⃣存储过程的构造使得开发具有复杂业务逻辑的存储过程变得更加困难。

3️⃣很难调试存储过程。

4️⃣开发和维护存储过程并不容易。

2、存储过程入门


Navicat命令行下创建第一个存储过程HelloProcedure(),也可以直接使用navicat中的图形化界面直接创建,也可以用MySQL Workbench图形化工具创建。

mysql> delimiter //  ---将分隔符;改为//
mysql> drop procedure if exists HelloProcedure; ---不要括弧
-> //
Query OK, rows affected mysql> create procedure HelloProcedure()
-> begin
-> select * from products;
-> end//
Query OK, rows affected mysql> delimiter ; ---将分隔符改回来
mysql> call HelloProcedure(); ---调用存储过程,要带括弧!

第一个命令是DELIMITER //,它与存储过程语法无关。 DELIMITER语句将标准分隔符 - 分号(;)更改为://。 在这种情况下,分隔符从分号(;)更改为双斜杠//

为什么我们必须更改分隔符? 因为我们想将存储过程作为整体传递给服务器,而不是让mysql工具一次解释每个语句。 在END关键字之后,使用分隔符//来指示存储过程的结束。 最后一个命令(DELIMITER;)将分隔符更改回分号(;)。

3、存储过程变量


声明变量:
declare x int default ;
declare x, y int default ; ---可以同时声明多个变量 分配变量值:
declare total int default ;
set total = ; ---分配给total的值为10 declare total int default ;
select count(*) into total
from products; ---将sql语句的查询结果分配给total 变量范围(作用域):
在存储过程中声明一个变量,那么当达到存储过程的END语句时,它将超出范围,因此在其它代码块中无法访问。
以@符号开头的变量是会话变量。直到会话结束前它可用和可访问。

4、存储过程参数


参数有三种:

  • IN---是默认模式。在存储过程中定义IN参数时,调用程序必须将参数传递给存储过程。 另外,IN参数的值被保护。这意味着即使在存储过程中更改了IN参数的值,在存储过程结束后仍保留其原始值。
mysql> drop procedure if exists GetOfficeByCountry;
Query OK, rows affected mysql> delimiter //
mysql> create procedure GetOfficeByCountry(in countryName varchar(255))
-> begin
-> select *
-> from offices
-> where country = countryName;
-> end //
Query OK, rows affected mysql> delimiter ;
mysql> call GetOfficeByCountry('USA');
+------------+---------------+-----------------+----------------------+--------------+-------+---------+------------+-----------+
| officeCode | city | phone | addressLine1 | addressLine2 | state | country | postalCode | territory |
+------------+---------------+-----------------+----------------------+--------------+-------+---------+------------+-----------+
| | San Francisco | + | Market Street | Suite | CA | USA | | NA |
| | Boston | + | Court Place | Suite | MA | USA | | NA |
| | NYC | + | East 53rd Street | apt. 5A | NY | USA | | NA |
+------------+---------------+-----------------+----------------------+--------------+-------+---------+------------+-----------+
rows in set Query OK, rows affected mysql> drop procedure if exists CountOrderByStatus;
Query OK, rows affected
  • OUT---可以在存储过程中更改OUT参数的值,并将其更改后新值传递回调用程序。请注意,存储过程在启动时无法访问OUT参数的初始值。
mysql> drop procedure if exists CountOrderByStatus;
Query OK, rows affected mysql> delimiter //
mysql> create procedure CountOrderByStatus(in orderStatus varchar(),out total int)
-> begin
-> select count(orderNumber)
-> into total
-> from orders
-> where status = orderStatus;
-> end //
Query OK, rows affected mysql> delimiter ;
mysql> call CountOrderByStatus('Shipped',@total);
Query OK, row affected mysql> select @total;
+--------+
| @total |
+--------+
| |
+--------+
row in set
  • INOUT---INOUT参数是INOUT参数的组合。这意味着调用程序可以传递参数,并且存储过程可以修改INOUT参数并将新值传递回调用程序
mysql> drop procedure if exists set_counter;
Query OK, rows affected mysql> delimiter //
mysql> create procedure set_counter(inout count int(4),in inc int())
-> begin
-> set count = count + inc;
-> end //
Query OK, rows affected mysql> delimiter ;
mysql> set @counter = 1;
Query OK, rows affected mysql> call set_counter(@counter,1); ---2
Query OK, rows affected mysql> call set_counter(@counter,1); ---3
Query OK, rows affected mysql> call set_counter(@counter,5); ---8
Query OK, rows affected mysql> select @counter;
+----------+
| @counter |
+----------+
| |
+----------+
row in set

5、返回多个值的存储过程


DELIMITER $$

CREATE PROCEDURE get_order_by_cust(
IN cust_no INT,
OUT shipped INT,
OUT canceled INT,
OUT resolved INT,
OUT disputed INT
)
BEGIN
-- shipped
SELECT
count(*) INTO shipped
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Shipped'; -- canceled
SELECT
count(*) INTO canceled
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Canceled'; -- resolved
SELECT
count(*) INTO resolved
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Resolved'; -- disputed
SELECT
count(*) INTO disputed
FROM
orders
WHERE
customerNumber = cust_no
AND status = 'Disputed'; END

6、IF语句(if  then  else end if;和 if then elseif then else end if;)


mysql> delimiter //
mysql> create procedure GetCustomerLevel(in p_cusNum int(),out p_cusLevel varchar())
-> begin
-> declare creditlim double;
->
-> select creditlimit into creditlim
-> from customers
-> where customerNumber = p_cusNum;
->
-> if creditlim > then
->   set p_cusLevel = 'PLATINUM';
-> elseif (creditlim <= and creditlim >= ) then
->   set p_cusLevel = 'GOLD';
-> elseif creditlim < then
->   set p_cusLevel = 'SILVER';
-> end if;
-> end //
Query OK, rows affected mysql> call GetCustomerLevel(,@p_cusLevel);
-> //
Query OK, row affected mysql> delimiter ;
mysql> select @p_cusLevel;
+-------------+
| @p_cusLevel |
+-------------+
| GOLD |
+-------------+
row in set

7、CASE语句


 case  when  ...    then  ...

     when  ...    then  ...

     else  ...

 end case;

注意:在select查询语句中,可以直接在end 后面加上字段别名!

 case  when  ...    then  ...

     when  ...    then  ...

     else  ...

 end 字段别名;  (或者end as 字段别名

mysql> delimiter //
mysql> create procedure GetCusShipping(in p_cusNum int(),out p_shipping varchar())
-> begin
-> declare cusCountry varchar();
->
-> select country into cusCountry
-> from customers
-> where customerNumber = p_cusNum;
->
-> case cusCountry
-> when 'USA' then set p_shipping = '2';
-> when 'Canada' then set p_shipping = '3';
-> else
-> set p_shipping = '5';
-> end case;
-> end //
Query OK, rows affected mysql> delimiter ;
mysql> set @p_cusNum = ;
Query OK, rows affected mysql> select country into @country
-> from customers
-> where customerNumber = @p_cusNum;
Query OK, row affected mysql> call GetCusShipping(@p_cusNum,@shipping);
Query OK, row affected mysql> select @p_cusNum as Customer,
-> @country as Country,
-> @shipping as Shipping;
+----------+---------+----------+
| Customer | Country | Shipping |
+----------+---------+----------+
| | USA | 2 |
+----------+---------+----------+
row in set
 

注意:可以直接在navicatl里面创建存储过程,创建的时候选择参数类型的时候记得加上具体的范围保存的时候只用写上存储过程的名称,不用带括号

8、IF和CASE语句使用技巧


IF语句和CASE语句的时候,看个人喜好。

IF判断嵌套少的话,使用IF更方便,但是IF判断嵌套太多的话,建议使用CASE更加简单易读!

9、循环


while...do...end while;

begin

    declare x int;

    declare str varchar();

    set x = ;

    set str = '';

    while x <= 5 do

    set str = concat(str,x,',');

    set x = x + 1;

    end while;

    select str;

end

mysql> call test_mysql_while_loop();
+------------+
| str |
+------------+
| 1,2,3,4,5, |
+------------+
1 row in set


Query OK, 0 rows affected

 

repeat...until...end repeat;

begin

    declare x int;

    declare str varchar();

    set x = ;

    set str = '';

    repeat

    set str = concat(str,x,',');

    set x = x + 1;

    until x > 5

    end repeat;

    select str;

end

mysql> call test_mysql_repeat_loop();
+------------+
| str |
+------------+
| 1,2,3,4,5, |
+------------+
1 row in set


Query OK, 0 rows affected

 

loop...leave...iterate...;  leave相当于break,iterate相当于continue.

begin

    declare x int;

    declare str varchar();

    set x = ;

    set str = '';

    loop_label: loop

    if x > 10 then

        leave loop_label;

    end if;

    set x = x + 1;

    if(x mod 2) then

        iterate loop_label;

    else

        set str = concat(str,x,',');

    end if;

    end loop;

    select str;

end

mysql> call test_mysql_loop();
+-------------+
| str |
+-------------+
| 2,4,6,8,10, |
+-------------+
1 row in set


Query OK, 0 rows affected

 

10、游标


要处理存储过程中的结果集,请使用游标。游标允许您迭代查询返回的一组行,并相应地处理每行。

MySQL游标为只读(无法通过光标更新基础表中的数据),不可滚动(只能按照select语句的顺序获取行)和敏感(敏感指向实际数据,不敏感指向数据副本,敏感比不敏感执行的快)。

可以在存储过程存储函数触发器中使用MySQL游标。

BEGIN

 DECLARE v_finished INTEGER DEFAULT ;
DECLARE v_email varchar() DEFAULT ""; -- 、declare cursor for employee email
DEClARE email_cursor CURSOR FOR
SELECT email FROM employees; -- 、declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = ; -- 、open
OPEN email_cursor; get_email: LOOP -- 、fetch
FETCH email_cursor INTO v_email; -- 、not found
IF v_finished = THEN
LEAVE get_email;
END IF; -- 、build email list
SET email_list = CONCAT(v_email,";",email_list); END LOOP get_email; --
、close
CLOSE email_cursor;
END mysql> set @email_list = "";
Query OK, rows affected mysql> call build_email_list(@email_list);
Query OK, rows affected mysql> select @email_list;
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| @email_list |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| mgerard@gmail.com;ykato@gmail.com;mnishi@gmail.com;tking@gmail.com;pmarsh@yiibai.com;afixter@yiibai.com;bjones@gmail.com;lbott@yiibai.com;pcastillo@gmail.com;ghernande@gmail.com;lbondur@yiibai.com;gvanauf@yiibai.com;ftseng@yiibai.com;spatterson@yiibai.com;jfirrelli@yiibai.com;lthompson@yiibai.com;ljennings@yiibai.com;abow@gmail.com;gbondur@gmail.com;wpatterson@yiibai.com;jfirrelli@yiibai.com;mpatterso@yiibai.com;dmurphy@yiibai.com; |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set

11、在数据库中列出存储过程


mysql> show procedure status;---列出有权访问的数据库的所有存储过程
+----------+--------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+----------+--------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| mydb2 | GetAllByUsername | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| mydb2 | GetAllUser | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| mydb2 | GetAllUser2 | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | AddOrderItem | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | build_email_list | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | CountOrderByStatus | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | Divide | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetCusShipping | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | GetCustomerLevel | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetCustomerLevel2 | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetCustomerShipping | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetOfficeByCountry | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | get_order_by_cust | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | HelloProcedure | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | insert_article_tags | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | insert_article_tags_exit | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | set_counter | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | test_mysql_loop | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | test_mysql_repeat_loop | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | test_mysql_while_loop | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
+----------+--------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
rows in set mysql> show procedure status where db = 'yiibaidb';---列出指定数据库的所有存储过程
+----------+--------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+----------+--------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| yiibaidb | AddOrderItem | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | build_email_list | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | CountOrderByStatus | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | Divide | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetCusShipping | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | GetCustomerLevel | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetCustomerLevel2 | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetCustomerShipping | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | GetOfficeByCountry | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | get_order_by_cust | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | HelloProcedure | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | insert_article_tags | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | insert_article_tags_exit | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | set_counter | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8 | utf8_general_ci | utf8_general_ci |
| yiibaidb | test_mysql_loop | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | test_mysql_repeat_loop | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
| yiibaidb | test_mysql_while_loop | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
+----------+--------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
rows in set mysql> show procedure status where name like '%product%';
Empty set mysql> show procedure status where name like '%email%';---列出指定名称的存储过程
+----------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+----------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| yiibaidb | build_email_list | PROCEDURE | root@localhost | -- :: | -- :: | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
+----------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
row in set mysql> show create procedure build_email_list;---列出存储过程的源代码
+------------------+----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation |
+------------------+----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| build_email_list | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` PROCEDURE `build_email_list`(INOUT email_list varchar())
BEGIN DECLARE v_finished INTEGER DEFAULT ;
DECLARE v_email varchar() DEFAULT ""; -- 、declare cursor for employee email
DEClARE email_cursor CURSOR FOR
SELECT email FROM employees; -- 、declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = ; -- 、open
OPEN email_cursor; get_email: LOOP -- 、fetch
FETCH email_cursor INTO v_email; -- 、not found
IF v_finished = THEN
LEAVE get_email;
END IF; -- 、build email list
SET email_list = CONCAT(v_email,";",email_list); END LOOP get_email; -- 、close
CLOSE email_cursor; END | utf8mb4 | utf8mb4_general_ci | utf8_general_ci |
+------------------+----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
row in set

12、存储过程中的MySQL错误处理


BEGIN
#Routine body goes here...
DECLARE EXIT HANDLER FOR 1062 SELECT 'Duplicate keys error encountered';
DECLARE EXIT HANDLER FOR SQLEXCEPTION SELECT 'SQLException encountered';
DECLARE EXIT HANDLER FOR SQLSTATE '23000' SELECT 'SQLSTATE 23000'
; INSERT INTO article_tags(article_id,tag_id)
VALUES(article_id,tag_id); SELECT COUNT(*) FROM article_tags; END 插入重复的值得时候会报错!处理程序优先级:每一个mysql错误都对应一个错误代码(例如1062),所以处理错误优先级是错误越具体,优先级越高

mysql> call insert_article_tags_3(1,3);
+----------------------------------+
| Duplicate keys error encountered |
+----------------------------------+
| Duplicate keys error encountered |
+----------------------------------+
1 row in set


Query OK, 0 rows affected

 
使用命名错误条件:
也就是给错误代码1051取个好理解的名字,这样发生对应的错误的时候,知道具体是什么错误。
DECLARE table_not_found CONDITION for ;
DECLARE EXIT HANDLER FOR table_not_found SELECT 'Please create table abc first';
SELECT * FROM abc;

13、使用MySQL SIGNAL/RESIGNAL语句引发错误条件


DELIMITER $$

CREATE PROCEDURE AddOrderItem(in orderNo int,
in productCode varchar(),
in qty int,in price double, in lineNo int ) BEGIN
DECLARE C INT; SELECT COUNT(orderNumber) INTO C
FROM orders
WHERE orderNumber = orderNo; -- check if orderNumber exists
IF(C != ) THEN
SIGNAL SQLSTATE ''
SET MESSAGE_TEXT = 'Order No not found in orders table';
END IF;
-- more code below
-- ...
END $$
DELIMITER ;
DELIMITER $$

CREATE PROCEDURE Divide(IN numerator INT, IN denominator INT, OUT result double)
BEGIN
DECLARE division_by_zero CONDITION FOR SQLSTATE ''; DECLARE CONTINUE HANDLER FOR division_by_zero
RESIGNAL SET MESSAGE_TEXT = 'Division by zero / Denominator cannot be zero';
--
IF denominator = THEN
SIGNAL division_by_zero;
ELSE
SET result := numerator / denominator;
END IF;
END $$
DELIMITER ; 

14、存储函数


存储的函数是返回单个值的特殊类型的存储程序。您使用存储的函数来封装在SQL语句或存储的程序中可重用的常用公式或业务规则。

可以在sql语句中和存储过程中调用存储函数,这样有助于代码的可读性和可维护性。

BEGIN
DECLARE lvl varchar(); IF p_creditLimit > THEN
SET lvl = 'PLATINUM';
ELSEIF (p_creditLimit <= AND p_creditLimit >= ) THEN
SET lvl = 'GOLD';
ELSEIF p_creditLimit < THEN
SET lvl = 'SILVER';
END IF; RETURN (lvl);
END

mysql> select customerName,CustomerLevel(creditLimit)
-> from customers
-> order by customerName;
+------------------------------------+----------------------------+
| customerName | CustomerLevel(creditLimit) |
+------------------------------------+----------------------------+
| Alpha Cognac | PLATINUM |
| American Souvenirs Inc | SILVER |
| Amica Models & Co. | PLATINUM |
| ANG Resellers | SILVER |
| Anna's Decorations, Ltd | PLATINUM |
| Anton Designs, Ltd. | SILVER |
| Asian Shopping Network, Co | SILVER |
| Asian Treasures, Inc. | SILVER |
| Atelier graphique | GOLD |
| Australian Collectables, Ltd | PLATINUM |
| Australian Collectors, Co. | PLATINUM |
| Australian Gift Network, Co | PLATINUM |
| Auto Associs & Cie. | PLATINUM |
| Auto Canal+ Petit | PLATINUM |
| Auto-Moto Classics Inc. | GOLD |
| AV Stores, Co. | PLATINUM |
| Baane Mini Imports | PLATINUM |
| Bavarian Collectables Imports, Co. | PLATINUM |
| BG&E Collectables | SILVER |
| Blauer See Auto, Co. | PLATINUM |
| Boards & Toys Co. | GOLD |
| CAF Imports | PLATINUM |
| Cambridge Collectables Co. | GOLD |
| Canadian Gift Exchange Network | PLATINUM |
| Classic Gift Ideas, Inc | PLATINUM |
| Classic Legends Inc. | PLATINUM |
| Clover Collections, Co. | PLATINUM |
| Collectable Mini Designs Co. | PLATINUM |
| Collectables For Less Inc. | PLATINUM |
| Corporate Gift Ideas Co. | PLATINUM |
| Corrida Auto Replicas, Ltd | PLATINUM |
| Cramer Spezialitten, Ltd | SILVER |
| Cruz & Sons Co. | PLATINUM |
| Daedalus Designs Imports | PLATINUM |
| Danish Wholesale Imports | PLATINUM |
| Der Hund Imports | SILVER |
| Diecast Classics Inc. | PLATINUM |
| Diecast Collectables | PLATINUM |
| Double Decker Gift Stores, Ltd | GOLD |
| Down Under Souveniers, Inc | PLATINUM |
| Dragon Souveniers, Ltd. | PLATINUM |
| Enaco Distributors | PLATINUM |
| Euro+ Shopping Channel | PLATINUM |
| Extreme Desk Decorations, Ltd | PLATINUM |
| Feuer Online Stores, Inc | SILVER |
| Franken Gifts, Co | SILVER |
| Frau da Collezione | GOLD |
| FunGiftIdeas.com | PLATINUM |
| Gift Depot Inc. | PLATINUM |
| Gift Ideas Corp. | GOLD |
| Gifts4AllAges.com | GOLD |
| giftsbymail.co.uk | PLATINUM |
| GiftsForHim.com | PLATINUM |
| Handji Gifts& Co | PLATINUM |
| Havel & Zbyszek Co | SILVER |
| Heintze Collectables | PLATINUM |
| Herkku Gifts | PLATINUM |
| Iberia Gift Imports, Corp. | PLATINUM |
| Kelly's Gift Shop | PLATINUM |
| King Kong Collectables, Co. | PLATINUM |
| Kommission Auto | SILVER |
| Kremlin Collectables, Co. | SILVER |
| L'ordine Souveniers | PLATINUM |
| La Corne D'abondance, Co. | PLATINUM |
| La Rochelle Gifts | PLATINUM |
| Land of Toys Inc. | PLATINUM |
| Lisboa Souveniers, Inc | SILVER |
| Lyon Souveniers | PLATINUM |
| Marseille Mini Autos | PLATINUM |
| Marta's Replicas Co. | PLATINUM |
| Men 'R' US Retailers, Ltd. | PLATINUM |
| Messner Shopping Network | SILVER |
| Microscale Inc. | GOLD |
| Mini Auto Werke | GOLD |
| Mini Caravy | PLATINUM |
| Mini Classics | PLATINUM |
| Mini Creations Ltd. | PLATINUM |
| Mini Gifts Distributors Ltd. | PLATINUM |
| Mini Wheels Co. | PLATINUM |
| Mit Vergngen & Co. | SILVER |
| Motor Mint Distributors Inc. | PLATINUM |
| Muscle Machine Inc | PLATINUM |
| Natrlich Autos | SILVER |
| Norway Gifts By Mail, Co. | PLATINUM |
| Online Diecast Creations Co. | PLATINUM |
| Online Mini Collectables | PLATINUM |
| Osaka Souveniers Co. | PLATINUM |
| Oulu Toy Supplies, Inc. | PLATINUM |
| Petit Auto | PLATINUM |
| Porto Imports Co. | SILVER |
| Precious Collectables | SILVER |
| Qubec Home Shopping Network | GOLD |
| Raanan Stores, Inc | SILVER |
| Reims Collectables | PLATINUM |
| Rovelli Gifts | PLATINUM |
| Royal Canadian Collectables, Ltd. | PLATINUM |
| Royale Belge | GOLD |
| Salzburg Collectables | PLATINUM |
| SAR Distributors, Co | SILVER |
| Saveley & Henriot, Co. | PLATINUM |
| Scandinavian Gift Ideas | PLATINUM |
| Schuyler Imports | SILVER |
| Signal Collectibles Ltd. | PLATINUM |
| Signal Gift Stores | PLATINUM |
| Souveniers And Things Co. | PLATINUM |
| Stuttgart Collectable Exchange | SILVER |
| Stylish Desk Decors, Co. | PLATINUM |
| Suominen Souveniers | PLATINUM |
| Super Scale Inc. | PLATINUM |
| Technics Stores Inc. | PLATINUM |
| Tekni Collectables Inc. | GOLD |
| The Sharp Gifts Warehouse | PLATINUM |
| Tokyo Collectables, Ltd | PLATINUM |
| Toms Spezialitten, Ltd | PLATINUM |
| Toys of Finland, Co. | PLATINUM |
| Toys4GrownUps.com | PLATINUM |
| UK Collectables, Ltd. | PLATINUM |
| Vida Sport, Ltd | PLATINUM |
| Vitachrome Inc. | PLATINUM |
| Volvo Model Replicas, Co | PLATINUM |
| Warburg Exchange | SILVER |
| West Coast Collectables Co. | PLATINUM |
+------------------------------------+----------------------------+
rows in set

MySQL存储过程整理的更多相关文章

  1. Mysql基本操作整理

    1.登录数据库 格式: mysql -h主机地址 -u用户名 -p用户密码 –P端口 –D数据库 –e “SQL 内容”>mysql -uroot -p 数据库名称~~~~~~~~~~~~~~~ ...

  2. 最全mysql笔记整理

    mysql笔记整理 作者:python技术人 博客:https://www.cnblogs.com/lpdeboke Windows服务 -- 启动MySQL net start mysql -- 创 ...

  3. MySQL存储过程(转)

    一.MySQL 创建存储过程 "pr_add" 是个简单的 MySQL 存储过程,这个存储过程有两个 int 类型的输入参数 "a"."b" ...

  4. MySql存储过程

    MySQL 存储过程 ```sql CREATE PROCEDURE myprocedure (IN para01 INTEGER) BEGIN DECLARE var01 CHAR(10); IF ...

  5. mysql存储过程和存储函数

    mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...

  6. mysql存储过程编写-入门案例-遁地龙卷风

    (-1)写在前面 这篇文章只是简要的叙述了mysql存储过程编写的基本概念. 我使用的mysql版本是5.7.9-log. 参照<<深入浅出MySQL>>. (0) delim ...

  7. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  8. MySQL 存储过程

    MySQL 存储过程 存储过程是通过给定的语法格式编写自定义的数据库API,类似于给数据库编写可执行函数. 简介 存储过程是一组为了完成特定功能的SQL语句集合,是经过编译后存储在数据库中. 存储过程 ...

  9. mysql存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

随机推荐

  1. C#自绘蒙版控件,带延时隐藏显示,拷贝底图功能

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  2. <hr />改变颜色

    其实,<hr />是有默认的border的,改变border颜色即可改变hr颜色. 例如: <hr style="position: relative;top: 2.3re ...

  3. yolov3实践(一)

    很多博友看了我的第一篇博客yolo类检测算法解析——yolo v3,对其有了一定的认识和了解,但是并没有贴出代码和运行效果,略显苍白.因此在把篇博客理论的基础上,造就了第一篇实践文章,也就是本文.只要 ...

  4. jira7.3.6添加导出excel的按钮

    在 jira的application-data 文件夹下 jira-config.properties 文件(如果没有就创建一个) 中添加以下内容:jira.export.excel.enabled= ...

  5. windows钩子函数

    一 什么时候用到钩子?(when)Windows操作系统是建立在事件驱动的消息处理机制之上,系统各部分之间的沟通也都是通过消息的相互传递而实现的.通常情况下,应用程序只能处理当前进程的消息,如果需要对 ...

  6. 【vue报错】——listen EADDRINUSE :::8080 解决方案

    题原因: 此项错误表示 8080 端口被占用 解决方案一: 打开cmd输入:netstat -ano|findstr "8080"查看所有端口信息,并通过findstr “8080 ...

  7. 教你如何使用android studio发布release 版本【转】

    原文链接 想必还有人对如何在Android studio (以下简称as)发布release版本的app而狂刷百度吧?都是过来人,我很理解这种心情,百度到的基本是半成品,为什么这么说呢?百度一下,你就 ...

  8. linux 中的 open() read() write() close() 函数

    1. open()函数 功能描述:用于打开或创建文件,在打开或创建文件时可以指定文件的属性及用户的权限等各种参数. 所需头文件:#include <sys/types.h>,#includ ...

  9. RPC原理

    RPC同步调用流程:(异步另说) 1)服务消费方(Client)以本地的调用方式调用远程服务. 2)客户端代理(Client Stub)接收到调用后负责将方法.参数等组装成能够进行网络传输的消息体. ...

  10. Linq基于两个属性的分组

    1.需求 我们看下面的定义 #region 学生类 /// <summary> /// 学生类 /// </summary> class Student { /// <s ...