0

I am struggling with deleting indexes of each table

that seems quite easy but there are some catches. drop clause takes only one index to be deleted and mysql gives me an error when i am trying to make a second loop ( which would go through indexes so that i could delete needed ones)

 delimiter &&
 CREATE PROCEDURE dropping_indexes_excpt_for_primaryone()
 BEGIN 
    DECLARE done INT DEFAULT 0;

    DECLARE tbl_name VARCHAR(200) DEFAULT '';
    
    DECLARE all_tables_cursor CURSOR for 
     (SELECT TABLE_NAME
      FROM information_schema.tables
      WHERE table_schema = DATABASE()
      AND table_type = 'BASE TABLE');
    
    
    DECLARE CONTINUE handler FOR NOT FOUND SET done = 1;
    OPEN all_tables_cursor;
    tables_loop: LOOP
        fetch all_tables_cursor INTO tbl_name;
        if done then
            leave tables_loop;
        END if;
--------------------------------------------------------------------------          
                #how can i delete them using the following statement?
                #SELECT index_name  FROM information_schema.STATISTICS WHERE TABLE_NAME= tbl_name AND                                       table_schema= DATABASE()
                 AND index_name <>'PRIMARY' 
                 GROUP BY index_name
                 HAVING COUNT(*)>1 
 ---------------------------------------------
        
    END loop tables_loop;
    close all_tables_cursor;
    END;
&&

i tried to make a second loop but it didn't work because i needed to have some cursor to store it in it is impossible to declare cursor not in the begining because table hasn`t been taken yet and ,to the best of my knowledge, it is not legitimate to use declare clause inside the loop (or in general - not in the begining) i also tried to create a separate procedure to do it but something went wrong there,too but i would like to do it in one stored procedure

  • 1
    You can declare at the beginning of each new block, see e.g. [Nested Cursors in Mysql](https://stackoverflow.com/questions/9699896/nested-cursors-in-mysql) – Solarflare Dec 31 '22 at 12:22

0 Answers0