0

I am trying to write the following trigger:

DELIMITER $$

CREATE TRIGGER trigger_test_1 AFTER INSERT ON test FOR EACH ROW
    BEGIN
        DECLARE sID INT;
        DECLARE m INT;
        DECLARE a INT;
        DECLARE c INT;

        SET sID = NEW.s;
        SELECT MONTH(NOW()) INTO m;
        SELECT YEAR(NOW()) INTO a;

        SELECT COUNT(*) INTO c 
            FROM testt t 
            WHERE t.m = m AND t.a = a AND t.sID = sID;

        IF c = 0 THEN 
            INSERT INTO testt VALUES (m, a, sID, '', '');   
        END $$

DELIMITER ;

The error is:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near '' at
line 15

The tables are quite simple:

CREATE TABLE test(
    s INT
);

CREATE TABLE testt(
    m INT,
    a INT,
    sID INT,
    t VARCHAR(200),
    d VARCHAR(500)
);

Thanks.

1 Answer 1

2

You miss END IF; at the end:

IF c = 0 THEN INSERT INTO testt VALUES (m, a, sID, '', ''); 
END IF;
END $$
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was worried about other aspects and missed this little error.
Must wait some time from the start of the question to accept any. I'll do it as soon as I can.

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.