MySQL 存儲過程 create procedure/function語句


mysql> show variables like '%trust%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | OFF   |
+---------------------------------+-------+
1 row in set (0.03 sec)
 
mysql> use course;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> delimiter //
mysql> select * from students;
    -> //
+-----+--------+--------+---------+---------------------+
| sid | sname  | gender | dept_id | brithday            |
+-----+--------+--------+---------+---------------------+
|   1 | Andrew | 0      |       1 | 1983-01-01 00:00:00 |
|   2 | Andy   | 0      |       1 | 1983-01-01 00:00:00 |
|   3 | Bob    | 0      |       1 | 1983-01-01 00:00:00 |
|   4 | Ruth   | 1      |       2 | 1983-01-01 00:00:00 |
|   5 | Mike   | 0      |       2 | 1986-01-01 00:00:00 |
|   6 | John   | 0      |       3 | 1986-01-01 00:00:00 |
|   7 | Cindy  | 1      |       3 | 1986-01-01 00:00:00 |
|   8 | Susan  | 1      |       3 | 1986-01-01 00:00:00 |
+-----+--------+--------+---------+---------------------+
8 rows in set (0.00 sec)

mysql> delimiter ;
mysql> select * from students;
+-----+--------+--------+---------+---------------------+
| sid | sname  | gender | dept_id | brithday            |
+-----+--------+--------+---------+---------------------+
|   1 | Andrew | 0      |       1 | 1983-01-01 00:00:00 |
|   2 | Andy   | 0      |       1 | 1983-01-01 00:00:00 |
|   3 | Bob    | 0      |       1 | 1983-01-01 00:00:00 |
|   4 | Ruth   | 1      |       2 | 1983-01-01 00:00:00 |
|   5 | Mike   | 0      |       2 | 1986-01-01 00:00:00 |
|   6 | John   | 0      |       3 | 1986-01-01 00:00:00 |
|   7 | Cindy  | 1      |       3 | 1986-01-01 00:00:00 |
|   8 | Susan  | 1      |       3 | 1986-01-01 00:00:00 |
+-----+--------+--------+---------+---------------------+
8 rows in set (0.00 sec)
delimiter //
create procedure simpleproc (in param1 int,out param2 int)
BEGIN
select count(*) into param2 from students where sid>param1;
END;
//
delimiter ;


delimiter //
create procedure simpleproc2 ()
BEGIN
select count(*) from students where sid>1;
END;
//
delimiter ;


delimiter //
create function hello (s char(20))
returns char(50)
return concat('Hello, ',s,'!');
//
delimiter ;
mysql> delimiter //
mysql> create procedure simpleproc (in param1 int,out param2 int)
    -> BEGIN
    -> select count(*) into param2 from students where sid>param1;
    -> END;
    -> //
Query OK, 0 rows affected (0.06 sec)

mysql> delimiter ;
mysql> call simpleproc(1,@a);
Query OK, 1 row affected (0.00 sec)

mysql> select @a;
+------+
| @a   |
+------+
|    7 |
+------+
1 row in set (0.00 sec)

mysql> select count(*) from students where sid>1;
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

mysql> call simpleproc(3,@a);
Query OK, 1 row affected (0.00 sec)

mysql> select @a;
+------+
| @a   |
+------+
|    5 |
+------+
1 row in set (0.00 sec)

mysql> delimiter //
mysql> create procedure simpleproc2 ()
    -> BEGIN
    -> select count(*) from students where sid>1;
    -> END;
    -> //
Query OK, 0 rows affected (0.11 sec)

mysql> delimiter ;
mysql> call simpleproc2 ();
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

mysql> show create procedure simpleproc;
+------------+-----------+-------------------+----------+---------+-----------+
| Procedure  | sql_mode  | Create Procedure  | character_set_client | collation_connection | Database Collation |
+------------+-----------+-------------------+----------+---------+-----------+
| simpleproc | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | 
CREATE DEFINER=`root`@`localhost` PROCEDURE `simpleproc`(in param1 int,out param2 int) BEGIN select count(*) into param2 from students where sid>param1; END
| utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci | +------------+-----------+-------------------+----------+---------+-----------+ 1 row in set (0.00 sec)
mysql> delimiter //
mysql> create function hello (s char(20))
    -> returns char(50)
    -> return concat('Hello, ',s,'!');
    -> //
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
                  : 該函數聲明中沒有確定性、沒有SQL或讀取SQL數據,並且啟用了二進制日志記錄(您*可能*希望使用不太安全的log_bin_trust_function_creator變量)
