MySQL/MariaDB数据库的存储过程

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.存储过程概述

1>.存储过程优势

  存储过程把经常使用的SQL语句或业务逻辑封装起来,预编译保存在数据库中,当需要时从数据库中直接调用,省去了编译的过程

  提高了运行速度

  同时降低网络数据传输量

2>.存储过程与自定义函数的区别

  存储过程实现的过程要复杂一些,而函数的针对性较强

  存储过程可以有多个返回值,而自定义函数只有一个返回值

  存储过程一般独立的来执行,而函数往往是作为其他SQL语句的一部分来使用

3>.存储过程存储位置

  存储过程保存在mysql.proc表中。
MariaDB [yinzhengjie]> SELECT * FROM mysql.proc\G          #不难发现存储过程和函数都保存在该表中。
*************************** 1. row ***************************
db: mysql
name: AddGeometryColumn
type: PROCEDURE
specific_name: AddGeometryColumn
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list: catalog varchar(64), t_schema varchar(64),
t_name varchar(64), geometry_column varchar(64), t_srid int
returns:
body: begin
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); PREPARE ls from @qwe; exe
cute ls; deallocate prepare ls; end definer: root@localhost
created: 2019-10-26 22:17:15
modified: 2019-10-26 22:17:15
sql_mode:
comment:
character_set_client: utf8
collation_connection: utf8_general_ci
db_collation: latin1_swedish_ci
body_utf8: begin
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); PREPARE ls from @qwe; exe
cute ls; deallocate prepare ls; end*************************** 2. row ***************************
db: mysql
name: DropGeometryColumn
type: PROCEDURE
specific_name: DropGeometryColumn
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list: catalog varchar(64), t_schema varchar(64),
t_name varchar(64), geometry_column varchar(64)
returns:
body: begin
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; deallocate prepare ls; en
d definer: root@localhost
created: 2019-10-26 22:17:15
modified: 2019-10-26 22:17:15
sql_mode:
comment:
character_set_client: utf8
collation_connection: utf8_general_ci
db_collation: latin1_swedish_ci
body_utf8: begin
set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; deallocate prepare ls; en
d*************************** 3. row ***************************
db: yinzhengjie
name: students_numbers
type: FUNCTION
specific_name: students_numbers
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list:
returns: smallint(6)
body: BEGIN
DECLARE x SMALLINT;
SELECT COUNT(StuID) FROM students INTO x;
RETURN x;
END
definer: root@localhost
created: 2019-10-28 20:38:19
modified: 2019-10-28 20:38:19
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
db_collation: utf8_general_ci
body_utf8: BEGIN
DECLARE x SMALLINT;
SELECT COUNT(StuID) FROM students INTO x;
RETURN x;
END
*************************** 4. row ***************************
db: yinzhengjie
name: deleteById
type: FUNCTION
specific_name: deleteById
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list: uid SMALLINT UNSIGNED
returns: varchar(100) CHARSET utf8
body: BEGIN
DELETE FROM students WHERE stuid = uid;
RETURN (SELECT COUNT(stuid) FROM students);
END
definer: root@localhost
created: 2019-10-28 20:06:27
modified: 2019-10-28 20:06:27
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
db_collation: utf8_general_ci
body_utf8: BEGIN
DELETE FROM students WHERE stuid = uid;
RETURN (SELECT COUNT(stuid) FROM students);
END
*************************** 5. row ***************************
db: yinzhengjie
name: addTwoNumber
type: FUNCTION
specific_name: addTwoNumber
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list: x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED
returns: smallint(6)
body: BEGIN
DECLARE a, b SMALLINT UNSIGNED;
SET a = x, b = y;
RETURN a+b;
END
definer: root@localhost
created: 2019-10-28 20:13:59
modified: 2019-10-28 20:13:59
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
db_collation: utf8_general_ci
body_utf8: BEGIN
DECLARE a, b SMALLINT UNSIGNED;
SET a = x, b = y;
RETURN a+b;
END
*************************** 6. row ***************************
db: yinzhengjie
name: showTime
type: PROCEDURE
specific_name: showTime
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list:
returns:
body: BEGIN
SELECT now();
END
definer: root@localhost
created: 2019-10-28 20:49:09
modified: 2019-10-28 20:49:09
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
db_collation: utf8_general_ci
body_utf8: BEGIN
SELECT now();
END
*************************** 7. row ***************************
db: yinzhengjie
name: dorepeat
type: PROCEDURE
specific_name: dorepeat
language: SQL
sql_data_access: CONTAINS_SQL
is_deterministic: NO
security_type: DEFINER
param_list: n INT
returns:
body: BEGIN
SET @i = 0;
SET @sum = 0;
REPEAT SET @sum = @sum+@i; SET @i = @i + 1;
UNTIL @i > n END REPEAT;
END
definer: root@localhost
created: 2019-10-28 21:32:01
modified: 2019-10-28 21:32:01
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
db_collation: utf8_general_ci
body_utf8: BEGIN
SET @i = 0;
SET @sum = 0;
REPEAT SET @sum = @sum+@i; SET @i = @i + 1;
UNTIL @i > n END REPEAT;
END
7 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SELECT * FROM mysql.proc\G          #不难发现存储过程和函数都保存在该表中。

