2013-12-18 125 views
0

我有以下查詢哪些推入id到臨時表中,並通過加入臨時表從另一個表中刪除記錄。 我正在使用限制來優化日誌文件沖洗。MySQL查詢返回錯誤消息

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; 

我在這裏得到語法錯誤。 有人可以幫忙,這裏有什麼問題嗎?

+6

如果包含實際的語法錯誤,它會讓您和未來訪問者更容易解決此問題。 –

回答

0

我們需要一個實際的錯誤信息來幫助你,儘管從你發佈的內容中我們可以看到一件事 - 肯定會丟失';'在結束之前結束後WHILE;

+0

謝謝你們!我創建了一個程序,它工作。還通過添加DO來修正WHILE的語法。 – user3115065

+0

非常感謝。在stackoverflow表達感謝的最好方法是通過投票;) –

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; 
+0

實際上這個END已經開始 - 它在WHILE之後的線上。 –

+0

哦,是的。謝謝。但是這兩條線都不需要,這可以從我鏈接的手動條目中看出。 – fancyPants