CREATE PROCEDURE ProcPage(in tableName varchar(20),#表名 in showField varchar(100),#要显示的列名 in whereText varchar(500),#where条件(只需要写where后面的语句) in orderText varchar(500),#排序条件(只需要写order by后面的语句) in pageSize int,#每一页显示的记录数 in pageIndex int,#当前页 out dataCount int#总记录数 )
BEGIN
if (pageSize<1)then
set pageSize=20;
end if;
if (pageIndex < 1)then
set pageIndex = 1;
end if;
if(LENGTH(whereText)>0)then
set whereText=CONCAT(' where 1=1 ',whereText);
end if;
if(LENGTH(orderText)>0)then
set orderText = CONCAT(' ORDER BY ',orderText);
end if;
set @strsql = CONCAT('select ',showField,' from ',tableName,' ',whereText,' ',orderText,' limit ',pageIndex*pageSize-pageSize,',',pageSize);
prepare stmtsql from @strsql;
execute stmtsql;
deallocate prepare stmtsql;
set @strsqlcount=concat('select count(1) as count into @datacount from ',tableName,'',whereText);
prepare stmtsqlcount from @strsqlcount;
execute stmtsqlcount;
deallocate prepare stmtsqlcount;
set datacount=@datacount;
END;