MySQL-使用游标在存储过程中删除

MySQL-使用游标在存储过程中删除,mysql,stored-procedures,cursor,sql-delete,Mysql,Stored Procedures,Cursor,Sql Delete,我正在创建一个包含delete语句和游标用法的存储过程。问题是我不能同时使用这两者 drop table if exists T1; create table T1(f1 int, f2 int); insert into T1 values(1, 1); insert into T1 values(2, 2); insert into T1 values(3, 3); drop table if exists T2; create table T2(f3 int, f4 int); inse

我正在创建一个包含delete语句和游标用法的存储过程。问题是我不能同时使用这两者

drop table if exists T1;
create table T1(f1 int, f2 int);
insert into T1 values(1, 1);
insert into T1 values(2, 2);
insert into T1 values(3, 3);

drop table if exists T2;
create table T2(f3 int, f4 int);
insert into T2 values(1, 11);
insert into T2 values(2, 22);
insert into T2 values(3, 33);


drop procedure if exists proc1;
DELIMITER //CREATE PROCEDURE proc1(inputValue int)
    BEGIN
    declare i int;
    declare cursorSize int;
    declare anF1 int;
    declare anF2 int;

    -- Block 1 - Begin:    
    delete from T2 where f3 = inputValue;
    -- Block 1 - End.    
    
    -- Block 2 - Begin:    
    declare cur1 cursor for 
        select f1, f2  
            from T1
            where f1 <= inputValue;
    open cur1;

    select FOUND_ROWS() into cursorSize;
    if (cursorSize > 0) then
        set i = 0;
        while i < cursorSize DO
            fetch cur1 into anF1, anF2;
                insert into T2 values(anF1, anF2);
            set i = i + 1;
            end while;
        end if;

    close cur1;
    -- Block 2 - End.    
    
    END //
DELIMITER ;
call proc1(3);
这是一个独立的MWE。 请注意,如果我对另一个块进行注释,则块1和块2可以单独运行,但不能同时运行

drop table if exists T1;
create table T1(f1 int, f2 int);
insert into T1 values(1, 1);
insert into T1 values(2, 2);
insert into T1 values(3, 3);

drop table if exists T2;
create table T2(f3 int, f4 int);
insert into T2 values(1, 11);
insert into T2 values(2, 22);
insert into T2 values(3, 33);


drop procedure if exists proc1;
DELIMITER //CREATE PROCEDURE proc1(inputValue int)
    BEGIN
    declare i int;
    declare cursorSize int;
    declare anF1 int;
    declare anF2 int;

    -- Block 1 - Begin:    
    delete from T2 where f3 = inputValue;
    -- Block 1 - End.    
    
    -- Block 2 - Begin:    
    declare cur1 cursor for 
        select f1, f2  
            from T1
            where f1 <= inputValue;
    open cur1;

    select FOUND_ROWS() into cursorSize;
    if (cursorSize > 0) then
        set i = 0;
        while i < cursorSize DO
            fetch cur1 into anF1, anF2;
                insert into T2 values(anF1, anF2);
            set i = i + 1;
            end while;
        end if;

    close cur1;
    -- Block 2 - End.    
    
    END //
DELIMITER ;
call proc1(3);
如果取消对块1中的delete语句的注释,则会收到此错误:

1064-您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解要使用的正确语法
请帮助我解决这个问题。

文档似乎没有明确说明,但我相当确定,游标声明之前不会有任何查询


您应该能够在声明游标之后但在打开游标之前执行删除操作。

文档似乎没有明确说明它,但我相当确定,游标声明之前不会有任何查询


你应该能够在游标声明之后但在它打开之前执行删除。

@PaulCampbell啊,是的,现在我再看一遍,我太专注于游标特定的部分了,而忽略了更一般的声明。。。必须是。。。在任何其他声明部分之前。@PaulCampbell啊,是的,现在我再看一遍,我太专注于光标特定的部分了,我忽略了更一般的声明。。。必须是。。。在任何其他声明之前。