0

Can you tell me what's wrong with my codes? I followed MySQL loop through tables to loop the tables of one database. But I got nothing from iterating the cursor. The SELECT @sql; outputs NULL value. Thanks!

DROP PROCEDURE IF EXISTS seedinvest.insertDatabaseRowHash;
DELIMITER $$
CREATE PROCEDURE seedinvest.insertDatabaseRowHash(_schemaName varchar(100))
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE table_name varchar(20);
    DECLARE table_cursor cursor for SELECT table_name FROM information_schema.tables
                                    WHERE table_schema = _schemaName;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN table_cursor;

    table_loop: LOOP
        FETCH table_cursor INTO table_name;
        IF done THEN
            LEAVE table_loop;
        END IF;

        SET @sql = CONCAT('DESC ', _schemaName, '.', table_name);
        SELECT @sql; -- It outputs Null value
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DROP PREPARE stmt;
    END LOOP;

    CLOSE table_cursor;

END$$


Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NULL' at line 1
Community
  • 1
  • 1
Bowen Liu
  • 99
  • 2
  • 7

0 Answers0