文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>mysql 预处理存储过程

mysql 预处理存储过程

时间:2010-05-03  来源:ubuntuer

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


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载