DELIMITER $$
USE `mysql`$$
DROP PROCEDURE IF EXISTS `proc_GetUserPriv`$$
CREATE DEFINER=`root`@`%` PROCEDURE `proc_GetUserPriv`(
)
COMMENT'获取账号权限信息列表'
BEGIN
# 获取用户总数,用于循环判断
SET @counts = (SELECT COUNT(*) FROM `user`);
SET @i = 1;
WHILE (@i <= @counts) DO
-- SELECT CONCAT(`user`,'@',"'",`host`,"'") INTO @sql2 FROM USER LIMIT 2,
-- set @SQL1 = concat('SELECT',CONCAT(`USER`,"@'",`HOST`,"'"),'into @SQL2 FROM USER LIMIT ',@i,",1"); -- 这句会导致错误
SET @SQL1 = CONCAT('select concat(','"',"'",'"',',user,','"',"'",'"',',','"@',"'",'",host,"',"'",'")',' into @SQL2 from user limit ',@i,",1");
PREPARE smtm1 FROM @SQL1 ;
EXECUTE smtm1;
DEALLOCATE PREPARE smtm1;
SET @sql3 = CONCAT("show grants for ",@SQL2);
PREPARE smtm2 FROM @sql3;
EXECUTE smtm2;
DEALLOCATE PREPARE smtm2;
SET @i = @i + 1;
END WHILE;
END$$
DELIMITER ;