How to do an operation on a Trigger? MYSQL

Asked

Viewed 402 times

0

CREATE TRIGGER aumentarsalario 
AFTER INSERT 
ON empregados
FOR EACH ROW
BEGIN
  UPDATE EMPREGADOS SET SALARIO = NEW.SALARIO * (NEW.SALARIO*(10/100)) WHERE NEW.SALARIO < 900;
END IF
END

How do I put an extra 10% in a product? at which point am I missing.

  • I remembered, so I erased kkkk

  • @Euaelucas if what you need is to change all the records that have the salary less than 900 once, to trigger is not a viable output. The best way would be to create a function and run it only when needed. The trigger will be executed every time a record is entered/updated/deleted, ie every time you register an employee, all employees who have less than 900 salary will receive a 10% increase, including what is being entered now.

3 answers

0

I believe that will solve your problem:

create trigger AUMENTARSALARIO
after insert on EMPREGADOS
for each row
begin
  update EMPREGADOS set SALARIO = SALARIO * 1.1 where SALARIO < 900;
end;

You had left one END IF there that was not necessary; moreover, made the salary receive ele mesmo * 10%, when it should add.

0


You don’t need to make one UPDATE to change the record itself.
That one trigger will be executed every time a new record is inserted, the variable NEW will contain the data of this new record, so to do what you need just do SET NEW.coluna = valor.

CREATE TRIGGER aumentarsalario 
AFTER INSERT 
ON empregados
FOR EACH ROW
BEGIN
  SET NEW.SALARIO = NEW.SALARIO * 1.1 WHERE NEW.SALARIO < 900;
END IF;
END;
  • Thank you very much.

0

Try this!

CREATE TRIGGER aumentarsalario 
  AFTER INSERT 
  ON empregados FOR EACH ROW
BEGIN
  UPDATE EMPREGADOS SET SALARIO = 1.1 * SALARIO WHERE SALARIO < 900;
END;
  • *0.1 does not add 10%, it changes to 10% of the value. I changed some things , try again

  • Points error in syntax. I cannot create this Rigger at all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.