简体   繁体   中英

Using Select INTO Statement in Mysql Trigger based on If Condition

I want to write a trigger. The trigger works in the following manner :

When table R_published gets a new entry, based on a column value in the entry(R_published.whichPublishable) it needs to copy a row from either the project_task_goodread_master table or the project_document_master table into the R_publishedGoodReads OR R_publishedDocuments tables respectively.

I have written the following trigger and I'm getting the error : "#1327 - Undeclared variable: R_publishedGoodReads"

CREATE TRIGGER trigger_after_published

    AFTER INSERT ON R_published

    FOR EACH ROW

    BEGIN

    IF (NEW.whichPublishable=1) THEN

        SELECT *  INTO R_publishedGoodReads FROM project_task_goodread_master 
WHERE

 goodReadID= new.publishedItemId;

    ELSEIF (NEW.whichPublishable=2) THEN 

      SELECT * INTO R_publishedDocuments FROM project_document_master where 

 documentID=new.publishedItemId;

      END IF 

END 

Is there anything wrong with the syntax ? Do I need to declare the table name that I am using for insert ? Thanks.

MySQL doesn't support SELECT...INTO TABLE. See MySQL Documentation

Try instead:

IF (NEW.whichPublishable=1) THEN
    INSERT INTO R_publishedGoodReads (col1, col2...)
    SELECT col1, col2... FROM project_task_goodread_master 
    WHERE goodReadID= new.publishedItemId;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM