0

我有一个输入参数错误,但我不知道为什么:

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

调用程序:

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

我的程序:

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
4

2 回答 2

1

首先,您需要在文本值周围的命令中添加引号,其次,您的 SQL 语句可以组合成一个语句(它们共享相同的 where 子句):

试试这个:

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

@score2并完全删除

于 2012-08-20T12:47:08.520 回答
0

过程生成这些语句:

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

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

它们是不正确的,因为必须引用所有文字(字符串值) -

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

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

所以,以这种方式更改代码 -

...
SET @score1 = CONCAT("UPDATE `", helpUserTable, "` SET Title = '", newTitle, "' WHERE CommentsId = ", CommentsId, " AND ArticleId = ", ArticleId);
...
于 2012-08-20T12:42:33.920 回答