Mysql存储过程无法按预期工作

时间:2018-06-11 11:31:36

标签: 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;

1 个答案:

答案 0 :(得分:0)

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