Procedural memory
-
Stored procedures, to accomplish a particular function set, after storing the word order sets compiled SQL database
-
Flexibility: can be stored during the flow control cycle of operation and to complete the complex calculation and determination
-
Consistency: you can make some operations associated with the process occurs by storing, so as to maintain the integrity of the database
- Efficiency: stored procedures effectively reduce the workload of developers and programmers to database
-
-
grammar
CREATE PROCEDURE SP_NAME(IN PRAM TYPE, OUT PRAM TYPE); IN 表示传入参数 默认不写时表示传入 TYPE 表示类型 OUT 表示传出参数 TYPE表示类型 INOUT 可以使一个传入参数在存储过程中被修改 并传出 存储过程内部语句以”;”结尾,因此在定义存储过程中需要切换控制台的命令结束符号 以避免歧义,可以使用DELIMITER //
- transfer
调用存储过程使用 CALL SP_NAME()
删除存储过程 DROP PROCEDURE SP_NAME
查看已定义存储过程 SHOW PROCEDURE STATUS/SHOW CREATE PROCEDURE SP_NAME(详细信息)
- Create a database
create database if not exists demo1 default character set 'utf8';
- Create a data table
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- Defined end symbol
-- 定义 结束符
DELIMETER $$
- No-argument
CREATE PROCEDURE loop_insert_post()
BEGIN
DECLARE i INT;
SET i = 1;
WHILE i<1000 DO
INSERT INTO user(`name`,`addtime`)values(concat('JM',i),now());
SET i = i+1;
END WHILE;
END $$
- call function
-- 调用函数
CALL loop_insert_post //
- Mysql restore default terminator
DELIMETER ;