我试图创建会杀死所有睡眠连接的函数:
CREATE DEFINER=`user`@`%` FUNCTION `kill_sleepers`() RETURNS int(11)
BEGIN
DECLARE Id, result INT;
DECLARE done INT DEFAULT FALSE;
DECLARE sleepers CURSOR FOR SELECT Id FROM INFORMATION_SCHEMA.processlist where COMMAND = 'Sleep';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET result = 0;
OPEN sleepers;
read_loop: LOOP
FETCH sleepers INTO Id;
IF done THEN
LEAVE read_loop;
END IF;
kill Id;
SET result = result + 1;
END LOOP;
CLOSE sleepers;
RETURN result;
END
但是当我运行它时,我得到了错误代码:1094。未知的线程ID:0'。如果我把杀戮放入:
IF Id != 0 THEN
kill Id;
SET result = result + 1;
END IF;
它会杀了什么,我犯错了?
答案 0 :(得分:0)
DROP FUNCTION KILL_SLEEPERS;
DELIMITER $$
CREATE FUNCTION KILL_SLEEPERS()
RETURNS INT READS SQL DATA
BEGIN
DECLARE DONE INT DEFAULT FALSE;
DECLARE VAR_ID INT;
DECLARE ITER INT;
DECLARE SLEEPERS CURSOR FOR SELECT ID FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND = 'SLEEP';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = TRUE;
SET ITER = 0;
OPEN SLEEPERS;
READ_LOOP: LOOP
FETCH SLEEPERS INTO VAR_ID;
IF DONE THEN
LEAVE READ_LOOP;
END IF;
KILL VAR_ID;
SET ITER = ITER + 1;
END LOOP;
CLOSE SLEEPERS;
RETURN ITER;
END;
$$
DELIMITER ;
SELECT KILL_SLEEPERS();
试试上面的代码。
我刚刚将变量名称从ID
更改为VAR_ID
。