Search code examples
mysqlprocedure

Update procedure Incorrect/ unknown column in entry parameter


I have error with one entry parameter but I dont know why:

0   14:31:47    call UpdateComment(66,1,2,"NOWYTitle","NOWYContent")    Error Code: 1054. Unknown column 'NOWYTitle' in 'field list'

call procedure:

call UpdateComment(66,1,2,"NOWYTitle","NOWYContent");

my procedure:

DELIMITER $$
DROP PROCEDURE IF EXISTS UpdateComment $$
CREATE PROCEDURE `UpdateComment`(UserId INT,/*Crc32 int(11) unsigned,*/ArticleId int(11),CommentsId int(11), newTitle varchar(255), newContent varchar(255))
BEGIN

        DECLARE helpUserTable varchar(255);

        set helpUserTable = CONCAT("User",UserId);

        set @score1 =CONCAT("UPDATE `",helpUserTable,"` SET Title = ",newTitle," WHERE CommentsId = ",CommentsId," AND ArticleId = ",ArticleId);
        PREPARE stmt FROM @score1;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        set @score2 =CONCAT("UPDATE `",helpUserTable,"` SET Content = ",newContent," WHERE CommentsId = ",CommentsId," AND ArticleId = ",ArticleId);
        PREPARE stmt FROM @score2;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;

END

Solution

  • Procedure generates these statements:

    UPDATE `User66` SET Title = NOWYTitle WHERE CommentsId = 2 AND ArticleId = 1
    
    UPDATE `User66` SET Content = NOWYContent WHERE CommentsId = 2 AND ArticleId = 1
    

    They are incorrect, because all literals (string values) must be quoted -

    UPDATE `User66` SET Title = 'NOWYTitle' WHERE CommentsId = 2 AND ArticleId = 1
    
    UPDATE `User66` SET Content = 'NOWYContent' WHERE CommentsId = 2 AND ArticleId = 1
    

    So, change code in this way -

    ...
    SET @score1 = CONCAT("UPDATE `", helpUserTable, "` SET Title = '", newTitle, "' WHERE CommentsId = ", CommentsId, " AND ArticleId = ", ArticleId);
    ...