shantanuo
shantanuo

Reputation: 32306

cursor not open error

I want all the column names of the table "accounts" to be processed. I found this stored procedure on stack overflow, but it is not working as expected.

delimiter $

create procedure prc_column ()
BEGIN
DECLARE num_rows int;
declare i int;
declare col_name varchar(1000);

DECLARE col_names CURSOR FOR
  SELECT column_name
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'accounts'
  ORDER BY ordinal_position;


select FOUND_ROWS() into num_rows;

SET i = 1;
the_loop: LOOP

   IF i > num_rows THEN
        CLOSE col_names;
        LEAVE the_loop;
    END IF;


    FETCH col_names 
    INTO col_name;     

// do something with column names for e.g. append it with _drupal

    SET i = i + 1;  
END LOOP the_loop;

END

I am getting an error:

ERROR 1326 (24000): Cursor is not open

Upvotes: 3

Views: 4671

Answers (1)

ajreal
ajreal

Reputation: 47321

You seems forget to declare open cursor before the loop

open col_names;
the_loop: LOOP
...

Upvotes: 6

Related Questions