光标未打开错误

时间:2011-02-10 09:19:22

标签: mysql

我希望处理表“accounts”的所有列名。 我在堆栈溢出时发现了这个存储过程,但它没有按预期工作。

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

我收到错误:

ERROR 1326(24000):光标未打开

1 个答案:

答案 0 :(得分:5)

您似乎忘记在循环之前声明打开游标

open col_names;
the_loop: LOOP
...