Mysql存储过程。这个问题有什么问题吗?

时间:2013-02-19 19:00:09

标签: mysql stored-procedures printing

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

编辑:   Per @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

1 个答案:

答案 0 :(得分:1)

第一次出错

DECLARE intCount INT default;

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

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

第二次错误

导致第二个错误是因为您无法执行字符串,您需要先准备一个语句:PrepareExecute

PREPARE stmt1 FROM @SQL;
EXECUTE stmt1;