2008年(8065)
分类: 服务器与存储
2008-04-29 08:22:20
DELIMITER $$;
DROP PROCEDURE IF EXISTS `mytest`.`MyPage`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MyPage`(
tableName varchar(100),
fieldsName VARCHAR(100),
pageIndex int,
pageSize int,
sortName VARCHAR(500),
strWhere varchar(500)
)
BEGIN
DECLARE fieldlist VARCHAR(200);
if fieldsName=''||fieldsName=null THEN
set fieldlist='*';
else
set fieldlist=fieldsName;
end if;
if strWhere=''||strWhere=null then
if sortName=''||sortName=null then
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' limit ',(pageindex-1)*pageSize,',',pageSize);
else
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' order by ',sortName,' limit ',(pageindex-1)*pageSize,',',pageSize);
end if;
else
if sortName=''||sortName=null then
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' where ',strWhere,' limit ',(pageindex-1)*pageSize,',',pageSize);
else
set @strSQL=concat('select ',fieldlist,' from ' , tableName,' where ',strWhere,' order by ',sortName,' limit ',(pageindex-1)*pageSize,',',pageSize);
end if;
end if;
PREPARE stmt1 FROM @strSQL;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END$$
DELIMITER ;$$