本文介绍了DECLARE CURSOR FOR的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我在下面的sp代码中出现语法错误.谁能帮我解决这个问题?

I dont't understand why im getting syntax error on my sp code below. Can anyone help me figure this out?

SQL错误(1064):

SQL Error (1064):

DELIMITER $$
DROP PROCEDURE IF EXISTS get_prereqs3$$
CREATE PROCEDURE get_prereqs3(IN prosp_courses_id SMALLINT(5))
BEGIN
    DECLARE done  int DEFAULT FALSE;
    DECLARE required SMALLINT(5) default 0; 
    DECLARE to_search SMALLINT(5) default 0; 
    DROP TABLE IF EXISTS tmp_list;
    CREATE TABLE tmp_list(courses_id SMALLINT(5), courses_id_req SMALLINT(5)) ENGINE = MEMORY;
    DECLARE CUR1 CURSOR FOR SELECT pc.prospectus_courses_id 
            FROM prereq_courses     pc          
            JOIN prerequisites      pr on (pr.id = pc.prerequisites_id)
            JOIN prospectus_courses ps on (ps.id = pr.prospectus_courses_id)
            WHERE ps.id = to_search 
    MAIN_LOOP: LOOP 
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
        OPEN cur1;
        FETCH cur1 INTO required;

        IF done THEN
            CLOSE cur1;
            LEAVE main_loop;
        ELSE
            insert into tmp_list values (to_search, required);
            set to_search = required;
            iterate main_loop;
        END IF;
    END LOOP;
    select  c.course_code 
        from tmp_list           t
        join prospectus_courses pc on pc.id = t.courses_id_req
        join courses            c  on c.id  = pc.courses_id ;
    drop table tmp_list;
END$$
DELIMITER ;

推荐答案

声明必须在BEGIN块之后.就您而言,只需将DECLARE cur1 CURSORDECLARE CONTINUE HANDLER..向上移动两行即可.

Declarations have to be right after a BEGIN block.In your case just move the DECLARE cur1 CURSOR and DECLARE CONTINUE HANDLER.. two lines up.

有时候,您希望稍后在代码中声明一个变量或游标,例如,如果满足条件的话.

Sometimes you want to declare a variable or cursor later in the code, for example only, if a condition is met.

在这种情况下,您可以再次用嵌套的BEGIN .. END包装该块.

In this case you can wrap the block with a nested BEGIN .. END again.

http://dev.mysql.com/doc/refman/5.5/en/begin-end.html http://dev.mysql.com/doc/refman/5.5/en /declare.html

您也声明了CUR1,但使用的是cur1.

Also you are declaring CUR1 but using cur1.

这篇关于DECLARE CURSOR FOR的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 02:52