Home > Database > Mysql Tutorial > MySQL动态游标_MySQL

MySQL动态游标_MySQL

WBOY
Release: 2016-06-01 13:26:50
Original
1429 people have browsed it

bitsCN.com

通过(准备语句+视图+静态游标)实现

-- 建立测试表和数据create table webuser (username varchar(10));insert into webuser values ('a1'),('a2'),('a3'),('b1'),('b2'),('b3');commit;-- 建立存储过程drop procedure if exists dynamic_cursor;delimiter //CREATE PROCEDURE dynamic_cursor (IN p_name varchar(10))BEGIN     DECLARE done INT DEFAULT 0;    DECLARE v_username varchar(10);    DECLARE cur CURSOR for( SELECT username from webuser_view);    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;    DROP VIEW IF EXISTS webuser_view;    SET @sqlstr = "CREATE VIEW webuser_view as ";    SET @sqlstr = CONCAT(@sqlstr , "SELECT username FROM webuser WHERE username like '", p_name,"%'");    PREPARE stmt FROM @sqlstr;    EXECUTE stmt;    DEALLOCATE PREPARE stmt;    OPEN cur;    f_loop:LOOP     FETCH cur INTO v_username;	IF done THEN 		LEAVE f_loop;  	END IF; 	SELECT v_username;        END LOOP f_loop;    CLOSE cur;END;//delimiter ;-- 测试call dynamic_cursor('a');
Copy after login

  

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template