简体   繁体   中英

HANDLER in MySQL how is it implemented?

thank you who can help me.

I am doing a procedure in Mysql. everything goes ok, until I try to implement a HANDLER inside a while.

So far everything ok, save my procedure without errors:

BEGIN
        SET N_CONT=0;
        SET N_TOTL=5;
            WHILE N_CONT <= N_TOTL DO
                
            
                SET N_CONT = N_CONT +1;
            END WHILE;
    
    END

when I try to place a handler inside, I get an error and I don't see why:

BEGIN
        SET N_CONT=0;
        SET N_TOTL=5;
            WHILE N_CONT <= N_TOTL DO
                DECLARE EXIT HANDLER FOR SQLEXCEPTION
                    BEGIN
                    -- cuerpo handler
                    END;
            
                SET N_CONT = N_CONT +1;
            END WHILE;
    
    END

What am I doing wrong? someone knows?

I think you need to wrap the body of the WHILE in BEGIN...END to be able to use DECLARE statements in it.

BEGIN
    SET N_CONT=0;
    SET N_TOTL=5;
    WHILE N_CONT <= N_TOTL DO
        BEGIN
            DECLARE EXIT HANDLER FOR SQLEXCEPTION
                BEGIN
                -- cuerpo handler
                END;
        
            SET N_CONT = N_CONT +1;
        END
    END WHILE;
END

Thanks Barmar. your solutions worked for me.

BEGIN
    SET N_CONT=0;
    SET N_TOTL=5;
    WHILE N_CONT <= N_TOTL DO
        BEGIN
            DECLARE EXIT HANDLER FOR SQLEXCEPTION
                BEGIN
                -- cuerpo handler
                END;
    SET N_CONT = N_CONT +1;
        END;
    END WHILE;
END

it just shows me an error, but adding ";" at the end of the while, it fixed me.

thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM