MySQL/MariaDB數據庫的存儲過程

         MySQL/MariaDB數據庫的存儲過程
html

                                   做者:尹正傑mysql

版權聲明:原創做品,謝絕轉載!不然將追究法律責任。sql

 

 

一.存儲過程概述數據庫

1>.存儲過程優點express

  存儲過程把常常使用的SQL語句或業務邏輯封裝起來,預編譯保存在數據庫中,當須要時從數據庫中直接調用,省去了編譯的過程

  提升了運行速度
  同時下降網絡數據傳輸量

2>.存儲過程與自定義函數的區別網絡

  存儲過程實現的過程要複雜一些,而函數的針對性較強

  存儲過程能夠有多個返回值,而自定義函數只有一個返回值
  存儲過程通常獨立的來執行,而函數每每是做爲其餘SQL語句的一部分來使用

3>.存儲過程存儲位置app

  存儲過程保存在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          #不難發現存儲過程和函數都保存在該表中。

 

二.存儲過程實戰less

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>.建立存儲過程ide

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:
      有條件控制的循環語句
相關文章
相關標籤/搜索