mysql存储过程是各种数据库内置的一个功能,它可以提高数据库各种性能,下面我来给大家分享一下我学习存储过程的些知识,大家有兴趣一起来看看.
今天又把mysql存储过程学习了下,大家先看以下代码,对语法不懂的朋友,可以详细看下语法结构,代码如下:
- CREATE PROCEDURE and CREATE FUNCTION 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: --Vevb.com
- Valid SQL routine statement
例,代码如下:
- DELIMITER $$ /*改变语句的结束符*/
- USE `test`$$ /*选择数据库*/
- DROP PROCEDURE IF EXISTS `outgo`$$ /*存在outgo存储过程就删除*/
- CREATE DEFINER=`root`@`%` PROCEDURE `outgo`(IN v_table CHAR(10), IN v_id INT(2), OUT v_value VARCHAR(32))
调用存储过程的语法是call.如下:
- mysql> call outgo('user', 2, @a);
- +----+--------------+
- | id | title |
- +----+--------------+
- | 2 | 你大爷的 |
- +----+--------------+
- 1 row in set (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- mysql> call outgo('user', 1, @a);
- +----+--------------------+
- | id | title |
- +----+--------------------+
- | 1 | 我来测试一记 |
- +----+--------------------+
- 1 row in set (0.00 sec)
其中调用中文的时候可能会出现Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (latin1_swedish_ci,NUMERIC), (utf8_general_ci,COERCIBLE) for operation 'concat'这是由于编码不一致导致的。
新闻热点
疑难解答