MySQL:为`show tables`声明一个游标?

时间:2012-02-14 22:45:16

标签: mysql stored-procedures cursor

我想编写一个存储过程,它遍历数据库中的所有表并执行一些操作。

drop procedure if exists p2;

DELIMITER //

CREATE PROCEDURE `p2` ()
LANGUAGE SQL
DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'A procedure'
BEGIN
declare cur1 cursor for
    show tables from my_database_name
END//


call p2;

这不起作用;我收到一条错误,说我在'show tables from my_database_name'

附近的SQL语法中有错误

当然,我不想做show tables from my_database_name而是说show tables from database()show tables from (select database()),而不是show tables

帮助?我可以在直接SQL中执行此操作,还是需要先从程序中调用database()然后遍历结果集?如何创建光标以捕获show tables或{{1}}等内容的值?

2 个答案:

答案 0 :(得分:3)

show tables不是SQL查询 - 它以不同的方式处理 - 但您可以查询the tables in the information_schema database。您需要SELECT table_name FROM information_schema.tables WHERE table_schema='db_name'

之类的内容

答案 1 :(得分:3)

这是一个示例,可以为您提供所有表格的计数。

DELIMITER $$                                                                                                                                                                    

DROP PROCEDURE IF EXISTS procCountAllTables $$                                                                                                                                  

CREATE PROCEDURE procCountAllTables()                                                                                                                                           

BEGIN                                                                                                                                                                           
        DECLARE table_name VARCHAR(255);                                                                                                                                        
        DECLARE end_of_tables INT DEFAULT 0;                                                                                                                                    

        DECLARE cur CURSOR FOR                                                                                                                                                  
            SELECT t.table_name                                                                                                                                                 
            FROM information_schema.tables t                                                                                                                                    
            WHERE t.table_schema = DATABASE() AND t.table_type='BASE TABLE';                                                                                                    
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_of_tables = 1;                                                                                                           

        OPEN cur;                                                                                                                                                               

        tables_loop: LOOP                                                                                                                                                       
            FETCH cur INTO table_name;                                                                                                                                          

            IF end_of_tables = 1 THEN                                                                                                                                           
                LEAVE tables_loop;                                                                                                                                              
            END IF;                                                                                                                                                             

            SET @s = CONCAT('SELECT ''', table_name, ''', COUNT(*) AS Count FROM ' , table_name);                                                                               
            PREPARE stmt FROM @s;                                                                                                                                               
            EXECUTE stmt;                                                                                                                                                       

        END LOOP;                                                                                                                                                               

        CLOSE cur;                                                                                                                                                              
    END $$                                                                                                                                                                      

DELIMITER ;