感谢每一位启发我这个存储过程的人!我很高兴与您分享:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `split_tablebyrowscnt` (IN `tableName` VARCHAR(40), IN `step` INT) BEGIN
SET @table := tableName;
SET @liminf := 0;
SET @limsup := step;
SET @sql_2 = CONCAT('SELECT COUNT(*) INTO @rwcnt FROM ',@table,';');
PREPARE stmt from @sql_2;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
WHILE @liminf<@rwcnt DO
SET @sql_1 = CONCAT('SELECT CAST(',@limsup,' as char(10)) INTO @limsup_str;');
PREPARE stmt from @sql_1;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @sql_loop =CONCAT('CREATE TABLE ',@table,'_',@limsup_str,' SELECT * FROM(SELECT @rownum:=@rownum+1 rownum,d.* FROM (',@table,' d, (SELECT @rownum:=0) r))t
WHERE ( rownum >?) AND (rownum <= ?);');
PREPARE stmt from @sql_loop;
EXECUTE stmt USING @liminf,@limsup;
DEALLOCATE PREPARE stmt;
SET @sql_drop = CONCAT('ALTER TABLE ',@table,'_',@limsup_str,' DROP COLUMN rownum;');
PREPARE stmt from @sql_drop;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @liminf = @liminf + step;
SET @limsup = @limsup + step;
END WHILE ;
END$$
DELIMITER ;
要执行该过程: CALL split_tablebyrowscnt('myTable',100)