There are at least following issues with your code:
- All declarations should come before any statements (
SET
in your case). That's what is causing your immediate error. - You didn't declare
DONE
variable - You didn't declare
LOTE_DETALLE_IDS
variable, but you declaredID_LOTE_DETALLE_MIN
which you didn't use. Perhaps you should renameID_LOTE_DETALLE_MIN
toLOTE_DETALLE_IDS
then. - Since declarations go first you have to re-write your cursor definition (e.g. with subquery) because
ID_INVENTARIO_IN
variable isn't initialized yet
Your whole declaration section can be re-written in a following manner
-- declaration of variables of the same type can be combined
DECLARE ID_INVENTARIO_IN,
LOTE_DETALLE_IDS,
CANTIDAD_A_COMPARARINT,
CANTIDAD_A_RESTAR,
DONE INT;
DECLARE cur_id CURSOR FOR ...
DECLARE CONTINUE HANDLER ...
-- initialize variables
SET ...
Now, I believe your trigger most likely can be re-written with just update statement(s). You can get help with it if you explain in plain words what you're trying to achieve and provide exact table schemas and sample data.