在基于If Condition的Mysql Trigger中使用Select INTO语句

时间:2012-02-01 04:39:41

标签: mysql triggers conditional-statements

我想写一个触发器。 触发器以下列方式工作:

当表R_published获取一个新条目时,根据条目中的列值(R_published.whichPublishable),它需要将一行从project_task_goodread_master表或project_document_master表复制到R_publishedGoodReads OR R_publishedDocuments表中。

我写了以下触发器,我收到错误:“#1327 - 未声明的变量: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 

语法有什么问题吗?我是否需要声明我用于插入的表名?谢谢。

2 个答案:

答案 0 :(得分:0)

MySQL不支持SELECT ... INTO TABLE。见MySQL Documentation

答案 1 :(得分:0)

尝试改为:

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