mysql
> delimiter ; mysql> show variables like '%bin%'; +--------------------------------------------+-------------------------------------+ | Variable_name | Value | +--------------------------------------------+-------------------------------------+ | bind_address | * | | binlog_cache_size | 32768 | | binlog_checksum | CRC32 | | binlog_direct_non_transactional_updates | OFF | | binlog_error_action | ABORT_SERVER | | binlog_expire_logs_seconds | 2592000 | | binlog_format | ROW | | binlog_group_commit_sync_delay | 0 | | binlog_group_commit_sync_no_delay_count | 0 | | binlog_gtid_simple_recovery | ON | | binlog_max_flush_queue_time | 0 | | binlog_order_commits | ON | | binlog_row_image | FULL | | binlog_row_metadata | MINIMAL | | binlog_row_value_options | | | binlog_rows_query_log_events | OFF | | binlog_stmt_cache_size | 32768 | | binlog_transaction_dependency_history_size | 25000 | | binlog_transaction_dependency_tracking | COMMIT_ORDER | | innodb_api_enable_binlog | OFF | | log_bin | ON | | log_bin_basename | /data/mysql/mysql/data/binlog | | log_bin_index | /data/mysql/mysql/data/binlog.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | log_statements_unsafe_for_binlog | ON | | max_binlog_cache_size | 18446744073709547520 | | max_binlog_size | 1073741824 | | max_binlog_stmt_cache_size | 18446744073709547520 | | mysqlx_bind_address | * | | sql_log_bin | ON | | sync_binlog | 1 | +--------------------------------------------+-------------------------------------+ 32 rows in set (0.01 sec) mysql> show variables like '%trust%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin_trust_function_creators | OFF | +---------------------------------+-------+ 1 row in set (0.01 sec) mysql> set global log_bin_trust_function_creators=on; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%trust%'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin_trust_function_creators | ON | +---------------------------------+-------+ 1 row in set (0.00 sec) mysql> delimiter // mysql> create function hello (s char(20)) -> returns char(50) -> return concat('Hello, ',s,'!'); -> // Query OK, 0 rows affected (0.34 sec) mysql> delimiter ; mysql> select hello('a'); +------------+ | hello('a') | +------------+ | Hello, a! | +------------+ 1 row in set (0.01 sec) mysql> select * from teacher; +----+-----------+---------+ | id | name | dept_id | +----+-----------+---------+ | 1 | Zhang san | 1 | | 2 | Li si | 1 | | 3 | Wang wu | 2 | | 4 | Liu liu | 3 | | 5 | Ding qi | 3 | +----+-----------+---------+ 5 rows in set (0.00 sec) mysql> select hello(name) from teacher; +-------------------+ | hello(name) | +-------------------+ | Hello, Zhang san! | | Hello, Li si! | | Hello, Wang wu! | | Hello, Liu liu! | | Hello, Ding qi! | +-------------------+ 5 rows in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update teacher set name=hello(name); Query OK, 5 rows affected (0.00 sec) Rows matched: 5 Changed: 5 Warnings: 0 mysql> select * from teacher; +----+-------------------+---------+ | id | name | dept_id | +----+-------------------+---------+ | 1 | Hello, Zhang san! | 1 | | 2 | Hello, Li si! | 1 | | 3 | Hello, Wang wu! | 2 | | 4 | Hello, Liu liu! | 3 | | 5 | Hello, Ding qi! | 3 | +----+-------------------+---------+ 5 rows in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.02 sec) mysql> select * from teacher; +----+-----------+---------+ | id | name | dept_id | +----+-----------+---------+ | 1 | Zhang san | 1 | | 2 | Li si | 1 | | 3 | Wang wu | 2 | | 4 | Liu liu | 3 | | 5 | Ding qi | 3 | +----+-----------+---------+ 5 rows in set (0.00 sec)
delimiter //
create procedure simpleproc2 ()
select count(*) from students where sid>1;
//
delimiter ;

delimiter //
create procedure simpleproc2 ()
select count(*) from students where sid>1;
select count(*) from students where sid>2;
//
delimiter ;

delimiter //
create procedure simpleproc2 ()
begin
select count(*) from students where sid>1;
select count(*) from students where sid>2;
end;
//
delimiter ;
mysql> drop procedure simpleproc2;
Query OK, 0 rows affected (0.09 sec)

mysql> delimiter //
mysql> create procedure simpleproc2 ()
    -> select count(*) from students where sid>1;
    -> //
Query OK, 0 rows affected (0.09 sec)

mysql> delimiter ;
mysql> 
mysql> call simpleproc2();
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> drop procedure simpleproc2;
Query OK, 0 rows affected (0.03 sec)

mysql> delimiter //
mysql> create procedure simpleproc2 ()
    -> select count(*) from students where sid>1;
    -> select count(*) from students where sid>2;
    -> //
Query OK, 0 rows affected (0.13 sec)

+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.13 sec)

mysql> delimiter ;
mysql> 
mysql> call simpleproc2();
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show create procedure simpleproc2;
+-------------+-------------+---------------------+----------------------+----------------------+--------------------+
| Procedure   | sql_mode    | Create Procedure    | character_set_client | collation_connection | Database Collation |
+-------------+-------------+---------------------+----------------------+----------------------+--------------------+
| simpleproc2 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | 
CREATE DEFINER=`root`@`localhost` PROCEDURE `simpleproc2`() select count(*) from students where sid>1;
| utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci | +-------------+-------------+---------------------+----------------------+----------------------+--------------------+ 1 row in set (0.00 sec) mysql> drop procedure simpleproc2; Query OK, 0 rows affected (0.13 sec) mysql> delimiter // mysql> create procedure simpleproc2 () -> begin -> select count(*) from students where sid>1; -> select count(*) from students where sid>2; -> end; -> // Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> show create procedure simpleproc2; +-------------+-----------+------------------+---------------+---------------+--------------------+ | Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation | +-------------+-----------+------------------+---------------+---------------+--------------------+ | simpleproc2 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
CREATE DEFINER=`root`@`localhost` PROCEDURE `simpleproc2`() begin select count(*) from students where sid>1; select count(*) from students where sid>2; end
| utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci | +-------------+-----------+-----------------+----------------+---------------+--------------------+ 1 row in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM