Mysql stored procedure write paging program _ MySQL

Source: Internet
Author: User
Mysql stored procedure write paging program bitsCN.com
Mysql stored procedure write paging program/* paging program */delimiter // drop procedure if exists sp_page; create procedure sp_page (p_table_name varchar (1024), p_fields varchar (1024), p_page_size int, p_curr_page int, p_order_string varchar (256), p_where_string varchar (1024), out p_total_rows int) not deterministic SQL security definer comment 'Paging stored procedure 'begin/* variable declaration */declare v_start_row int default 0; declare v_limit_string varchar (256 ); /* assign variable values */if p_curr_page <1 then set p_curr_page = 1; end if; set @ rows_total = 0; set v_start_row = (p_curr_page-1) * p_page_size; set v_limit_string = concat ('limit', v_start_row, ',', p_page_size); set @ total_string = concat ('SELECT count (*) into @ rows_total from ', p_table_name, '', p_where_string); set @ query_string = concat ('select', p_fields, 'from', p_table_name,'', p_where_string, '', p_order_string,'', v_limit_string ); select @ total_string; select @ query_string;/* preprocessing */prepare stmt_count from @ total_string; execute prepare; deallocate prepare stmt_count; set region = @ rows_total; prepare stmt_query from @ query_string; execute stmt_query; deallocate prepare stmt_query; end; // call stored procedure parameter description the name of the table the number of records displayed on each page of the field to be queried the total number of records on the current page call sp_page ('t_score ','*', 10, 1, '','', @ total_rows) // bitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.