Home  >  Article  >  Database  >  Mysql那些事儿之(十二)存储过程_MySQL

Mysql那些事儿之(十二)存储过程_MySQL

WBOY
WBOYOriginal
2016-06-01 13:39:01988browse

bitsCN.com


Mysql那些事儿之(十二)存储过程

 

相关链接:

Mysql那些事儿之(一)mysql的安装

http:///database/201210/162314.html;

Mysql那些事儿之(二)有关数据库的操作

http:///database/201210/162315.html;

Mysql那些事儿之(三)有关数据表的操作

http:///database/201210/162316.html;

Mysql那些事儿之(四)数据表数据查询操作

http:///database/201210/162317.html;

Mysql那些事儿之(五)操作时间

http:///database/201210/162318.html;

Mysql那些事儿之(六)字符串模式匹配

http:///database/201210/163969.html;

Mysql那些事儿之(七)深入select查询

http:///database/201210/163970.html;

Mysql那些事儿之(八)索引

http:///database/201210/163971.html;

Mysql那些事儿之(九)常用的函数

http:///database/201210/164229.html;

Mysql那些事儿之(十)触发器一

http:///database/201210/164516.html

Mysql那些事儿之(十一)触发器二

http:///database/201210/164766.html

 

存储过程是经过编译之后存放在数据库中的sql语句集合。

存储过程怎么写

看例子:

存储过程的语法

Sql代码  

create procedure proc_name (proc_peremeter1,.....)  --存储过程名称和参数  

[characteristic ....]   

routine_body  --存储过程   

调用存储过程的语法

Sql代码  

call proc_name  --调用存储过程  

示例 创建存储过程

当然创建存储过程之前先建立相关的表,为了学习只建立一个简单的表结构;

Sql代码  

CREATE TABLE filmall (  

  id smallint(5) NOT NULL,  

  film_id smallint(6) NOT NULL,  

  name varchar(40) DEFAULT NULL,  

  store_id smallint(6) NOT NULL,  

  txt text,  

  PRIMARY KEY (`id`)  

) ;      

在数据表里插入数据。

写一个简单的存储过程:

Sql代码  

--存储过程的名称为proc_film_store,参数为3个:前面两个为输入,后面一个为输出  

Sql代码  

delimiter $$  

Sql代码  

create procedure proc_film_store (IN p_film_id INT,IN p_store_id INT,OUT p_film_count INT)  

BEGIN  

SELECT txt FROM filmall WHERE film_id = p_film_id AND store_id = p_store_id;  

SELECT FOUND_ROWS() INTO p_film_count;  --将条数放入变量中  

END $$  

Sql代码  

delimiter ;  

 现在就可以调用存储过程了。

Sql代码  

call proc_film_store(1,1,@a);---输入参数  

--执行完之后会输出查询出来的数据  

 

--输出查询到的条数  

select @a;  

 存储过程就OK了。

Sql代码  

--还有一点要说明的是,书写存储过程时,一般会用到如下命令  

--书写存储过程之前,改变结束符  

delimiter $$  --这个语句的意思是将结尾符;替换为$$  

--写完存储过程之后,再将结束符改回来  

delimiter ;  

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn