mysql储存过程总结
原创1.
create PROCEDURE getRes(IN num int)
BEGIN
select * from t_r where id=num; //存储过程主题 请注意,Entry参数没有定义与查询条件字段相同的名称。 如果位于左侧,则不定义id
END
call getRes(10949);//调用存储过程
drop PROCEDURE getRes;//删除存储过程
2.
create PROCEDURE getRes(IN num int, //入参
out resName VARCHAR(100),//出参
out resSize int(10)//出参 )
BEGIN
select name into resName from t_resource where id=num;
select size into resSize from t_resource where id=num;
END
call getRes(10949,@resName,@resSize);//调用存储过程
SELECT @resName,@resSize;//返回值
3.
create PROCEDURE incrCount(INOUT count int,IN num int)//INOUT 输入和输出参数:表示调用方传递给过程的值和过程发送给调用方的值。
BEGIN
set count=count+num;
end
set @count=1;
call incrCount(@count,1);
SELECT @count;
- if else 语句
create PROCEDURE incra(IN num int)
BEGIN
DECLARE var int;
set var=num+1;
if var=1 then select 女; else SELECT 男; end if;
end
call incra(1)
5. case when 语句
create PROCEDURE incrc(IN num int)
BEGIN
DECLARE var int;
set var=num+1;
case var
when 1 then select 女; when 2 then SELECT 男; else SELECT 人妖 ;
end case;
end
call incrc(8)
6.mybatis 调用存储过程
<select id="count" parameterType="DevicePOJO" useCache="false"
statementType="CALLABLE"> //statementType=”CALLABLE” 必须为CALLABLE,告诉MyBatis执行存储过程
call countDevicesName(
{devoceName,mode=IN,jdbcType=VARCHAR}, //mode=IN 输入参数 mode=OUT输出参数 jdbcType为数据库定义的字段类型。
{deviceCount,mode=OUT,jdbcType=INTEGER});
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除