简体   繁体   中英

MySQL Error 1064: Creating a procedure

I am modifying a procedure of mine. Since I switched to another webspace provider and they don't allow SQL-Trigger, I need to do the following in my procedure:

After a comment is created for a recruitment, the recruitments "last activity"-field has to be updated. The created comment id should also be returned.

I tried this:

BEGIN
    DECLARE id INT;

    INSERT INTO
        `comments` (
        `comments`.`recruitment_id`,
        `comments`.`user_id`,
        `comments`.`comment`
    )
    VALUES (
        recruitment_id,
        user_id,
        com
    );

    SELECT
        LAST_INSERTED_ID()
    INTO
        id;

    UPDATE
        `recruitments`
    SET
        `recruitments`.`lastActivity` = `comments`.`creationDate`
    INNER JOIN
        `comments`
    ON
        `recruitments`.`id` = `comments`.`recruitment_id`
    WHERE
        `comments`.`id` = id;

    SELECT id;
END

But I get an 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 'INNER JOIN         `comments`     ON         `recruitments`.`id` = `comments`' at line 25

I bet it's just a small mistake again but I can't seem to find it =(

Try writing the body like this:

BEGIN
    DECLARE v_id INT;

    INSERT INTO comments(recruitment_id, user_id, comment)
      VALUES (v_recruitment_id, v_user_id, v_com);

    SELECT v_id := LAST_INSERTED_ID();

    UPDATE recruitments r INNER JOIN
           comments c
           ON r.id = c.recruitment_id
        SET r.lastActivity = c.creationDate
        WHERE c.id = v.id;

    SELECT v_id;
END;

There were several issues with your query:

  • Identify parameters with a prefix so they are not confused with columns in a query.
  • Identify variables with a prefix as well.
  • Don't qualify the column names in the column list for a select .
  • The proper syntax for update with join in MySQL is to put the join logic right after the update .

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