1

I'm writing the code for an hotel management DB for school assignement. i need to insert a query inside a trigger and my sql server gives me this sintaxerror:

#1064 in the query SQL near 'SELECT codiceFiscale FROM IMPIEGATO WHERE ambiente=new.ambiente SET NEW.nome=U' line 5

CODE:

CREATE TRIGGER IF NOT EXISTS Gestione.ControlloExtra
BEFORE INSERT ON SPESEEXTRA
FOR EACH ROW
    BEGIN
    IF EXISTS SELECT codiceFiscale FROM IMPIEGATO WHERE ambiente=new.ambiente
        SET NEW.nome=UPPER(NEW.nome); 
3
  • Are you using MySQL or MS SQL Server? (That code attempt is product specific, and won't run on both those products.) Commented Feb 5, 2018 at 11:24
  • 1
    Aren't you missing an END to your BEGIN? Commented Feb 5, 2018 at 11:24
  • I'm using MySQL. Yes the END is missing but that is not the problem, the problem is the query. Commented Feb 5, 2018 at 11:27

1 Answer 1

2

Subqueries need parentheses:

CREATE TRIGGER IF NOT EXISTS Gestione.ControlloExtra
BEFORE INSERT ON SPESEEXTRA
FOR EACH ROW
BEGIN
    IF EXISTS (SELECT i.codiceFiscale FROM IMPIEGATO i WHERE i.ambiente = new.ambiente)
        SET NEW.nome = UPPER(NEW.nome);
    END IF;
    . . .
END;
Sign up to request clarification or add additional context in comments.

1 Comment

@AlessandroSoffiati . . . It cannot be exactly the same error because of the parentheses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.