存储过程

创建存储过程

create
    # 创建存储过程: https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
    definer = `mysql.sys`@localhost procedure table_exists(IN in_db varchar(64), IN in_table varchar(64),
                                                           OUT out_exists enum ('', 'BASE TABLE', 'VIEW', 'TEMPORARY'))
    # 存储过程的注释
    comment '
笔者注
-----

摘自内置存储过程: sys.table_exists

Description
-----------

Tests whether the table specified in in_db and in_table exists either as a regular
table, or as a temporary table. The returned value corresponds to the table that
will be used, so if there''s both a temporary and a permanent table with the given
name, then ''TEMPORARY'' will be returned.

Parameters
-----------

in_db (VARCHAR(64)):
  The database name to check for the existance of the table in.

in_table (VARCHAR(64)):
  The name of the table to check the existance of.

out_exists ENUM('''', ''BASE TABLE'', ''VIEW'', ''TEMPORARY''):
  The return value: whether the table exists. The value is one of:
    * ''''           - the table does not exist neither as a base table, view, nor temporary table.
    * ''BASE TABLE'' - the table name exists as a permanent base table table.
    * ''VIEW''       - the table name exists as a view.
    * ''TEMPORARY''  - the table name exists as a temporary table.

Example
--------

mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.07 sec)

mysql> use db1;
Database changed
mysql> CREATE TABLE t1 (id INT PRIMARY KEY);
Query OK, 0 rows affected (0.08 sec)

mysql> CREATE TABLE t2 (id INT PRIMARY KEY);
Query OK, 0 rows affected (0.08 sec)
'
    # 访问控制 invoker或definer,决定调用存储过程的方式
    # 文档: https://dev.mysql.com/doc/refman/8.0/en/stored-objects-security.html
    # 博客: https://www.cnblogs.com/gaoyuechen/p/9407463.html
    sql security invoker
BEGIN
    # 实现具体业务逻辑的代码
END;

编译sql和使用占位符变量

https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html