Mysql 存储过程未按预期工作

标签 mysql stored-procedures

我需要编写一个 SP,其中 - 1. 选择与给定 where 子句匹配的行数。 2. 循环行计数并执行限制为 n 的删除查询,直到行计数不为 0 时为止。 3. 将行计数值减少n。

SP 写入 -

BEGIN 
DECLARE current_timestamp_millis BIGINT;
    DECLARE RETENTION_DAYS SMALLINT;
    DECLARE numRows BIGINT DEFAULT 0;

    -- No. of days to retain data for
    SET RETENTION_DAYS = 1;

    -- Current epoch timestamp in millis
    SET current_timestamp_millis = UNIX_TIMESTAMP(NOW())*1000; 

    -- SQL query to get the count of rows ,eligible to get deleted. 
    select count(*) as numRows from table1 where state = 2 AND end_time < (UNIX_TIMESTAMP(NOW())*1000 - (1 * 24 * 60 * 60 * 1000)) ;
    -- Loop on the num of rows from above select query and delete the rows in chunks of 100
    WHILE(numRows >=0) 
    DO
    Insert into test_t values(current_time_millis);
    Delete from table1 where end_time < ((UNIX_TIMESTAMP(NOW())*1000 - (1 * 24 * 60 * 60 * 1000))) limit 100;
    SET numRows = numRows - 100;
    DO SLEEP(2);
    END WHILE;
END;

最佳答案

您没有设置 numrows,请尝试 select..into numrows。

关于Mysql 存储过程未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50796717/

相关文章:

php - 字符在数据库中存储为奇怪的字符,但在 HTML 中正确呈现

mysql - 查找包含纬度和经度的表格的 25 个最近地址

sql-server - SQL Server 存储过程的 TRY/CATCH block 的位置

时间:2019-05-08 标签:c#asp.net : capture the raiserror() message from an SQL stored procedure

mysql存储过程,使用列名作为参数

Entity Framework 迁移脚本中的 MySql 存储过程/循环

mysql - 使用存储过程从连接表中获取唯一的产品

mysql - 更新 Magento 数据库以反射(reflect)默认账单地址

MySQL 查询以下内容

PHP将用户数组插入用户数组并循环用户和用户数据(属性)