The mysql stored procedure retrieves the return value in dynamic SQL

  • 2020-05-19 06:04:24
  • OfStack

MySql general paging stored procedure
Process parameters

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);
 # Concatenation queries the total record SQL statements 
   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;
   # To obtain the dynamic SQL Statement return value 
  set p_recordcount = @recordcount;
  # Calculate the total number of pages based on the total number of record hops 
   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;
   # Splice the dynamics of paging query records SQL statements 
   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 $


# Concatenation queries the total record SQL statements    
set v_sqlcounts = concat('select count(*) into @recordcount from ',v_tables,v_where);   
set @sqlcounts := v_sqlcounts;   
# Preprocessing performance SQL   
prepare stmt from @sqlcounts;   
# Pass the dynamic SQL Within the parameters of the    
set @s1= categoryid;   
execute stmt using @s1;   
deallocate prepare stmt;   
# To obtain the dynamic SQL Statement return value    
set recordcount = @recordcount;  

# concatenate SQL statements to query the total record
set v_sqlcounts = concat('select count(*) into @recordcount from ',v_tables,v_where);
set @sqlcounts := v_sqlcounts;
# preprocess dynamic SQL
prepare stmt from @sqlcounts;
Pass dynamic SQL internal parameters
set @s1= categoryid; execute stmt using @s1; deallocate prepare stmt;
Gets the return value of the dynamic SQL statement
set recordcount = @recordcount;
Above, I will do the stored procedure paging using dynamic SQL and put the number of count records queried into recordcount via the variable @recordcount.
The judgment of mysql's IF ELSE and other databases is a little different. The simple judgment statement is as follows.

# Calculate the total number of pages based on the total number of record hops    
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;  

Calculate the total number of pages 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;

Related articles: