mysql动态执行sql批量删除数据

Posted hxlasky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql动态执行sql批量删除数据相关的知识,希望对你有一定的参考价值。

 

CREATE PROCEDURE `sp_delete_pushmsg_data`() 
BEGIN
declare l_delete_date varchar(16);
declare l_state_date  varchar(16);
declare l_dutyno      int;
declare l_row_cnt int DEFAULT 0;
declare rn        int default 0;
declare i         int default 0;
set l_dutyno=101;
set l_delete_date=date_format(DATE_ADD(now(),INTERVAL -30 day),%Y-%m-%d); /*删除90天之前的*/
set l_state_date=date_format(now(),%Y%m%d);

set i=0;

/*
set @strsql01 = CONCAT(‘delete from db_pushmsg.app_message_‘,i,‘ where create_time <= ‘‘2018-06-24 00:00:00‘‘ and message_type not in (1,2,3,4,5,6,7,8,9, 4096, 4097, 4098, 24576, 24577,24578, 28672, 28673, 36865, 4353, 4354) limit 1000‘);
select @strsql01;

*/

while i<=10 DO
insert into tb_stat_duty_log(dutydate,dutyno,status,remark) values(l_state_date,l_dutyno,0,concat(开始清理,i));
label:
    WHILE (1 = 1)
    DO
      set @strsql01 = CONCAT(delete from db_test.tb_test,i, where create_time <= ‘‘2018-06-24 00:00:00‘‘ and message_type not in (1,2,3,4,5,6,7,8,9, 4096, 4097, 4098, 24576, 24577,24578, 28672, 28673, 36865, 4353, 4354) limit 1000);
      /*select @strsql01;*/
      PREPARE stmt01 FROM @strsql01;
      execute stmt01;
      SET @aa = ROW_COUNT();
      select @aa;
       IF @aa = 0 THEN
         LEAVE label;
      END IF;
      deallocate prepare stmt01;
    END WHILE;
    deallocate prepare stmt01;
    set i = i +1;

insert into tb_stat_duty_log(dutydate,dutyno,status,remark) values(l_state_date,l_dutyno,0,concat(结束清理,i));

end while;

END 

 

以上是关于mysql动态执行sql批量删除数据的主要内容,如果未能解决你的问题,请参考以下文章

mysql 怎么批量删除没有数据的数据?怎么重新随即排列主键?

mysql批量删除,已经获得id,条件是另一个表,不存在的就删除,请问sql语句怎么写?

sql 语句 批量删除指定id

java 批量执行sql语句 mysql批量执行sql语句

sql命令删除id用SQL语句批量删除指定ID

我想根据id批量删除几条记录,该怎么写sql语句,我用的数据库是mysql!