MySQL查询返回错误消息

时间:2013-12-18 11:44:45

标签: mysql while-loop

我有以下查询将id推入临时表并通过加入临时表从另一个表中删除记录。 我正在使用limit来优化日志文件刷新。

SET @StartDate='2012-12-26';
SET @EndDate='2014-01-11';

INSERT INTO temp_A
SELECT id FROM A
WHERE created > @StartDate  AND created < @EndDate
ORDER BY created;

SET @recordNotFound =0;

WHILE(@recordNotFound = 0)
BEGIN
DELETE 
FROM B
WHERE EXISTS
      (SELECT t2.id
       FROM temp_A AS t2
       WHERE t2.id = B.txn_id
) 
ORDER BY txn_id DESC
LIMIT 1000;

IF (ROW_COUNT() <> 1000) THEN
SET @recordNotFound=1;
END IF;
END
END WHILE;

我在这里遇到语法错误。 有人可以帮忙,这里出了什么问题?

2 个答案:

答案 0 :(得分:0)

我们需要一条实际的错误消息才能帮助您,但是根据您发布的内容,我们可以确定一件事 - 缺少';'在END WHILE之后的END之后;

答案 1 :(得分:0)

请查看manual entry for while

SET @StartDate='2012-12-26';
SET @EndDate='2014-01-11';

INSERT INTO temp_A
SELECT id FROM A
WHERE created > @StartDate  AND created < @EndDate
ORDER BY created;

SET @recordNotFound =0;

WHILE (@recordNotFound = 0) DO /*you're missing a DO here*/
#BEGIN /*you don't need this*/
DELETE 
FROM B
WHERE EXISTS
      (SELECT t2.id
       FROM temp_A AS t2
       WHERE t2.id = B.txn_id
) 
ORDER BY txn_id DESC
LIMIT 1000;

IF (ROW_COUNT() <> 1000) THEN
SET @recordNotFound=1;
END IF;
#END /*this line you don't need either*/
END WHILE;