0

这是存储的过程。我想做的是遍历所有数据库并在特定表中查找行数。如果计数为 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
4

1 回答 1

1

第一个错误

DECLARE intCount INT default;

没有默认值,只需将其更改为:

DECLARE intCount INT default 0;
  • MySQL 说错误是near,这意味着就在之前。

第二个错误

第二个错误是因为不能执行字符串导致的,需要先准备一个语句:Prepare , Execute

PREPARE stmt1 FROM @SQL;
EXECUTE stmt1; 
于 2013-02-19T19:14:04.710 回答