mysql存儲進程 在靜態SQL內獲得前往值的辦法詳解。本站提示廣大學習愛好者:(mysql存儲進程 在靜態SQL內獲得前往值的辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql存儲進程 在靜態SQL內獲得前往值的辦法詳解正文
MySql通用分頁存儲進程
進程參數
p_cloumns varchar(500),p_tables varchar(100),p_where varchar(4000),p_order varchar(100),p_pageindex int,p_pagesize int,out p_recordcount int,out p_pagecount int
$:begin
declare v_sqlcounts varchar(4000);
declare v_sqlselect varchar(4000);
#拼接查詢總記載的SQL語句
set v_sqlcounts = concat('select count(*) into @recordcount from ',p_tables,p_where);
#select v_sqlcounts;leave $;
set @sqlcounts = v_sqlcounts;
prepare stmt from @sqlcounts;
execute stmt;
deallocate prepare stmt;
#獲得靜態SQL語句前往值
set p_recordcount = @recordcount;
#依據總記載跳數盤算出總頁數
set p_pagecount = ceiling((p_recordcount+0.0)/p_pagesize);
if p_pageindex <1 then
set p_pageindex = 1;
elseif p_pageindex > p_pagecount and p_pagecount <> 0 then
set p_pageindex = p_pagecount;
end if;
#拼接分頁查詢記載的靜態SQL語句
set v_sqlselect = concat('select ',p_cloumns,' from ',p_tables,p_where,if(p_order is not null,p_order,''),' limit ',(p_pageindex-1)*p_pagesize,' , ',p_pagesize);
#select v_sqlselect;leave $;
set @sqlselect = v_sqlselect;
prepare stmtselect from @sqlselect;
execute stmtselect;
deallocate prepare stmtselect;
end $
#拼接查詢總記載的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語句
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和其他數據庫的斷定有點紛歧樣,簡略的斷定語句以下。
#依據總記載跳數盤算出總頁數
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;
#依據總記載跳數盤算出總頁數 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;