4

I want to get the last inserted ID of every channel from log table then write it to another table. For this purpose I wrote a trigger on log table, but it doesn't work because of a syntax error.

A syntax the used for case statement, exactly like a Sqlite reference.

CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END  
CASE WHEN x=w1 THEN r1 WHEN x=w2 THEN r2 ELSE r3 END 

My code:

CREATE TRIGGER ChnState_log AFTER INSERT 
ON CallLog
BEGIN
    CASE NEW.Dir
        WHEN 0
        BEGIN
            CASE
                WHEN 0=(SELECT Id FROM ChnStatus WHERE No = NEW.SrcNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt0", NEW.ID);
                END;
                WHEN 1=(SELECT Id FROM ChnStatus WHERE No = NEW.SrcNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt1", NEW.ID);
                END;
                WHEN 2=(SELECT Id FROM ChnStatus WHERE No = NEW.SrcNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt2", NEW.ID);
                END;
                WHEN 3=(SELECT Id FROM ChnStatus WHERE No = NEW.SrcNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt3", NEW.ID);
                END;
            END;
        END;

        WHEN 1
        BEGIN
            CASE
                WHEN 0=(SELECT Id FROM ChnStatus WHERE No = NEW.DestNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt0", NEW.ID);
                END;
                WHEN 1=(SELECT Id FROM ChnStatus WHERE No = NEW.DestNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt1", NEW.ID);
                END;
                WHEN 2=(SELECT Id FROM ChnStatus WHERE No = NEW.DestNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt2", NEW.ID);
                END;
                WHEN 3=(SELECT Id FROM ChnStatus WHERE No = NEW.DestNo)
                BEGIN
                    INSERT INTO Setting(Name, Value) VALUES ("LstChnSt3", NEW.ID);
                END;
            END;
        END;
    END;
END;
4
  • 1
    I am not sure if this is the answer as it seems too obvious... the word WHERE is misspelt in every sub-query. Commented Jan 22, 2014 at 14:37
  • 1
    you are right. i fix it Commented Jan 23, 2014 at 7:46
  • I will add this as the answer then - it would be appreciated if you mark it as the correct one. Pleased you are sorted. Commented Jan 23, 2014 at 7:51
  • 1
    @Brendan As CL. said, the problem of query was for CASE expression. Commented Dec 2, 2017 at 6:24

2 Answers 2

7

A CASE expression can be used only to select between other expressions, not for statements like INSERT.

The WHEN clause of the CREATE TRIGGER statement often helps. For anything else, you have to put the logic inside the actual statements, like this:

CREATE TRIGGER ChnState_log_src
AFTER INSERT ON CallLog
FOR EACH ROW
WHEN NEW.Dir = 0
BEGIN
    INSERT INTO Setting(Name, Value)
    VALUES('LstChnSt' || (SELECT Id
                          FROM ChnStatus
                          WHERE No = NEW.SrcNo),
           NEW.ID);
END;

CREATE TRIGGER ChnState_log_dest
AFTER INSERT ON CallLog
FOR EACH ROW
WHEN NEW.Dir = 1
BEGIN
    INSERT INTO Setting(Name, Value)
    VALUES('LstChnSt' || (SELECT Id
                          FROM ChnStatus
                          WHERE No = NEW.DestNo),
           NEW.ID);
END;
Sign up to request clarification or add additional context in comments.

2 Comments

Is FOR EACH ROW always required in a PostgreSQL TRIGGER?
@skeetastax This question is about SQLite. The PostgreSQL trigger syntax is entirely different.
0

I am not sure if this is the answer as it seems too obvious... the word WHERE is misspelt in every sub-query

1 Comment

I think your former comment solved the issue in wording the question. As obvious, the core of the problem isn't affected by this. OP corrected his misspelling, so there is no need for this answer.

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.