• notice
  • Congratulations on the launch of the Sought Tech site

Methodial method for NEW values ​​in dynamic SQL

MySql Universal Pagination Stored Procedure
Process parameters
The code is as follows:

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 statements for splicing and querying total records
   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;
   #Get dynamic SQL statement return value
  set [email protected];
  #Calculate the total number of pages based on the total record hops
   set p_pagecount=ce iling((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;
   #Dynamic SQL statement for splicing paging query records
   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 $

The code is as follows:

#Mosaic query Total recorded SQL statements  
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;  
#Get dynamic SQL statement return value  
set [email protected]

#SQL statements for splicing and querying total records
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;
#Get dynamic SQL statement return value
set [email protected];
I am on the above In the stored procedure paging, dynamic SQL is used to put the number of count records queried into recordcount through the variable @recordcount.The judgment of
mysql is a little different from that of other databases.The simple judgment sentence is as follows.
The code is as follows:

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

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+