我正在尝试使用存储过程中的EXECUTE语句填充表。有没有办法执行此类或类似的东西。
DELIMITER $$
DROP PROCEDURE IF EXISTS `mysql`.`ShowUserGrants` $$
CREATE PROCEDURE mysql.ShowUserGrants()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE strGrant CHAR(100);
DECLARE cur1 CURSOR
FOR SELECT CONCAT("SHOW GRANTS FOR '",user,"'@'",host,"';")
FROM mysql.user
WHERE host!='localhost';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
OPEN cur1;
read_loop:LOOP
FETCH cur1 INTO strGrant;
IF done THEN
CLOSE Cur1;
LEAVE read_loop;
END IF;
SET @a = strGrant;
PREPARE strStmt FROM @a;
INSERT INTO user_grants (grants) VALUES(EXECUTE strStmt);
/* EXECUTE strStmt; */
DEALLOCATE PREPARE strStmt;
END LOOP;
END $$
DELIMITER ;
EXECUTE语句按我的意思工作,但需要填充表user_grants。 感谢。
答案 0 :(得分:1)
DELIMITER $$
DROP PROCEDURE IF EXISTS `mysql`.`ShowUserGrants` $$
CREATE PROCEDURE mysql.ShowUserGrants()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE strGrant CHAR(100);
DECLARE cur1 CURSOR
FOR SELECT CONCAT("SHOW GRANTS FOR '",user,"'@'",host,"';")
FROM mysql.user
WHERE host!='localhost';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
OPEN cur1;
read_loop:LOOP
FETCH cur1 INTO strGrant;
IF done THEN
CLOSE Cur1;
LEAVE read_loop;
END IF;
SET @a = 'INSERT INTO user_grants(grants) VALUES(?)';
PREPARE strStmt FROM @a;
SET @var = strGrant;
EXECUTE strStmt USING @var;
DEALLOCATE PREPARE strStmt;
END LOOP;
END $$
DELIMITER ;
或更好,用
替换sp bodyINSERT INTO user_grants(grants)
SELECT CONCAT("SHOW GRANTS FOR '",user,"'@'",host,"';")
FROM mysql.user
WHERE host!='localhost';