DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_test`$$
CREATE PROCEDURE `test`.`sp_test`(IN table_name varchar(255),IN total int)
BEGIN
set @stmt = concat('select * from ',table_name,' limit ?');
prepare s1 from @stmt;
set @s = total;
execute s1 using @s;
deallocate prepare s1;
END$$
DELIMITER ;
select * from test.song limit 10;
query result(10 records)
id name datetime rank
1 woshiduide1 2007-10-03 00:27:11 9
2 woshiduide2 2007-10-03 00:27:11 9
3 woshiduide3 2007-10-03 00:27:11 4
4 woshiduide4 2007-10-03 00:27:11 6
5 woshiduide5 2007-10-03 00:27:11 7
6 woshiduide6 2007-10-03 00:27:11 5
7 woshiduide7 2007-10-03 00:27:11 3
8 woshiduide8 2007-10-03 00:27:11 9
9 woshiduide9 2007-10-03 00:27:11 6
10 woshiduide10 2007-10-03 00:27:11 4
call sp_test('test.song',10);
query result(10 records)
id name datetime rank
1 woshiduide1 2007-10-03 00:27:11 9
2 woshiduide2 2007-10-03 00:27:11 9
3 woshiduide3 2007-10-03 00:27:11 4
4 woshiduide4 2007-10-03 00:27:11 6
5 woshiduide5 2007-10-03 00:27:11 7
6 woshiduide6 2007-10-03 00:27:11 5
7 woshiduide7 2007-10-03 00:27:11 3
8 woshiduide8 2007-10-03 00:27:11 9
9 woshiduide9 2007-10-03 00:27:11 6
10 woshiduide10 2007-10-03 00:27:11 4
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sp_test`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test`(IN table_name varchar(255),
IN page_begin int,IN page_end int)
BEGIN
set @stmt = concat('select * from ',table_name,' limit ?,?');
prepare s1 from @stmt;
set @s1 = page_begin;
set @s2 = page_end;
execute s1 using @s1,@s2;
deallocate prepare s1;
END$$
DELIMITER ;
select * from cs_test;
call sp_test('cs_test',2,3);
结果:
query result(9 records)
id name
1 csdn1
2 colorful1
3 bbs1
4 cu1
5 cu2
6 woshiduide\n\n\n\n\n
7 woshicuode
8 I'm wrong
9 I'm right
query result(3 records)
id name
3 bbs1
4 cu1
5 cu2
|