IF Mariadb phpmyadmin

Asked

Viewed 66 times

0

I’m trying to create a Rigger using phpmyadmin with Mariadb bank but when it gets on the line if it gives a message:

unrecognized statement type. (near IF)

Trigger is the one:

SET @TOTAL = (SELECT COUNT(*) FROM cliente C WHERE C.NR_CPF = NEW.NR_CPF);


IF @TOTAL > 0 THEN BEGIN
   SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'An error occurred'
END IF;

1 answer

1

I believe that the correct one, keeping this logic, would be:

BEGIN 
  DECLARE total BIGINT;

  SELECT COUNT(*)
   INTO  @total 
    FROM  cliente C
     WHERE C.NR_CPF = NEW.NR_CPF;

  IF @total > 0 THEN 
    SIGNAL SQLSTATE '45000' SET message_text = 'An error occurred'; 
  END IF;

END

Another way would be to just add BEGIN and END and a ; to correct, thus:

BEGIN

  SET @total = (SELECT COUNT(*) FROM cliente C WHERE C.NR_CPF = NEW.NR_CPF);

  IF @total > 0 THEN
     SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'An error occurred';
  END IF;

END
  • when it arrives in the word IF gives the message unrecognized statement type. (near IF)

Browser other questions tagged

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