Uso de la instrucción Select INTO en Mysql Trigger según la condición If

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.

preguntado el 01 de febrero de 12 a las 04:02

2 Respuestas

MySQL doesn't support SELECT...INTO TABLE. See Documentación de MySQL

Respondido 01 Feb 12, 08:02

Intente en su lugar:

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

Respondido 01 Feb 12, 08:02

CREATE TRIGGER trigger_after_published AFTER INSERT ON R_published FOR EACH ROW BEGIN IF (NEW.whichPublishable=1) THEN INSERT INTO R_publishedGoodReads SELECT * FROM project_task_goodread_master WHERE goodReadID= new.publishedItemId; ELSEIF (NEW.whichPublishable=2) THEN INSERT INTO R_publishedDocuments SELECT * FROM project_document_master where documentID=new.publishedItemId; END IF END; - Rujuta

It still gives me the folliwing error : #1064 - 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 11 - Rujuta

Not sure what line 11 is exactly, but try explicitly listing the column names you are inserting. INSERT INTO R_publishedGoodReads (col1, col2) SELECT col1, col2 más bien que INSERT INTO R_publishedGoodReads SELECT * - Diamante negro

delimiter # CREATE TRIGGER trigger_after_published AFTER INSERT ON R_published FOR EACH ROW BEGIN CASE WHEN (NEW.whichPublishable=1) THEN INSERT INTO R_publishedGoodReads SELECT * FROM project_task_goodread_master WHERE goodReadID= new.publishedItemId; WHEN (NEW.whichPublishable=2) THEN INSERT INTO R_publishedDocuments SELECT * FROM project_document_master where documentID=new.publishedItemId; END# delimiter ; This worked ! But somehow it doesn't list my trigger in 'show trigger' query results, neither does it act right when I add a row to the R_published. - Rujuta

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.