CREATE PROCEDURE Commonpageproc (
In P_tablename VARCHAR (1024),
In P_fields VARCHAR (1024),
In P_pagesize INT,
In P_pageindex INT,
In P_orderfield VARCHAR (128),
In P_sqlwhere VARCHAR (1024),
Out P_totalrecord INT
)
Not deterministic
SQL SECURITY Definer
COMMENT ' Paging stored procedure '
BEGIN
/* Define variable */
DECLARE M_begin_row INT DEFAULT 0;
DECLARE m_limit_string CHAR (64);
/* Construct statement */
SET M_begin_row = (p_pageindex-1) * p_pagesize;
SET m_limit_string = CONCAT (' Limit ', M_begin_row, ', ', p_pagesize);
if (p_sqlwhere<> ") Then
Set P_sqlwhere=concat (' where ', p_sqlwhere);
End If;
If P_orderfield<> ' Then
Set P_orderfield=concat (' ORDER by ', P_orderfield);
End If;
SET @COUNT_STRING = CONCAT (' SELECT COUNT (*) into @ROWS_TOTAL from ', P_tablename, ', p_sqlwhere);
SET @MAIN_STRING = CONCAT (' SELECT ', P_fields, ' from ', P_tablename, ' ', P_sqlwhere, ', P_orderfield, m_limit_string);
/* Pre-treatment */
PREPARE count_stmt from @COUNT_STRING;
EXECUTE count_stmt;
Deallocate PREPARE count_stmt;
SET P_totalrecord = @ROWS_TOTAL;
PREPARE main_stmt from @MAIN_STRING;
EXECUTE main_stmt;
Deallocate PREPARE main_stmt;
END
MySQL Paging stored procedure