循环遍历来自information_schema mysql的结果

时间:2014-08-14 19:19:42

标签: mysql stored-procedures cursor

我试图找到所有具有名称RecordID的列的表,然后遍历这些表以查看RecordID> 5000或不。

CREATE PROCEDURE check_IDs ()
BEGIN
  DECLARE done INT DEFAULT 0;
  DECLARE result varchar(50);
    DECLARE cur1 CURSOR for SELECT table_name FROM  INFORMATION_SCHEMA.COLUMNS 
      WHERE column_name = 'RecordID' ;   

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    OPEN cur1;
    read_loop: LOOP
     FETCH cur1 INTO result;
     IF done THEN
        LEAVE read_loop;
     END IF;

      Select * from `result` where RecordID > 5000;
    END LOOP;
    close cur1;    
END;

它表示未找到表结果。

1 个答案:

答案 0 :(得分:0)

它必须

PREPARE stmt FROM CONCAT('SELECT * FROM `', result, '`WHERE RecordID > 5000;)';
EXECUTE stmt;
DEALLOCATE stmt;

而不是

Select * from `result` where RecordID > 5000;

查看SQL Syntax for Prepared Statements

的示例(最后一篇)

您的语句将使用硬编码表名result。看来,你的数据库中没有这样的表。