- DELIMITER $$
- DROP PROCEDURE IF EXISTS `hcymysql`$$
- CREATE DEFINER=`admin`@`%` PROCEDURE `hcymysql`(IN v_tname VARCHAR(20))
- BEGIN
- DECLARE i INT;
- DECLARE done INT DEFAULT 0;
- DECLARE hcymysql_cursor CURSOR FOR SELECT id FROM view_tname;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
- DROP VIEW IF EXISTS view_tname ;
- SET @sql=CONCAT("create view view_tname as select * from ", v_tname);
- PREPARE stmt1 FROM @sql;
- EXECUTE stmt1 ;
- DEALLOCATE PREPARE stmt1;
- OPEN hcymysql_cursor;
- select_loop: LOOP
- FETCH hcymysql_cursor INTO i;
- IF done=1 THEN
- LEAVE select_loop;
- END IF;
- SELECT i;
- END LOOP select_loop;
- CLOSE hcymysql_cursor;
- END$$
- DELIMITER ;
执行:
- mysql> select * from t1;
- +------+------+
- | id | name |
- +------+------+
- | 1 | a |
- | 2 | b |
- | 3 | cc |
- | 4 | d |
- | 5 | e |
- | 6 | f |
- | 7 | g |
- | 8 | h |
- | 9 | i |
- | 9 | h |
- +------+------+
- 10 rows in set (0.03 sec)
- mysql> call hcymysql('t1');
- +------+
- | i |
- +------+
- | 1 |
- +------+
- 1 row in set (0.17 sec)
- +------+
- | i |
- +------+
- | 2 |
- +------+
- 1 row in set (0.19 sec)
- +------+
- | i |
- +------+
- | 3 |
- +------+
- 1 row in set (0.21 sec)
- +------+
- | i |
- +------+
- | 4 |
- +------+
- 1 row in set (0.23 sec)
- +------+
- | i |
- +------+
- | 5 |
- +------+
- 1 row in set (0.26 sec)
- +------+
- | i |
- +------+
- | 6 |
- +------+
- 1 row in set (0.29 sec)
- +------+
- | i |
- +------+
- | 7 |
- +------+
- 1 row in set (0.32 sec)
- +------+
- | i |
- +------+
- | 8 |
- +------+
- 1 row in set (0.35 sec)
- +------+
- | i |
- +------+
- | 9 |
- +------+
- 1 row in set (0.37 sec)
- +------+
- | i |
- +------+
- | 9 |
- +------+
- 1 row in set (0.40 sec)
- Query OK, 0 rows affected (0.43 sec)
本文转自hcymysql51CTO博客,原文链接: http://blog.51cto.com/hcymysql/1046929,如需转载请自行联系原作者