二.存储过程实战

MariaDB [yinzhengjie]> HELP CREATE PROCEDURE;
Name: 'CREATE PROCEDURE'
Description:
Syntax:
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:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER } routine_body:
Valid SQL routine statement These statements create stored routines. By default, a routine is
associated with the default database. To associate the routine
explicitly with a given database, specify the name as db_name.sp_name
when you create it. The CREATE FUNCTION statement is also used in MySQL to support UDFs
(user-defined functions). See
https://mariadb.com/kb/en/create-function-udf/. A UDF can
be regarded as an external stored function. Stored functions share
their namespace with UDFs. See
http://dev.mysql.com/doc/refman/5.5/en/function-resolution.html, for
the rules describing how the server interprets references to different
kinds of functions. To invoke a stored procedure, use the CALL statement (see [HELP CALL]).
To invoke a stored function, refer to it in an expression. The function
returns a value during expression evaluation. CREATE PROCEDURE and CREATE FUNCTION require the CREATE ROUTINE
privilege. They might also require the SUPER privilege, depending on
the DEFINER value, as described later in this section. If binary
logging is enabled, CREATE FUNCTION might require the SUPER privilege,
as described in
https://mariadb.com/kb/en/binary-logging-of-stored-routines/. By default, MySQL automatically grants the ALTER ROUTINE and EXECUTE
privileges to the routine creator. This behavior can be changed by
disabling the automatic_sp_privileges system variable. See
https://mariadb.com/kb/en/stored-routine-privileges/. The DEFINER and SQL SECURITY clauses specify the security context to be
used when checking access privileges at routine execution time, as
described later in this section. If the routine name is the same as the name of a built-in SQL function,
a syntax error occurs unless you use a space between the name and the
following parenthesis when defining the routine or invoking it later.
For this reason, avoid using the names of existing SQL functions for
your own stored routines. The IGNORE_SPACE SQL mode applies to built-in functions, not to stored
routines. It is always permissible to have spaces after a stored
routine name, regardless of whether IGNORE_SPACE is enabled. The parameter list enclosed within parentheses must always be present.
If there are no parameters, an empty parameter list of () should be
used. Parameter names are not case sensitive. Each parameter is an IN parameter by default. To specify otherwise for
a parameter, use the keyword OUT or INOUT before the parameter name. *Note*: Specifying a parameter as IN, OUT, or INOUT is valid only for a
PROCEDURE. For a FUNCTION, parameters are always regarded as IN
parameters. An IN parameter passes a value into a procedure. The procedure might
modify the value, but the modification is not visible to the caller
when the procedure returns. An OUT parameter passes a value from the
procedure back to the caller. Its initial value is NULL within the
procedure, and its value is visible to the caller when the procedure
returns. An INOUT parameter is initialized by the caller, can be
modified by the procedure, and any change made by the procedure is
visible to the caller when the procedure returns. For each OUT or INOUT parameter, pass a user-defined variable in the
CALL statement that invokes the procedure so that you can obtain its
value when the procedure returns. If you are calling the procedure from
within another stored procedure or function, you can also pass a
routine parameter or local routine variable as an IN or INOUT
parameter. The following example shows a simple stored procedure that uses an OUT
parameter: MariaDB> delimiter // MariaDB> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END//
Query OK, 0 rows affected (0.00 sec) MariaDB> delimiter ; MariaDB> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec) MariaDB> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec) The example uses the mysql client delimiter command to change the
statement delimiter from ; to // while the procedure is being defined.
This enables the ; delimiter used in the procedure body to be passed
through to the server rather than being interpreted by mysql itself.
See
https://mariadb.com/kb/en/stored-procedure-overview/. The RETURNS clause may be specified only for a FUNCTION, for which it
is mandatory. It indicates the return type of the function, and the
function body must contain a RETURN value statement. If the RETURN
statement returns a value of a different type, the value is coerced to
the proper type. For example, if a function specifies an ENUM or SET
value in the RETURNS clause, but the RETURN statement returns an
integer, the value returned from the function is the string for the
corresponding ENUM member of set of SET members. The following example function takes a parameter, performs an operation
using an SQL function, and returns the result. In this case, it is
unnecessary to use delimiter because the function definition contains
no internal ; statement delimiters: MariaDB> CREATE FUNCTION hello (s CHAR(20))
MariaDB> RETURNS CHAR(50) DETERMINISTIC
-> RETURN CONCAT('Hello, ',s,'!');
Query OK, 0 rows affected (0.00 sec) MariaDB> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world! |
+----------------+
1 row in set (0.00 sec) Parameter types and function return types can be declared to use any
valid data type, except that the COLLATE attribute cannot be used prior
to MySQL 5.5.3. As of 5.5.3, COLLATE can be used if preceded by the
CHARACTER SET attribute. The routine_body consists of a valid SQL routine statement. This can be
a simple statement such as SELECT or INSERT, or a compound statement
written using BEGIN and END. Compound statements can contain
declarations, loops, and other control structure statements. The syntax
for these statements is described in
https://mariadb.com/kb/programmatic-and-compound-statements. MySQL permits routines to contain DDL statements, such as CREATE and
DROP. MySQL also permits stored procedures (but not stored functions)
to contain SQL transaction statements such as COMMIT. Stored functions
may not contain statements that perform explicit or implicit commit or
rollback. Support for these statements is not required by the SQL
standard, which states that each DBMS vendor may decide whether to
permit them. Statements that return a result set can be used within a stored
procedure but not within a stored function. This prohibition includes
SELECT statements that do not have an INTO var_list clause and other
statements such as SHOW, EXPLAIN, and CHECK TABLE. For statements that
can be determined at function definition time to return a result set, a
Not allowed to return a result set from a function error occurs
(ER_SP_NO_RETSET). For statements that can be determined only at
runtime to return a result set, a PROCEDURE %s can't return a result
set in the given context error occurs (ER_SP_BADSELECT). USE statements within stored routines are not permitted. When a routine
is invoked, an implicit USE db_name is performed (and undone when the
routine terminates). The causes the routine to have the given default
database while it executes. References to objects in databases other
than the routine default database should be qualified with the
appropriate database name. For additional information about statements that are not permitted in
stored routines, see
https://mariadb.com/kb/en/stored-routine-privileges/
. For information about invoking stored procedures from within programs
written in a language that has a MySQL interface, see [HELP CALL]. MySQL stores the sql_mode system variable setting that is in effect at
the time a routine is created, and always executes the routine with
this setting in force, regardless of the server SQL mode in effect when
the routine is invoked. The switch from the SQL mode of the invoker to that of the routine
occurs after evaluation of arguments and assignment of the resulting
values to routine parameters. If you define a routine in strict SQL
mode but invoke it in nonstrict mode, assignment of arguments to
routine parameters does not take place in strict mode. If you require
that expressions passed to a routine be assigned in strict SQL mode,
you should invoke the routine with strict mode in effect. URL: https://mariadb.com/kb/en/create-procedure/ MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> HELP CREATE PROCEDURE;

