程序MySQL中插入错误

时间:2012-08-02 08:30:08

标签: mysql insert add procedure

程序中的错误INSERT。此插件添加所有列而不包含 UserId

----------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
DROP PROCEDURE IF EXISTS CreateTable $$
CREATE PROCEDURE `CreateTable`
(
    md5 VARCHAR(255),table_name VARCHAR(255),CommentsId INT(11),
    Parent VARCHAR(100), Content TEXT,UserId INT(11), Title VARCHAR(255), 
    Nick VARCHAR(20), IdLength INT(11), suffix INT(11)
)
BEGIN



DECLARE full_table_name varchar(255);

SET @SQL=CONCAT('SELECT COUNT(\'X\') INTO @COUNTER FROM INFORMATION_SCHEMA.COLUMNS ',
            'WHERE TABLE_NAME like \'', table_name, '%\'');

PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

    SET full_table_name= table_name;


    SET @SQL=CONCAT('CREATE TABLE IF NOT EXISTS ', full_table_name, ' LIKE CommentsPattern');
    PREPARE stmt FROM @SQL;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;


SET @SQL=CONCAT('INSERT INTO ', full_table_name,'(md5,UserId, Parent, Content, CommentsId, Title, Nick,IdLength,suffix)',
        'values(',UserId,',"',md5, '",', Parent, ',"', Content, '",',CommentsId,',"',Title,'","',Nick,'",', IdLength, ',',suffix, ')');
    prepare stmt from @sql;
    execute stmt;
    deallocate prepare stmt;

END

我想添加这些值:

call CreateTable('dcf57shasdsd5723m','dcf',2,60201,'My article is',444,'New Title','Ethan',1,2);

其中:

  1. md5 - dfc7shgsnsty3475723m
  2. table_name - dfc
  3. UserId - 444
  4. 标题 - 新标题
  5. 尼克 - Ethan
  6. 评论 - 2
  7. 家长 - 60201
  8. IdLength - 1
  9. 后缀 - 2
  10. 内容 - 我的文章

1 个答案:

答案 0 :(得分:1)

在INSERT INTO中,您按顺序声明列:md5,UserId,Parent,... 但是,在值中,md5和UserId值被切换:',UserId,','',md5,'“,',Parent