这是存储的过程。我想做的是遍历所有数据库并在特定表中查找行数。如果计数为 0,则打印该表。
DELIMITER $$
CREATE PROCEDURE checkTableData()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE db VARCHAR(255);
DECLARE appDBs CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'db_%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DECLARE intCount INT default;
OPEN appDBs;
REPEAT
FETCH appDBs INTO db;
IF NOT done THEN
SET @SQL = CONCAT('SELECT @intCount = COUNT(1) FROM ', db, '.SMS_CODES');
EXECUTE @SQL;
IF @intCount = 0 THEN
SELECT db;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE appDBs;
END $$
delimiter ;
错误就在这条线上
IF NOT done THEN
编辑:试过了IF done != 0 THEN
。错误信息:
You have an error in your SQL; Check the manual that corresponds to your MySQL server version for the right syntax to use near ;
OPEN appDBs;
REPEAT
FETCH appDBs INTO db;
`IF don' at line 7
编辑:根据@Gerve,我更新了proc并将intCount声明移到光标上方的顶部,然后将其设置为0。这样错误就消失了。现在另一个错误是:
You have an error in your SQL; Check the manual that corresponds to your MySQL server version for the right syntax to use near '@SQL
@SQL = CONCAT('SELECT @intCount = COUNT(1) FROM ', db, '.ECR_SMS_COUNTRY_CODES');
EXECUTE @SQL;
END IF;
UNTIL done END REPEAT;
CLOSE appDBs;
EN' at line 15