DECLARE totalMoney INT DEFAULT 0;
DECLARE userId BIGINT(20) UNSIGNED;
DECLARE c_iId BIGINT(20) UNSIGNED;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE isExists TINYINT;
DECLARE cur CURSOR FOR SELECT iId,uId FROM `t_member_points_input_record` WHERE bussinessType=4;
-- declare continue handler for not found set done=true;
DROP TEMPORARY TABLE IF EXISTS abcs;
CREATE TEMPORARY TABLE IF NOT EXISTS abcs(
uId BIGINT(20) UNSIGNED,
tradePoints INT(11)
)ENGINE = HEAP;
TRUNCATE TABLE abcs;
OPEN cur;
cur_loop:LOOP
FETCH cur INTO c_iId,userId;
IF userId IS NULL THEN LEAVE cur_loop;END IF;
SELECT 1 INTO isExists FROM abcs WHERE uId=userId LIMIT 1;
SELECT tradePoints*0.5 INTO totalMoney FROM `t_member_points_input_record` WHERE iId=c_iId LIMIT 1;
IF isExists=1 THEN
UPDATE abcs SET tradePoints=tradePoints+totalMoney WHERE uId=userId LIMIT 1;
ELSE
-- select userId,totalMoney;
INSERT INTO abcs VALUES(userId,totalMoney);
END IF;
-- SELECT * FROM abcs;
SET isExists = 0;
-- set userId = null;
END LOOP;
CLOSE cur;
SELECT * FROM abcs;
t_member_points_input_record表是用户的收入表,我想将这里面的金额全部减半后插入临时表作他用。
但是以上代码运行之后无输出。
DECLARE userId BIGINT(20) UNSIGNED;
DECLARE c_iId BIGINT(20) UNSIGNED;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE isExists TINYINT;
DECLARE cur CURSOR FOR SELECT iId,uId FROM `t_member_points_input_record` WHERE bussinessType=4;
-- declare continue handler for not found set done=true;
DROP TEMPORARY TABLE IF EXISTS abcs;
CREATE TEMPORARY TABLE IF NOT EXISTS abcs(
uId BIGINT(20) UNSIGNED,
tradePoints INT(11)
)ENGINE = HEAP;
TRUNCATE TABLE abcs;
OPEN cur;
cur_loop:LOOP
FETCH cur INTO c_iId,userId;
IF userId IS NULL THEN LEAVE cur_loop;END IF;
SELECT 1 INTO isExists FROM abcs WHERE uId=userId LIMIT 1;
SELECT tradePoints*0.5 INTO totalMoney FROM `t_member_points_input_record` WHERE iId=c_iId LIMIT 1;
IF isExists=1 THEN
UPDATE abcs SET tradePoints=tradePoints+totalMoney WHERE uId=userId LIMIT 1;
ELSE
-- select userId,totalMoney;
INSERT INTO abcs VALUES(userId,totalMoney);
END IF;
-- SELECT * FROM abcs;
SET isExists = 0;
-- set userId = null;
END LOOP;
CLOSE cur;
SELECT * FROM abcs;
t_member_points_input_record表是用户的收入表,我想将这里面的金额全部减半后插入临时表作他用。
但是以上代码运行之后无输出。