日期:2014-05-16 浏览次数:20854 次
DROP TABLE IF EXISTS T_User; CREATE TABLE T_User( id INT PRIMARY KEY, age INT, tname VARCHAR(20) ); INSERT INTO T_User VALUE(1,11,'abc1'); INSERT INTO T_User VALUE(2,12,'abc2'); INSERT INTO T_User VALUE(3,13,'abc3'); INSERT INTO T_User VALUE(4,14,'abc4'); INSERT INTO T_User VALUE(5,15,'abc5'); INSERT INTO T_User VALUE(6,16,'abc6');
CREATE PROCEDURE curdemo() BEGIN DECLARE age1 INT; DECLARE id1 INT; DECLARE Cur CURSOR FOR SELECT id,age FROM T_User; OPEN Cur; FETCH Cur INTO id1,age1; WHILE @@fetch_status=0 BEGIN UPDATE T_User SET age=age1+1 WHERE id=id1; FETCH Cur INTO id1,age1; END CLOSE Cur; DEALLOCATE Cur; END ############################################################################ CREATE PROCEDURE curdemo1() BEGIN DECLARE age1 INT; DECLARE id1 INT; DECLARE Cur CURSOR FOR SELECT id,age FROM T_User; DECLARE EXIT HANDLER FOR NOT FOUND CLOSE Cur; OPEN Cur; REPEAT FETCH Cur INTO id1,age1; UPDATE T_User SET age=age1+1 WHERE id=id1; UNTIL done END REPEAT; CLOSE Cur; DEALLOCATE; END;
delimiter // CREATE PROCEDURE curdemo() BEGIN DECLARE age1 INT; DECLARE id1 INT; DECLARE Cur CURSOR FOR SELECT id,age FROM T_User; OPEN Cur; FETCH Cur INTO id1,age1; WHILE @@fetch_status=0 BEGIN UPDATE T_User SET age=age1+1 WHERE id=id1; FETCH Cur INTO id1,age1; END CLOSE Cur; DEALLOCATE Cur; END// delimiter ;