1
CREATE TRIGGER comissao
ON tb_Pedido
FOR INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE
v_codFuncionario INT DEFAULT NULL;
DECLARE v_dataPedido DATETIME(3) DEFAULT NULL;
DECLARE v_valorComissao DECIMAL(15,4) DEFAULT NULL
IF(SELECT COUNT(*) FROM INSERTED) = 1 AND (SELECT COUNT(*) FROM DELETED) = 1
THEN
SET v_codFuncionario = (SELECT codFuncionario FROM DELETED);
SET v_dataPedido = (SELECT dataPedido FROM DELETED);
DELETE FROM tb_Comissao WHERE dataPedido = v_dataPedido AND codFuncionario = v_codFuncionario;
SET v_codFuncionario = (SELECT codFuncionario FROM INSERTED);
SET v_dataPedido = (SELECT dataPedido FROM INSERTED);
SET v_valorComissao = (SELECT totalPedido FROM INSERTED); * 0.05
INSERT INTO tb_Comissao (codFuncionario, dataPedido, valorComissao) VALUES (v_codFuncionario, v_dataPedido, v_valorComissao);
ELSE
IF(SELECT COUNT(*) FROM INSERTED) = 1
THEN
SET v_codFuncionario = (SELECT codFuncionario FROM INSERTED);
SET v_dataPedido = (SELECT dataPedido FROM INSERTED);
SET v_valorComissao = (SELECT totalPedido FROM INSERTED); * 0.05
INSERT INTO tb_Comissao (codFuncionario, dataPedido, valorComissao) VALUES (v_codFuncionario, v_dataPedido, v_valorComissao);
ELSE
SET v_dataPedido = (SELECT dataPedido FROM DELETED);
DELETE FROM tb_comissao WHERE dataPedido = v_dataPedido AND codFuncionario = v_codFuncionario;
END IF;
END IF;
END
1064 - You have a syntax error in your SQL next to 'ON tb_Pedido
Post the error !!
– rbz
#1064 - You have a syntax error in your SQL next to 'ON tb_Pedido
– Arthur Gabriel Silva Arantes
Arthur, when you need to add something, click "edit" below your question, and include it. So it is complete and better understood to others :]
– rbz
Try to put
CREATE TRIGGER comissao
ON nome_do_seu_banco.tb_Pedido
– rbz
wouldn’t be the event before, so:
CREATE TRIGGER comissao
BEFORE INSERT ON tb_Pedido...
? See documentation https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html and examples https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html– Ricardo Pontual
OK I’ll see, from now mt thanks
– Arthur Gabriel Silva Arantes
@Ricardopunctual I think not. What can be is that you have to define an event only between
FOR INSERT, UPDATE, DELETE
... For exampleBEFORE UPDATE
,AFTER INSERT
, etc.– rbz
@RBZ ran a test on sqlfiddle and it doesn’t work, but following the documentation syntax works (http://sqlfiddle.com/#!9/4c89b3) that above syntax works on
sql-server
but in themySql
it seems that no– Ricardo Pontual
I believe I would have to replace for Insert with BEFORE or AFTER .
– Lucas Brogni
@Ricardopunctual exactly!... I was looking at the
sql-server
, and I got confused on this link: Mysql Basic: Triggers , I didn’t see the word "event" hereCREATE TRIGGER nome momento evento
. Sorry, guys, sorry.– rbz
Exactly, I also went to look at the documentation because Trigger is not something you create all the time, nor knew that the syntax was a little different, we are always learning :)
– Ricardo Pontual
Thank you all, I followed the Mysql documentation and managed to create Trigger!
– Arthur Gabriel Silva Arantes