1>.创建存储过程

MariaDB [yinzhengjie]> DELIMITER  //
MariaDB [yinzhengjie]> CREATE PROCEDURE showTime()
-> BEGIN
-> SELECT now();
-> END//
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]> DELIMITER ;
MariaDB [yinzhengjie]>

创建无参存储过程

MariaDB [yinzhengjie]> DELIMITER //
MariaDB [yinzhengjie]> CREATE PROCEDURE selectById(IN uid SMALLINT UNSIGNED) #需要用户传入一个参数,然后根据用户传入的参数做相应的处理
-> BEGIN
-> SELECT * FROM students WHERE stuid = uid; #根据用户输入的参数查询相应的stuid。
-> END//
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]> DELIMITER ;
MariaDB [yinzhengjie]>

创建含参存储过程:只有一个IN参数(根据用户输入的数字查询对应表的记录)

MariaDB [yinzhengjie]> DELIMITER //
MariaDB [yinzhengjie]> CREATE PROCEDURE dorepeat(n INT)
-> BEGIN
-> SET @i = 0;      #注意此处定义的是会话变量,尽管调用存储过程结束,改变了依旧可以在调用的会话中查询到。
-> SET @sum = 0;
-> REPEAT SET @sum = @sum+@i; SET @i = @i + 1;    #循环体为重复对sum执行累加操作,步长为1.
-> UNTIL @i > n END REPEAT;                #结束循环条件是会话变量"i"的值大于用户传入的参数
-> END//
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]> DELIMITER ;
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

