使用 Procedure with Loop 删除 SQL 中的记录
Delete records in SQL using the Procedure with Loop
我正在尝试使用 SQL 的 DELETE 过程从数据库中删除 1000 万条记录以及更多记录。我想删除循环中的记录,就像每个交互记录一样,应该像下面这样删除 1。
drop procedure if exists PROC_WEEKLY_TASK_DELETE;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `PROC_WEEKLY_TASK_DELETE`()
BEGIN
set @rows = (select count(*) from MY_TABLE where IS_VALID_DATE='false');
set @a = 0;
while(@a<@rows)
do delete from MY_TABLE where IS_VALID_DATE='false' limit 1;
set @a = (@a+1);
commit;
end while;
END
//
DELIMITER ;
我正在使用上述程序删除记录,但出现了类似语法的问题
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1
请帮我创建一个循环删除记录的程序。谢谢
特别感谢@Akina 的所有建议
请检查我删除记录的新程序。
drop procedure if exists PROC_WEEKLY_TASK_DELETE_LIMIT;
DELIMITER $$
CREATE DEFINER=root@localhost PROCEDURE PROC_WEEKLY_TASK_DELETE_LIMIT()
BEGIN
repeat delete from MY_TABLE where IS_VALID_DATE = 'false' limit 10000;
until not row_count()
end repeat;
END$$
DELIMITER ;
Above procedure runs successfully on local but in case of server you need to remove the DEFINER only
我正在尝试使用 SQL 的 DELETE 过程从数据库中删除 1000 万条记录以及更多记录。我想删除循环中的记录,就像每个交互记录一样,应该像下面这样删除 1。
drop procedure if exists PROC_WEEKLY_TASK_DELETE;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `PROC_WEEKLY_TASK_DELETE`()
BEGIN
set @rows = (select count(*) from MY_TABLE where IS_VALID_DATE='false');
set @a = 0;
while(@a<@rows)
do delete from MY_TABLE where IS_VALID_DATE='false' limit 1;
set @a = (@a+1);
commit;
end while;
END
//
DELIMITER ;
我正在使用上述程序删除记录,但出现了类似语法的问题
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1
请帮我创建一个循环删除记录的程序。谢谢
特别感谢@Akina 的所有建议
请检查我删除记录的新程序。
drop procedure if exists PROC_WEEKLY_TASK_DELETE_LIMIT;
DELIMITER $$
CREATE DEFINER=root@localhost PROCEDURE PROC_WEEKLY_TASK_DELETE_LIMIT()
BEGIN
repeat delete from MY_TABLE where IS_VALID_DATE = 'false' limit 10000;
until not row_count()
end repeat;
END$$
DELIMITER ;
Above procedure runs successfully on local but in case of server you need to remove the DEFINER only