mysql 动态表分页存储过程

1、编写存储过程

DELIMITER $$

USE `ry_cloud`$$

DROP PROCEDURE IF EXISTS `proc_nifi_user`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_nifi_user`(IN table_name VARCHAR(255),IN page INT,IN pageSize INT,OUT total INT,OUT totalPage INT)
BEGIN
    DECLARE m INT;
    SET m=(page-1)*pageSize;
    SET @sqlStmt = CONCAT('SELECT * FROM ', table_name,' limit ',m,',',pageSize);
    SET @sqlStmtCount = CONCAT('SELECT COUNT(*) INTO @total FROM ',table_name);
    PREPARE stmt FROM @sqlStmt;
    PREPARE stmtCount FROM @sqlStmtCount;
    EXECUTE stmt;
    EXECUTE stmtCount;
    DEALLOCATE PREPARE stmt;
    DEALLOCATE PREPARE stmtCount;
    SET total =@total;
    SET totalPage = CEILING(total*1.0/pageSize);
    END$$

DELIMITER ;

2、调用测试存储过程

SET @total=0;
SET @totalPage=0;
CALL proc_nifi_user('nifi_user',1,100,@total,@totalPage);
SELECT @total;
SELECT @totalPage;

你可能感兴趣的:(数据库)