Mysql-A simple introduction to stored procedures

definition:

        The English of the stored procedure is Stored Procedure. Its idea is very simple, it is the encapsulation of a set of pre-compiled SQL statements. Execution process: The stored procedure is pre-stored on the MySQL server. When it needs to be executed, the client only needs to send a command to the server to call the stored procedure, and the server can execute all the pre-stored series of SQL statements.

Create a stored procedure template syntax:

CREATE PROCEDURE 存储过程名(IN|OUT|INOUT 参数名 参数类型,...)
[characteristics ...]

BEGIN

 存储过程体

END

parameter explanation

symbol before parameter

IN : The current parameter is an input parameter, that is, an input parameter; the stored procedure just reads the value of this parameter. If no parameter type is defined, the default is IN, which means input parameters.

OUT: The current parameter is an output parameter, that is, an out parameter; after the execution is completed, the client or application that calls this stored procedure can read the value returned by this parameter.

INOUT : The current parameter can be either an input parameter or an output parameter.

Constraints for stored procedures

characteristics indicates the constraints on the stored procedure specified when creating the stored procedure, and its value information is as follows

1. LANGUAGE SQL: Indicates that the stored procedure execution body is composed of SQL statements, and the language supported by the current system is SQL.

2. [NOT] DETERMINISTIC: Indicates whether the result of the stored procedure execution is deterministic. DETERMINISTIC means the result is deterministic. The same input results in the same output every time the stored procedure is executed. NOT DETERMINISTIC means that the result is indeterminate, and the same input may get different output. If neither value is specified, the default is NOT DETERMINISTIC.

3. { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }: Indicates the restrictions on the use of SQL statements by subroutines:

1) CONTAINS SQL means that the subroutine of the current stored procedure contains SQL statements, but does not contain SQL statements for reading and writing data;

2) NO SQL means that the subroutine of the current stored procedure does not contain any SQL statement;

3) READS SQL DATA indicates that the subroutine of the current stored procedure contains SQL statements for reading data;

4) MODIFIES SQL DATA indicates that the subroutine of the current stored procedure contains the SQL statement for writing data.

5) By default, the system will specify CONTAINS SQL.

4. SQL SECURITY { DEFINER | INVOKER } : Permission to execute the current stored procedure, that is, specify which users can execute the current stored procedure.

1) DEFINER indicates that only the creator or definer of the current stored procedure can execute the current stored procedure;

2) INVOKER indicates that users with access rights to the current stored procedure can execute the current stored procedure.

3) If no relevant value is set, MySQL defaults to DEFINER.

4) COMMENT 'string': Comment information, which can be used to describe the stored procedure.

 keyword explanation

1. BEGIN...END: BEGIN...END contains multiple statements, and each statement ends with (;) symbol.

2. DECLARE: DECLARE is used to declare variables, the position used is in the middle of the BEGIN...END statement, and the variable needs to be declared before other statements are used.

3. SET: Assignment statement, used to assign values ​​to variables.

4. SELECT... INTO: Store the query results from the data table into variables, that is, assign values ​​to variables.

 Demo example

The sql script can be run directly under any local practice database, including table structure and data

DROP TABLE IF EXISTS `employees`;
CREATE TABLE `employees`  (
  `EMPLOYEE_ID` int(12) NOT NULL,
  `LAST_NAME` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `MANAGER_ID` int(11) NULL DEFAULT NULL,
  `SALARY` decimal(10, 2) NULL DEFAULT NULL,
  `department_id` int(11) NULL DEFAULT NULL,
  `location` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of employees
-- ----------------------------
INSERT INTO `employees` VALUES (100, 'STEVEN', 100, 1000.00, 50, 'jiangsu');
INSERT INTO `employees` VALUES (101, 'Kochhar', 100, 2000.00, 10, 'shenzhen');
INSERT INTO `employees` VALUES (102, 'De Haan', 100, 1000.00, 50, 'jiangsu');
INSERT INTO `employees` VALUES (103, 'Hunold', 102, 2000.00, 20, 'shenzhen');
INSERT INTO `employees` VALUES (104, 'Emst', 103, 1000.00, 30, 'jiangsu');
INSERT INTO `employees` VALUES (107, 'Lorentz', 103, 3000.00, 20, 'shenzhen');
INSERT INTO `employees` VALUES (124, 'Mourgos', 100, 1000.00, 40, 'beijing');

SET FOREIGN_KEY_CHECKS = 1;

Data effect:

 Stored procedure usage and its explanation

need:

        Create a stored procedure show_mgr_name() to query the name of an employee leader, and use the INOUT parameter "empname" to input the employee name, #output the leader's name.

#使用 DELIMITER 
#DELIMITER 语句只是用于帮助解析器正确解释存储过程定义的语法,并不会影响代码块中使用的实际 SQL 语句。
#在存储过程定义完成后,使用 DELIMITER ; 将分隔符改回默认的分号 (;)。
delimiter //
# 创建存储过程 show_mgr_name 指定为inout类型,即输如输出都是empname
create procedure show_mgr_name(inout empname varchar(25))
begin
    # 查询领导的名字,并将查询的LAST_NAME结果为变量empname赋值
    select LAST_NAME into empname
    from dbtest15.employees
        where EMPLOYEE_ID =
              #根据输入的员工姓名查询到领导的id
              (
            select MANAGER_ID
            from dbtest15.employees
            where LAST_NAME = empname
            );
end //
delimiter ;
#设置传入参数为Hunold,
set @empname :='Hunold';
# 使用call语法调用show_mgr_name存储过程
call show_mgr_name(@empname);
#查询结果:根据上面效果图片可以知道Hunold的领导是De Haan
select @empname;

debugging

1. Use the SELECT statement to query the intermediate results of program execution to debug the correctness of a SQL statement. After the debugging is successful, move the SELECT statement to the next SQL statement, and then debug the next SQL statement.

2. Copy the SQL statement in the stored procedure and debug it segment by segment.

         Many companies prohibit the use of stored procedures, but we may not use stored procedures, but we still need to master them. If you understand them, you can find some examples and practice more.

Guess you like

Origin blog.csdn.net/weixin_44285713/article/details/130808536