创建含参存储过程:只有一个IN参数(根据用户输入的数字累加案例)

MariaDB [yinzhengjie]> DELIMITER //
MariaDB [yinzhengjie]> CREATE PROCEDURE deleteById(IN uid SMALLINT UNSIGNED, OUT num SMALLINT UNSIGNED)
-> BEGIN
-> DELETE FROM students WHERE stuid >= uid;
-> SELECT row_count() into num; #将更新的行数保存到num变量中,改变了会随着存储过程调用而传出。
-> END//
Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]> DELIMITER ;
MariaDB [yinzhengjie]> 说明:
创建存储过程deleteById,包含一个IN参数和一个OUT参数.调用时,传入删除的ID和保存被修改的行数值的用户变量@Line,select @Line;输出被影响行数

创建含参存储过程:包含IN参数和OUT参数

2>.查看存储过程列表

MariaDB [yinzhengjie]> SHOW PROCEDURE STATUS;
+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character
_set_client | collation_connection | Database Collation |+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+| mysql | AddGeometryColumn | PROCEDURE | root@localhost | 2019-10-26 22:17:15 | 2019-10-26 22:17:15 | DEFINER | | utf8
| utf8_general_ci | latin1_swedish_ci || mysql | DropGeometryColumn | PROCEDURE | root@localhost | 2019-10-26 22:17:15 | 2019-10-26 22:17:15 | DEFINER | | utf8
| utf8_general_ci | latin1_swedish_ci || yinzhengjie | deleteById | PROCEDURE | root@localhost | 2019-10-28 21:23:31 | 2019-10-28 21:23:31 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | dorepeat | PROCEDURE | root@localhost | 2019-10-28 21:32:01 | 2019-10-28 21:32:01 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | selectById | PROCEDURE | root@localhost | 2019-10-28 21:21:37 | 2019-10-28 21:21:37 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | showTime | PROCEDURE | root@localhost | 2019-10-28 20:49:09 | 2019-10-28 20:49:09 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci |+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+6 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SHOW PROCEDURE STATUS;

