shell 获取mysql存储过程返回值_mysql存储过程 在动态SQL内获取返回值

这篇博客介绍了如何在MySQL存储过程中通过动态SQL获取查询的记录数,并将其存储到变量中,同时展示了如何进行分页计算和条件判断。文章包含具体的SQL代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很简单,不需要使用游标循环来读取,直接select * into @变量就OK了,还可以传递参数来执行。

1

f82d653e25f5b2ae14305a4a7b95f6b2.gifMySql通用分页存储过程2f82d653e25f5b2ae14305a4a7b95f6b2.gif3f82d653e25f5b2ae14305a4a7b95f6b2.gif过程参数4f82d653e25f5b2ae14305a4a7b95f6b2.gifp_cloumnsvarchar(500),p_tablesvarchar(100),p_wherevarchar(4000),p_ordervarchar(100),p_pageindexint,p_pagesizeint,out p_recordcountint,out p_pagecountint5f82d653e25f5b2ae14305a4a7b95f6b2.gif6f82d653e25f5b2ae14305a4a7b95f6b2.gif$$:begin7f82d653e25f5b2ae14305a4a7b95f6b2.gifdeclarev_sqlcountsvarchar(4000);8f82d653e25f5b2ae14305a4a7b95f6b2.gifdeclarev_sqlselectvarchar(4000);9f82d653e25f5b2ae14305a4a7b95f6b2.gif #拼接查询总记录的SQL语句10f82d653e25f5b2ae14305a4a7b95f6b2.gifsetv_sqlcounts=concat('select count(*) into @recordcount from',p_tables,p_where);11f82d653e25f5b2ae14305a4a7b95f6b2.gif #selectv_sqlcounts;leave $$;12f82d653e25f5b2ae14305a4a7b95f6b2.gifset@sqlcounts=v_sqlcounts;13f82d653e25f5b2ae14305a4a7b95f6b2.gifpreparestmtfrom@sqlcounts;14f82d653e25f5b2ae14305a4a7b95f6b2.gifexecutestmt;15f82d653e25f5b2ae14305a4a7b95f6b2.gifdeallocatepreparestmt;16f82d653e25f5b2ae14305a4a7b95f6b2.gif #获取动态SQL语句返回值17f82d653e25f5b2ae14305a4a7b95f6b2.gifsetp_recordcount=@recordcount;18f82d653e25f5b2ae14305a4a7b95f6b2.gif #根据总记录跳数计算出总页数19f82d653e25f5b2ae14305a4a7b95f6b2.gifsetp_pagecount=ceiling((p_recordcount+0.0)/p_pagesize);20f82d653e25f5b2ae14305a4a7b95f6b2.gififp_pageindex<1then21f82d653e25f5b2ae14305a4a7b95f6b2.gifsetp_pageindex=1;22f82d653e25f5b2ae14305a4a7b95f6b2.gif elseif p_pageindex>p_pagecountandp_pagecount<>0then23f82d653e25f5b2ae14305a4a7b95f6b2.gifsetp_pageindex=p_pagecount;24f82d653e25f5b2ae14305a4a7b95f6b2.gifendif;25f82d653e25f5b2ae14305a4a7b95f6b2.gif #拼接分页查询记录的动态SQL语句26f82d653e25f5b2ae14305a4a7b95f6b2.gifsetv_sqlselect=concat('select',p_cloumns,'from',p_tables,p_where,if(p_orderisnotnull,p_order,''),'limit',(p_pageindex-1)*p_pagesize,',',p_pagesize);27f82d653e25f5b2ae14305a4a7b95f6b2.gif #selectv_sqlselect;leave $$;28f82d653e25f5b2ae14305a4a7b95f6b2.gifset@sqlselect=v_sqlselect;29f82d653e25f5b2ae14305a4a7b95f6b2.gifpreparestmtselectfrom@sqlselect;30f82d653e25f5b2ae14305a4a7b95f6b2.gifexecutestmtselect;31f82d653e25f5b2ae14305a4a7b95f6b2.gifdeallocatepreparestmtselect;32f82d653e25f5b2ae14305a4a7b95f6b2.gifend$$

Sql代码

#拼接查询总记录的SQL语句

setv_sqlcounts = concat('select count(*) into @recordcount from ',v_tables,v_where);

set@sqlcounts := v_sqlcounts;

#预处理动态SQL

preparestmtfrom@sqlcounts;

#传递动态SQL内参数

set@s1= categoryid;

executestmt using @s1;

deallocatepreparestmt;

#获取动态SQL语句返回值

setrecordcount = @recordcount;

#拼接查询总记录的SQL语句

set v_sqlcounts = concat('select count(*) into @recordcount from ',v_tables,v_where);

set @sqlcounts := v_sqlcounts;

#预处理动态SQL

prepare stmt from @sqlcounts;

#传递动态SQL内参数

set @s1= categoryid;

execute stmt using @s1;

deallocate prepare stmt;

#获取动态SQL语句返回值

set recordcount = @recordcount;

以上我上再做存储过程分页里用到动态SQL里将查询到的count记录条数通过变量@recordcount放到recordcount里面了。

mysql的IF ELSE和其他数据库的判断有点不一样,简单的判断语句如下。

Sql代码

#根据总记录跳数计算出总页数

setpagecount = ceiling((recordcount+0.0)/pagesize);

if pageindex <1then

setpageindex = 1;

elseif pageindex > pagecountthen

setpageindex = pagecount;

else

selectpageindex,pagecount;

endif;

#根据总记录跳数计算出总页数

set pagecount = ceiling((recordcount+0.0)/pagesize);

if pageindex <1 then

set pageindex = 1;

elseif pageindex > pagecount then

set pageindex = pagecount;

else

select pageindex,pagecount;

end if;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值