日期:2014-05-16 浏览次数:20922 次
DELIMITER $$ USE 'dzhst2'$$ DROP PROCEDURE IF EXISTS `test_mwl`$$ CREATE PROCEDURE `test_mwl`() BEGIN DECLARE flag INT DEFAULT 0; DECLARE v_co VARCHAR(32); DECLARE v_c10 MEDIUMTEXT; DECLARE objs LONGTEXT; -- 定义游标 DECLARE myCursor CURSOR FOR (SELECT DISTINCT co,c10 FROM ST22059_main WHERE c10 IS NOT NULL); DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag=1; -- 创建临时表 DROP TABLE IF EXISTS tmp_table; CREATE TEMPORARY TABLE tmp_table( tmp_co VARCHAR(32), tmp_c10 MEDIUMTEXT )ENGINE=MEMORY; -- 打开游标 OPEN myCursor; -- 将游标指定的数据赋值给变量 FETCH myCursor INTO v_co,v_c10; REPEAT IF NOT flag THEN INSERT INTO tmp_table(tmp_co,tmp_c10) VALUES(v_co,v_c10); END IF; UNTIL flag END REPEAT; -- 关闭游标 CLOSE myCursor; -- 查询临时表 SET objs='SELECT DISTINCT tmp_co,tmp_c10 FROM tmp_table'; SET @sql_txt = objs; PREPARE stmt FROM @sql_txt; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- 清空临时表 TRUNCATE tmp_table; END$$ DELIMITER ;