I’m creating a trigger in MySQL that should set a variable and use it to update a table. But i can’t find the right syntax to make this work.
This is what i tried:
CREATE TRIGGER after_assinatura_update AFTER UPDATE ON documento_eletronico_assinatura FOR EACH ROW BEGIN SET @prox_ass = ( SELECT dea.usuario_id FROM documento_eletronico_assinatura dea INNER JOIN usuario u ON dea.usuario_id = u.id WHERE dea.documento_id = NEW.documento_id AND dea.assinatura = 0 ORDER BY dea.posicao ASC LIMIT 1 );/*ERROR: statement incomplete, expecting: ';'*/ UPDATE documento_eletronico SET documento_eletronico.prox_assinatura = @prox_ass WHERE documento_eletronico.id = NEW.documento_id;
CREATE TRIGGER after_assinatura_update AFTER UPDATE ON documento_eletronico_assinatura FOR EACH ROW UPDATE documento_eletronico SET documento_eletronico.prox_assinatura = @prox_ass WHERE documento_eletronico.id = NEW.documento_id; /*TRIGGER stops here and doesn't set the variable. Adding a BEGIN gives the "expected: ';'" at The line above.*/ SET @prox_ass = ( SELECT dea.usuario_id FROM documento_eletronico_assinatura dea INNER JOIN usuario u ON dea.usuario_id = u.id WHERE dea.documento_id = NEW.documento_id AND dea.assinatura = 0 ORDER BY dea.posicao ASC LIMIT 1 );
Advertisement
Answer
You probably just need a DELIMITER
statement. However, you can express this as one statement:
SET @prox_ass = ( /*ERROR: statement incomplete, expecting: ';'*/ UPDATE documento_eletronico de CROSS JOIN (SELECT dea.usuario_id FROM documento_eletronico_assinatura dea JOIN usuario u ON dea.usuario_id = u.id WHERE dea.documento_id = NEW.documento_id AND dea.assinatura = 0 ORDER BY dea.posicao ASC LIMIT 1 ) u ); SET de.prox_assinatura = u.usuario_id WHERE de.id = NEW.documento_id;