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