MariaDB [yinzhengjie]> SHOW PROCEDURE STATUS\G
*************************** 1. row ***************************
Db: mysql
Name: AddGeometryColumn
Type: PROCEDURE
Definer: root@localhost
Modified: 2019-10-26 22:17:15
Created: 2019-10-26 22:17:15
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
Db: mysql
Name: DropGeometryColumn
Type: PROCEDURE
Definer: root@localhost
Modified: 2019-10-26 22:17:15
Created: 2019-10-26 22:17:15
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 3. row ***************************
Db: yinzhengjie
Name: deleteById
Type: PROCEDURE
Definer: root@localhost
Modified: 2019-10-28 21:23:31
Created: 2019-10-28 21:23:31
Security_type: DEFINER
Comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
*************************** 4. row ***************************
Db: yinzhengjie
Name: dorepeat
Type: PROCEDURE
Definer: root@localhost
Modified: 2019-10-28 21:32:01
Created: 2019-10-28 21:32:01
Security_type: DEFINER
Comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
*************************** 5. row ***************************
Db: yinzhengjie
Name: selectById
Type: PROCEDURE
Definer: root@localhost
Modified: 2019-10-28 21:21:37
Created: 2019-10-28 21:21:37
Security_type: DEFINER
Comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
*************************** 6. row ***************************
Db: yinzhengjie
Name: showTime
Type: PROCEDURE
Definer: root@localhost
Modified: 2019-10-28 20:49:09
Created: 2019-10-28 20:49:09
Security_type: DEFINER
Comment:
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
6 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SHOW PROCEDURE STATUS\G

3>.查看存储过程定义

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE showTime\G
*************************** 1. row ***************************
Procedure: showTime
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `showTime`()
BEGIN
SELECT now();
END
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE showTime\G

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE selectById\G
*************************** 1. row ***************************
Procedure: selectById
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `selectById`(IN uid SMALLINT UNSIGNED)
BEGIN
SELECT * FROM students WHERE stuid = uid;
END
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE selectById\G

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE dorepeat\G
*************************** 1. row ***************************
Procedure: dorepeat
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `dorepeat`(n INT)
BEGIN
SET @i = 0;
SET @sum = 0;
REPEAT SET @sum = @sum+@i; SET @i = @i + 1;
UNTIL @i > n END REPEAT;
END
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE dorepeat\G

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE deleteById\G
*************************** 1. row ***************************
Procedure: deleteById
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `deleteById`(IN uid SMALLINT UNSIGNED, OUT num SMALLINT
UNSIGNED)BEGIN
DELETE FROM students WHERE stuid >= uid;
SELECT row_count() into num;
END
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> SHOW CREATE PROCEDURE deleteById\G

4>.调用存储过程

MariaDB [yinzhengjie]> CALL showTime;                     #当无参时,可以省略"()",当有参数时,不可省略"()”
+---------------------+
| now() |
+---------------------+
| 2019-10-28 21:27:04 |
+---------------------+
1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> CALL showTime;     #当无参时,可以省略"()",当有参数时,不可省略"()”

MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
22 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> call selectById(20);
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
| 20 | Diao Chan | 19 | F | 7 | NULL |
+-------+-----------+-----+--------+---------+-----------+
1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> call selectById(20);

MariaDB [yinzhengjie]> CALL dorepeat(200);
Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT @sum;
+-------+
| @sum |
+-------+
| 20100 |
+-------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT @i;
+------+
| @i |
+------+
| 201 |
+------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> CALL dorepeat(200);

MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
22 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> CALL deleteById(2,@Line);
Query OK, 1 row affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT @Line;
+-------+
| @Line |
+-------+
| 21 |
+-------+
row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM students;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
+-------+-------------+-----+--------+---------+-----------+
row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> CALL deleteById(2,@Line);

5>.存储过程修改

  ALTER语句修改存储过程只能修改存储过程的注释等无关紧要的东西,不能修改存储过程体,所以要修改存储过程,方法就是删除重建

6>.删除存储过程

MariaDB [yinzhengjie]> SHOW PROCEDURE STATUS;
+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character
_set_client | collation_connection | Database Collation |+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+| mysql | AddGeometryColumn | PROCEDURE | root@localhost | 2019-10-26 22:17:15 | 2019-10-26 22:17:15 | DEFINER | | utf8
| utf8_general_ci | latin1_swedish_ci || mysql | DropGeometryColumn | PROCEDURE | root@localhost | 2019-10-26 22:17:15 | 2019-10-26 22:17:15 | DEFINER | | utf8
| utf8_general_ci | latin1_swedish_ci || yinzhengjie | deleteById | PROCEDURE | root@localhost | 2019-10-28 21:23:31 | 2019-10-28 21:23:31 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | dorepeat | PROCEDURE | root@localhost | 2019-10-28 21:32:01 | 2019-10-28 21:32:01 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | selectById | PROCEDURE | root@localhost | 2019-10-28 21:21:37 | 2019-10-28 21:21:37 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | showTime | PROCEDURE | root@localhost | 2019-10-28 20:49:09 | 2019-10-28 20:49:09 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci |+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+6 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP PROCEDURE IF EXISTS deleteById;
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP PROCEDURE IF EXISTS selectById;
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW PROCEDURE STATUS;
+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character
_set_client | collation_connection | Database Collation |+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+| mysql | AddGeometryColumn | PROCEDURE | root@localhost | 2019-10-26 22:17:15 | 2019-10-26 22:17:15 | DEFINER | | utf8
| utf8_general_ci | latin1_swedish_ci || mysql | DropGeometryColumn | PROCEDURE | root@localhost | 2019-10-26 22:17:15 | 2019-10-26 22:17:15 | DEFINER | | utf8
| utf8_general_ci | latin1_swedish_ci || yinzhengjie | dorepeat | PROCEDURE | root@localhost | 2019-10-28 21:32:01 | 2019-10-28 21:32:01 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci || yinzhengjie | showTime | PROCEDURE | root@localhost | 2019-10-28 20:49:09 | 2019-10-28 20:49:09 | DEFINER | | utf8mb4
| utf8mb4_general_ci | utf8_general_ci |+-------------+--------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------
------------+----------------------+--------------------+4 rows in set (0.00 sec) MariaDB [yinzhengjie]>

MariaDB [yinzhengjie]> DROP PROCEDURE IF EXISTS deleteById;

7>.流程控制

  存储过程和函数中可以使用流程控制来控制语句的执行

  流程控制:
    IF:
      用来进行条件判断。根据是否满足条件,执行不同语句     CASE:
      用来进行条件判断,可实现比IF语句更复杂的条件判断
    
    LOOP:
      重复执行特定的语句,实现一个简单的循环     LEAVE:
      用于跳出循环控制     ITERATE:
      跳出本次循环,然后直接进入下一次循环     REPEAT:
      有条件控制的循环语句。当满足特定条件时,就会跳出循环语句     WHILE:
      有条件控制的循环语句

最新文章

  1. how to use automapper in c#, from cf~
  2. js 中使用工厂方法和构造器方法
  3. Error: Linux下 mysql.sock文件丢失被删除解决方法
  4. Android在子线程中更新UI(二)
  5. sort和qsort排序
  6. LYNC2013介绍和基础架构准备角色
  7. PLSQL WEBSERVICES 发布
  8. django(权限、认证)系统—— 基于Authentication backends定制
  9. Selenium自动化Chrome浏览器 在windows下窗口最大化
  10. 关于安装 Microsoft Office
  11. android 之TCP客户端编程
  12. 安装psycopg2时出错:Error: pg_config executable not found.
  13. mongorestore 一次踩雷
  14. Extjs Window用法详解 2 打印具体应用
  15. Windows系统盘瘦身指南
  16. opencv学习之PyImageSearch
  17. mysql实战优化之九:MySQL查询缓存总结
  18. Android侧滑菜单和轮播图之滑动冲突
  19. PHP-输入变量
  20. (转)zabbix之生产案例

热门文章

  1. PMP 第6章错题总结
  2. 将pip源设置国内源
  3. Linux下tar的安装方式
  4. [转帖]Shell运维手册
  5. [转帖]在VirtualBox Linux 7u2 中安装Oracle RAC 12.2.0.1.0
  6. [Linux] 树莓派 4B 安装 Ubuntu 19.10 (Eoan Ermine) IOT 版
  7. vue 项目不显示样式 排版错乱
  8. Word 插入页码 -- 视频教程(7)
  9. jQuery Ajax async=>false异步改为同步时,导致浏览器假死的处理方法
  10. HTML登录注册页面简单实现