0

I need to create triggers for an exercice, i have 3 tables, which one have one int argument and i want to create this trigger:

   CREATE TRIGGER D1_1 BEFORE INSERT ON T1 FOR EACH ROW 
   WHEN((SELECT COUNT(*) FROM T2) < 1)
   INSERT INTO T2 VALUES(6) 
   END;

Syntaxe error near THEN..

Thank you!

1

1 Answer 1

1

You don't even need conditional logic:

    INSERT INTO T2
        SELECT 6
        WHERE NOT EXISTS (SELECT 1 FROM t2);

Note that NOT EXISTS is generally more efficient than COUNT(*) = 0. NOT EXISTS stops at the first matching row, rather going through the entire table to count everything -- and then doing something if there are no rows.

Sign up to request clarification or add additional context in comments.

2 Comments

No, i really need this trigger, cuz i have 5 others triggers and they are linked, i need this trigger ^^
@valt . . . You can incorporate this logic into a trigger. I left out the trigger definition, because it would seem to distract from the logic of the query.

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.