IN
类型 (有参数无返回)OUT
类型 (无参数有返回)IN
又带有OUT
(有参数,有返回)INOUT
(有参数有返回)准备工作:
CREATE DATABASE dbtest15;
USE dbtest15;
CREATE TABLE employees
AS
SELECT *
FROM atguigudb.`employees`;
CREATE TABLE departments
AS
SELECT *
FROM atguigudb.`departments`;
SELECT *
FROM employees;
SELECT *
FROM departments;
avg_employee_salary()
,返回所有员工的平均工资show_max_salary()
,用来查看"emps
"表的最高薪资# 举例2: 创建存储过程avg_employee_salary(),返回所有员工的平均工资
DELIMITER $
CREATE PROCEDURE avg_employee_salary()
BEGIN
SELECT AVG(salary) FROM employees;
END $
DELIMITER;
CALL avg_employee_salary();
# 举例3:创建存储过程show_max_salary(),用来查看"emps"表的最高薪资
DELIMITER //
CREATE PROCEDURE show_max_salary()
BEGIN
SELECT MAX(salary)
FROM employees;
END //
DELIMITER;
show_min_salary()
,查看"emps
"表的最低薪资值。并将最低薪资通过OUT
参数"ms
"输出# 举例4:创建存储过程show_min_salary(),查看"emps"表的最低薪资值。并将最低薪资通过OUT参数"ms"输出
DESC employees;
DELIMITER //
CREATE PROCEDURE show_min_salary(OUT ms DOUBLE)
BEGIN
SELECT MIN(salary) INTO ms
FROM employees;
END //
DELIMITER;
# 调用
CALL show_min_salary(@ms);
# 查看
SELECT @ms;
show_someone_salary()
,查看"emps
"表的某一个员工的薪资待遇,并用IN
参数empname
输入员工姓名# 举例5:创建存储过程show_someone_salary(),查看"emps"表的某一个员工的薪资待遇,并用IN参数empname输入员工姓名
DELIMITER //
CREATE PROCEDURE show_someone_salary(IN empname VARCHAR(20))
BEGIN
SELECT salary FROM employees
WHERE last_name = empname;
END //
DELIMITER;
# 调用方式1
CALL show_someone_salary('Abel');
# 调用方式2
SET @empname := 'Abel';
CALL show_someone_salary(@empname);
show_someone_salary2()
,查看"emps
"表的某一个员工的薪资,并用IN
参数empname
输入员工的姓名,用OUT
参数empsalary
输入员工的薪资# 举例6:创建存储过程show_someone_salary2(),查看"emps"表的某一个员工的薪资,并用IN参数empname输入员工的姓名,用OUT参数empsalary输入员工的薪资
DELIMITER //
CREATE PROCEDURE show_someone_salary2(IN empname VARCHAR(20), OUT empsalary DECIMAL(10,2))
BEGIN
SELECT salary INTO empsalary
FROM employees
WHERE last_name = empname;
END //
DELIMITER;
# 调用
SET @empname = 'Abel';
CALL show_someone_salary2(@empname, @empsalary);
SELECT @empsalary;
show_mgr_name()
.查询某个员工领导的姓名,并用INOUT
参数"empname
"输入员工姓名,输出领导姓名# 举例7:创建存储过程show_mgr_name().查询某个员工领导的姓名,并用INOUT参数"empname"输入员工姓名,输出领导姓名
DESC employees;
DELIMITER //
CREATE PROCEDURE show_mgr_name(INOUT empname VARCHAR(25))
BEGIN
SELECT last_name INTO empname
FROM employees
WHERE employee_id = (
SELECT manager_id
FROM employees
WHERE last_name = empname
);
END //
DELIMITER;
# 调用
SET @empname := 'Abel';
CALL show_mgr_name(@empname);
SELECT @empname;
email_by_name()
参数定义为空,该函数查询Abel
的email
,并返回,数据类型为字符串型# 举例1:创建存储函数,名称为email_by_name(0,参数定义为空,该函数查询Abel的email,并返回,数据类型为字符串型
DELIMITER //
CREATE FUNCTION email_by_name()
RETURNS VARCHAR(25)
DETERMINISTIC
CONTAINS SQL
READS SQL DATA
BEGIN
RETURN (SELECT email FROM employees WHERE last_name = 'Abel');
END //
DELIMITER ;
# 调用
SELECT email_by_name();
SELECT email, last_name FROM employees WHERE last_name = 'Abel';
email_by_id()
,参数传入emp_id
,该函数查询emp_id
,并返回,数据类型为字符串型。# 创建存储函数,名称定义为email_by_id(),参数传入emp_id,该函数查询emp_id,并返回,数据类型为字符串型。
# 创建函数前,执行此语句,保证函数的创建可以成功(可以不用检验特征)
SET GLOBAL log_bin_trust_function_creators = 1;
DELIMITER //
CREATE FUNCTION email_by_id(emp_id INT)
RETURNS VARCHAR(25)
BEGIN
RETURN (SELECT email FROM employees WHERE employee_id = emp_id);
END //
DELIMITER;
# 调用
SELECT email_by_id(102);
SET @emp_id = 102;
SELECT email_by_id(@emp_id);
count_by_id()
,参数传入dept_id
, 该函数查询dept_id
部门的员工数,并返回,数据类型为整型。# 举例3:创建存储函数count_by_id(),参数传入dept_id, 该函数查询dept_id部门的员工数,并返回,数据类型为整型。
DELIMITER //
CREATE FUNCTION count_by_id(dept_id INT)
RETURNS INT
BEGIN
RETURN (SELECT COUNT(*) FROM employees WHERE department_id = dept_id);
END //
DELIMITER;
# 调用
SELECT count_by_id(30);
SET @dept_id := 30;
SELECT count_by_id(@dept_id);
存储过程和存储函数的总结:
此外,存储函数可以放在查询语句中使用,存储过程不行。反之,存储过程的功能更加强大,包括能够执行对表的操作和事务的操作,这些功能是存储函数不具备的。
MySQL
存储了存储过程和函数的状态信息,用户可以使用SHOW STATUS
语句或SHOW CREATE
语句来查看,也可以直接从系统的information_scheme
数据库中查询。这里介绍三种方法:# 使用SHOW CREATE语句查看存储过程和函数的创建信息
SHOW CREATE PROCEDURE show_mgr_name;
SHOW CREATE FUNCTION count_by_id;
# 使用SHOW CREATE语句查看存储过程和函数的状态信息
SHOW PROCEDURE STATUS;
SHOW PROCEDURE STATUS LIKE 'show_max_salary';
SHOW FUNCTION STATUS LIKE 'email_by_id';
# 从information_schema.Routines表中查看存储过程和函数信息
SELECT * FROM information_schema.`ROUTINES`
WHERE ROUTINE_NAME = 'EMAIL_BY_ID';
ALTER
语句实现ALTER PROCEDURE show_max_salary
SQL SECURITY INVOKER
COMMENT '查询最高工资';
DROP FUNCTION IF EXIT count_by_id;
DROP PROCEDURE IF EXIT show_min_salary;