SQL存储过程抛出错误

时间:2015-08-25 18:14:01

标签: mysql stored-procedures

我试图在mySQL中创建存储过程中运行以下操作,但是收到错误:

  

"您的SQL语法出错了;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近' AS BEGIN DECLARE col_name VARCHAR(255)'在第2行"

这是存储过程:

CREATE PROCEDURE getColumnUniqueValuePercentage 
AS
BEGIN
DECLARE col_name VARCHAR(255);

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

SET @query = '';
SET @n := (SELECT COUNT(*) FROM foo);

OPEN col_names;
SET @i = 0;
the_loop: LOOP

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

    FETCH FROM col_names INTO col_name;     
        SET @query = CONCAT('SELECT ', col_name, ', COUNT(*)*100/', @n, ' as count from full_db3 group by ', col_name,' order by count desc');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
    SET @i = @i + 1;  
END LOOP the_loop;
END

我在这里做错了什么?

0 个答案:

没有答案