“pr_add” 是个简单的 MySQL 存储过程,这个存储过程有两个 int 类型的输入參数 “a”、“b”,返回这两个參数的和。
(
a int,
b int
)
begin
declare c int;
set a = 0;
end if;
set b = 0;
end if;
return c;- 不能在 MySQL 存储过程中使用。return 仅仅能出如今函数中。
/
end;
二、调用 MySQL 存储过程
call pr_add(10, 20);
运行 MySQL 存储过程,存储过程參数为 MySQL 用户变量。
set @b = 20;
三、MySQL 存储过程特点
创建 MySQL 存储过程的简单语法为:
(
[in|out|inout] 參数 datatype
)
begin
MySQL 语句;
end;
MySQL 存储过程參数假设不显式指定“in”、“out”、“inout”,则默觉得“in”。习惯上,对于是“in” 的參数,我们都不会显式指定。
(
@a int,- 错误
b int - 正确
)
3. MySQL 存储过程的參数不能指定默认值。
(
a int,
b int
)
as - 错误,MySQL 不须要 “as”
begin
mysql statement ...;
end;
5. 假设 MySQL 存储过程中包括多条 MySQL 语句,则须要 begin end keyword。
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;
6. MySQL 存储过程中的每条语句的末尾,都要加上分号 “;”
set a = 0;
end if;
end;
7. MySQL 存储过程中的凝视。
这是个
多行 MySQL 凝视。
/
set a = 0;
end if;
end;
8. 不能在 MySQL 存储过程中使用 “return” keyword。
return c;- 不能在 MySQL 存储过程中使用。return 仅仅能出如今函数中。
/
end;
9. 调用 MySQL 存储过程时候,须要在过程名字后面加“()”,即使没有一个參数,也须要“()”
10. 由于 MySQL 存储过程參数没有默认值,所以在调用 MySQL 存储过程时候,不能省略參数。能够用 null 来替代。
须要MySQL 5
2,Hello World
MySQL存储过程之Hello World
- DELIMITER $$
- DROP PROCEDURE IF EXISTS HelloWorld$$
- CREATE PROCEDURE HelloWorld()
- BEGIN
- SELECT "Hello World!";
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXISTS HelloWorld$$
CREATE PROCEDURE HelloWorld()
BEGIN
SELECT "Hello World!";
END$$ DELIMITER ;
3,变量
使用DECLARE来声明,DEFAULT赋默认值,SET赋值
- DECLARE counter INT DEFAULT ;
- SET counter = counter+;
DECLARE counter INT DEFAULT 0;
SET counter = counter+1;
4,參数
IN为默认类型,值必须在调用时指定,值不能返回(值传递)
OUT值能够返回(指针传递)
INOUT值必须在调用时指定,值能够返回
- CREATE PROCEDURE test(a INT, OUT b FLOAT, INOUT c INT)
CREATE PROCEDURE test(a INT, OUT b FLOAT, INOUT c INT)
5,条件推断
IF THEN、ELSEIF、ELSE、END IF
- DELIMITER $$
- DROP PROCEDURE IF EXISTS discounted_price$$
- CREATE PROCEDURE discunted_price(normal_price NUMERIC(, ), OUT discount_price NUMERIC(, ))
- BEGIN
- IF (normal_price > ) THEN
- SET discount_price = normal_price * .;
- ELSEIF (normal_price > ) THEN
- SET discount_price = normal_price * .;
- ELSE
- SET discount_price = normal_price;
- END IF;
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXISTS discounted_price$$
CREATE PROCEDURE discunted_price(normal_price NUMERIC(8, 2), OUT discount_price NUMERIC(8, 2))
BEGIN
IF (normal_price > 500) THEN
SET discount_price = normal_price * .8;
ELSEIF (normal_price > 100) THEN
SET discount_price = normal_price * .9;
ELSE
SET discount_price = normal_price;
END IF;
END$$ DELIMITER ;
6,循环
LOOP、END LOOP
- DELIMITER $$
- DROP PROCEDURE IF EXISTS simple_loop$$
- CREATE PROCEDURE simple_loop(OUT counter INT)
- BEGIN
- SET counter = ;
- my_simple_loop: LOOP
- SET counter = counter+;
- IF counter = THEN
- LEAVE my_simple_loop;
- END IF;
- END LOOP my_simple_loop;
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXISTS simple_loop$$ CREATE PROCEDURE simple_loop(OUT counter INT)
BEGIN
SET counter = 0;
my_simple_loop: LOOP
SET counter = counter+1;
IF counter = 10 THEN
LEAVE my_simple_loop;
END IF;
END LOOP my_simple_loop;
END$$ DELIMITER ;
WHILE DO、END WHILE
- DELIMITER $$
- DROP PROCEDURE IF EXISTS simple_while$$
- CREATE PROCEDURE simple_while(OUT counter INT)
- BEGIN
- SET counter = ;
- WHILE counter != DO
- SET counter = counter+;
- END WHILE;
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXISTS simple_while$$ CREATE PROCEDURE simple_while(OUT counter INT)
BEGIN
SET counter = 0;
WHILE counter != 10 DO
SET counter = counter+1;
END WHILE;
END$$ DELIMITER ;
REPEAT、UNTILL
- DELIMITER $$
- DROP PROCEDURE IF EXISTS simple_repeat$$
- CREATE PROCEDURE simple_repeat(OUT counter INT)
- BEGIN
- SET counter = ;
- REPEAT
- SET counter = counter+;
- UNTIL counter = END REPEAT;
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXISTS simple_repeat$$ CREATE PROCEDURE simple_repeat(OUT counter INT)
BEGIN
SET counter = 0;
REPEAT
SET counter = counter+1;
UNTIL counter = 10 END REPEAT;
END$$ DELIMITER ;
7,异常处理
假设用cursor获取SELECT语句返回的全部结果集时应该定义NOT FOUND error handler来防止存储程序提前终结
假设SQL语句可能返回constraint violation等错误时应该创建一个handler来防止程序终结
8,数据库交互
INTO用于存储单行记录的查询结果
- DECLARE total_sales NUMERIC(, );
- SELECT SUM(sale_value) INTO total_sales FROM sales WHERE customer_id=in_customer_id;
DECLARE total_sales NUMERIC(8, 2);
SELECT SUM(sale_value) INTO total_sales FROM sales WHERE customer_id=in_customer_id;
CURSOR用于处理多行记录的查询结果
- DELIMITER $$
- DROP PROCEDURE IF EXITS cursor_example$$
- CREATE PROCEDURE cursor_example()
- READS SQL DATA
- BEGIN
- DECLARE l_employee_id INT;
- DECLARE l_salary NUMERIC(,);
- DECLARE l_department_id INT;
- DECLARE done INT DEFAULT ;
- DECLARE cur1 CURSOR FOR SELECT employee_id, salary, department_id FROM employees;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=;
- OPEN cur1;
- emp_loop: LOOP
- FETCH cur1 INTO l_employee_id, l_salary, l_department_id;
- IF done= THEN
- LEAVE emp_loop;
- END IF;
- END LOOP emp_loop;
- CLOSE cur1;
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXITS cursor_example$$
CREATE PROCEDURE cursor_example()
READS SQL DATA
BEGIN
DECLARE l_employee_id INT;
DECLARE l_salary NUMERIC(8,2);
DECLARE l_department_id INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT employee_id, salary, department_id FROM employees;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; OPEN cur1;
emp_loop: LOOP
FETCH cur1 INTO l_employee_id, l_salary, l_department_id;
IF done=1 THEN
LEAVE emp_loop;
END IF;
END LOOP emp_loop;
CLOSE cur1;
END$$
DELIMITER ;
unbounded SELECT语句用于存储过程返回结果集
- DELIMITER $$
- DROP PROCEDURE IF EXISTS sp_emps_in_dept$$
- CREATE PROCEDURE sp_emps_in_dept(in_employee_id INT)
- BEGIN
- SELECT employee_id, surname, firstname, address1, address2, zipcode, date_of_birth FROM employees WHERE department_id=in_employee_id;
- END$$
- DELIMITER ;
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_emps_in_dept$$
CREATE PROCEDURE sp_emps_in_dept(in_employee_id INT)
BEGIN
SELECT employee_id, surname, firstname, address1, address2, zipcode, date_of_birth FROM employees WHERE department_id=in_employee_id;
END$$ DELIMITER ;
UPDATE、INSERT、DELETE、CREATE TABLE等非查询语句也能够嵌入存储过程里
- DELIMITER $$
- DROP PROCEDURE IF EXITS sp_update_salary$$
- CREATE PROCEDURE sp_update_salary(in_employee_id INT, in_new_salary NUMERIC(,))
- BEGIN
- IF in_new_salary < OR in_new_salary > THEN
- SELECT "Illegal salary: salary must be between $5000 and $500, 000";
- ELSE
- UPDATE employees SET salary=in_new_salary WHERE employee_id=in_employee_id;
- END IF:
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXITS sp_update_salary$$
CREATE PROCEDURE sp_update_salary(in_employee_id INT, in_new_salary NUMERIC(8,2))
BEGIN
IF in_new_salary < 5000 OR in_new_salary > 500000 THEN
SELECT "Illegal salary: salary must be between $5000 and $500, 000";
ELSE
UPDATE employees SET salary=in_new_salary WHERE employee_id=in_employee_id;
END IF:
END$$ DELIMITER ;
9,使用CALL调用存储程序
- DELIMITER $$
- DROP PROCEDURE IF EXISTS call_example$$
- CREATE PROCEDURE call_example(employee_id INT, employee_type VARCHAR())
- NO SQL
- BEGIN
- DECLARE l_bonus_amount NUMERIC(,);
- IF employee_type='MANAGER' THEN
- CALL calc_manager_bonus(employee_id, l_bonus_amount);
- ELSE
- CALL calc_minion_bonus(employee_id, l_bonus_amount);
- END IF;
- CALL grant_bonus(employee_id, l_bonus_amount);
- END$$
- DELIMITER ;
DELIMITER $$ DROP PROCEDURE IF EXISTS call_example$$
CREATE PROCEDURE call_example(employee_id INT, employee_type VARCHAR(20))
NO SQL
BEGIN
DECLARE l_bonus_amount NUMERIC(8,2); IF employee_type='MANAGER' THEN
CALL calc_manager_bonus(employee_id, l_bonus_amount);
ELSE
CALL calc_minion_bonus(employee_id, l_bonus_amount);
END IF;
CALL grant_bonus(employee_id, l_bonus_amount);
END$$
DELIMITER ;
10,一个复杂的样例
- CREATE PROCEDURE putting_it_all_together(in_department_id INT)
- MODIFIES SQL DATA
- BEGIN
- DECLARE l_employee_id INT;
- DECLARE l_salary NUMERIC(,);
- DECLARE l_department_id INT;
- DECLARE l_new_salary NUMERIC(,);
- DECLARE done INT DEFAULT ;
- DECLARE cur1 CURSOR FOR
- SELECT employee_id, salary, department_id
- FROM employees
- WHERE department_id=in_department_id;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=;
- CREATE TEMPORARY TABLE IF NOT EXISTS emp_raises
- (employee_id INT, department_id INT, new_salary NUMERIC(,));
- OPEN cur1;
- emp_loop: LOOP
- FETCH cur1 INTO l_employee_id, l_salary, l_department_id;
- IF done= THEN /* No more rows */
- LEAVE emp_loop;
- END IF;
- CALL new_salary(1_employee_id, l_new_salary); /* Get new salary */
- IF (l_new_salary <> l_salary) THEN /* Salary changed */
- UPDATE employees
- SET salary=l_new_salary
- WHERE employee_id=l_employee_id;
- /* Keep track of changed salaries */
- INSERT INTO emp_raises(employee_id, department_id, new_salary)
- VALUES (l_employee_id, l_department_id, l_new_salary);
- END IF:
- END LOOP emp_loop;
- CLOSE cur1;
- /* Print out the changed salaries */
- SELECT employee_id, department_id, new_salary from emp_raises
- ORDER BY employee_id;
- END;
CREATE PROCEDURE putting_it_all_together(in_department_id INT)
MODIFIES SQL DATA
BEGIN
DECLARE l_employee_id INT;
DECLARE l_salary NUMERIC(8,2);
DECLARE l_department_id INT;
DECLARE l_new_salary NUMERIC(8,2);
DECLARE done INT DEFAULT 0; DECLARE cur1 CURSOR FOR
SELECT employee_id, salary, department_id
FROM employees
WHERE department_id=in_department_id; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; CREATE TEMPORARY TABLE IF NOT EXISTS emp_raises
(employee_id INT, department_id INT, new_salary NUMERIC(8,2)); OPEN cur1;
emp_loop: LOOP
FETCH cur1 INTO l_employee_id, l_salary, l_department_id;
IF done=1 THEN /* No more rows */
LEAVE emp_loop;
END IF;
CALL new_salary(1_employee_id, l_new_salary); /* Get new salary */
IF (l_new_salary <> l_salary) THEN /* Salary changed */
UPDATE employees
SET salary=l_new_salary
WHERE employee_id=l_employee_id;
/* Keep track of changed salaries */
INSERT INTO emp_raises(employee_id, department_id, new_salary)
VALUES (l_employee_id, l_department_id, l_new_salary);
END IF:
END LOOP emp_loop;
CLOSE cur1;
/* Print out the changed salaries */
SELECT employee_id, department_id, new_salary from emp_raises
ORDER BY employee_id;
END;
11,存储方法
存储方法与存储过程的差别
1,存储方法的參数列表仅仅同意IN类型的參数,并且不是必需也不同意指定INkeyword
2,存储方法返回一个单一的值,值的类型在存储方法的头部定义
3,存储方法能够在SQL语句内部调用
4,存储方法不能返回结果集
语法:
- CREATE
- [DEFINER = { user | CURRENT_USER }]
- PROCEDURE sp_name ([proc_parameter[,...]])
- [characteristic ...] routine_body
- CREATE
- [DEFINER = { user | CURRENT_USER }]
- FUNCTION sp_name ([func_parameter[,...]])
- RETURNS type
- [characteristic ...] routine_body
- proc_parameter:
- [ IN | OUT | INOUT ] param_name type
- func_parameter:
- param_name type
- type:
- Any valid MySQL data type
- characteristic:
- LANGUAGE SQL
- | [NOT] DETERMINISTIC
- | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
- | SQL SECURITY { DEFINER | INVOKER }
- | COMMENT 'string'
- routine_body:
- Valid SQL procedure statement
CREATE
[DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body CREATE
[DEFINER = { user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body proc_parameter:
[ IN | OUT | INOUT ] param_name type func_parameter:
param_name type type:
Any valid MySQL data type characteristic:
LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string' routine_body:
Valid SQL procedure statement
各參数说明见CREATE PROCEDURE and CREATE FUNCTION Syntax
样例:
- DELIMITER $$
- DROP FUNCTION IF EXISTS f_discount_price$$
- CREATE FUNCTION f_discount_price
- (normal_price NUMERIC(,))
- RETURNS NUMERIC(,)
- DETERMINISTIC
- BEGIN
- DECLARE discount_price NUMERIC(,);
- IF (normal_price > ) THEN
- SET discount_price = normal_price * .;
- ELSEIF (normal_price >) THEN
- SET discount_price = normal_price * .;
- ELSE
- SET discount_price = normal_price;
- END IF;
- RETURN(discount_price);
- END$$
- DELIMITER ;
DELIMITER $$ DROP FUNCTION IF EXISTS f_discount_price$$
CREATE FUNCTION f_discount_price
(normal_price NUMERIC(8,2))
RETURNS NUMERIC(8,2)
DETERMINISTIC
BEGIN
DECLARE discount_price NUMERIC(8,2); IF (normal_price > 500) THEN
SET discount_price = normal_price * .8;
ELSEIF (normal_price >100) THEN
SET discount_price = normal_price * .9;
ELSE
SET discount_price = normal_price;
END IF; RETURN(discount_price);
END$$ DELIMITER ;
12,触发器
触发器在INSERT、UPDATE或DELETE等DML语句改动数据库表时触发
触发器的典型应用场景是重要的业务逻辑、提高性能、监控表的改动等
触发器能够在DML语句运行前或后触发
- DELIMITER $$
- DROP TRIGGER sales_trigger$$
- CREATE TRIGGER sales_trigger
- BEFORE INSERT ON sales
- FOR EACH ROW
- BEGIN
- IF NEW.sale_value > THEN
- SET NEW.free_shipping = 'Y';
- ELSE
- SET NEW.free_shipping = 'N';
- END IF;
- IF NEW.sale_value > THEN
- SET NEW.discount = NEW.sale_value * .;
- ELSE
- SET NEW.discount = ;
- END IF;
- END$$
- DELIMITER ;
转自:http://fyb613.blog.163.com/blog/static/325460922010044417672/