程序不止一行

时间:2012-08-14 07:48:10

标签: mysql limit procedure

错误:结果由多行组成 我认为问题在于LIMIT这个词 可以帮忙吗?

DELIMITER $$

CREATE PROCEDURE `InsertComment`(crc int(11) unsigned, userId int, title varchar(255),
                nick varchar(20), parentId int, content text)
BEGIN

         DECLARE tableName VARCHAR(4);
         DECLARE tbName VARCHAR(15);    
         DECLARE results INTEGER;
         DECLARE depth integer;
         DECLARE results2 INTEGER;
         DECLARE resultsM INTEGER;
         DECLARE commentsId INTEGER;
         DECLARE CommentX INTEGER ;
         DECLARE COUNTER INTEGER;


         set tbName = CAST(crc AS CHAR);
         set tableName = SUBSTRING(tbName, 1, 5);




        CALL CreateCommentsTable(tableName);

        if parentId = 0 then

                SET @results2 = 0;

                SET @q2 = CONCAT("SELECT commentsId INTO @results2 FROM `",tableName,"` WHERE CRC32 = ",crc," ORDER BY commentsId DESC LIMIT 1");
                    PREPARE stmq2 FROM @q2;
                    EXECUTE stmq2;
                    DEALLOCATE PREPARE stmq2;

                SET commentsId = @results2 + 1;

                SET depth = 0;
                SET @results = 0;

            else

                    SET @q2 = CONCAT("SELECT Depth INTO @results FROM `",tableName,"` WHERE CRC32 = ",crc," AND commentsId = ",parentId);
                                PREPARE stmq2 FROM @q2;
                                EXECUTE stmq2;
                                DEALLOCATE PREPARE stmq2;

                    set depth = @results;
                    set depth = depth + 1;

                    set @CommentX = 0;
                    SET @COUNTER = 0;


                WHILE @COUNTER = 0 AND @results > -1 DO


                            SET @q4= CONCAT("SELECT COUNT(*) INTO @COUNTER FROM `",tableName,"` WHERE CRC32 = ",crc," AND Depth = ",@results," AND commentsId > ",parentId);
                            PREPARE stmq2 FROM @q4;
                            EXECUTE stmq2;
                            DEALLOCATE PREPARE stmq2;

                            IF @COUNTER > 0 THEN

                                SET @q3= CONCAT("SELECT commentsId INTO @CommentX FROM `",tableName,"` WHERE CRC32 = ",crc," AND Depth = ",@results," AND commentsId > ",parentId," ORDER BY commentsId ASC Limit 1" );
                                PREPARE stmq2 FROM @q3;
                                EXECUTE stmq2;
                                DEALLOCATE PREPARE stmq2;


                            END IF;

                            SET @results = @results - 1;



                END WHILE;

                            SET @resultsM = 0;

                            IF @CommentX = 0 THEN
                                  SET @q2 = CONCAT("SELECT commentsId INTO @resultsM FROM `",tableName,"` WHERE CRC32 = ",crc," ORDER BY commentsId DESC LIMIT 1");

                                  PREPARE stmq2 FROM @q2;
                                  EXECUTE stmq2;
                                  DEALLOCATE PREPARE stmq2;

                                SET commentsId = @resultsM +1;


                                else

                                      SET commentsId = @CommentX + 1;

                            END IF;


                SET @u = CONCAT("UPDATE `",tableName,"` SET commentsId = commentsId + 1 WHERE CRC32 = ",crc,"  AND commentsId > ",commentsId + 1);
                PREPARE stmq FROM @u;
                EXECUTE stmq;
                DEALLOCATE PREPARE stmq;

        end if;

        SET @a = CONCAT("INSERT INTO `", tableName ,"` (`Crc32`, `UserId`, `Title`, `Nick`, `CommentsId`, `Depth`, `Content`, `CommentStatus`, `ViewStatus`) 
                         VALUES (",crc,", ",userId,", '",title,"', '",nick,"',",commentsId,", ",depth,", '",content,"', 1, 1);");
        PREPARE stmi FROM @a;
        EXECUTE stmi;
        DEALLOCATE PREPARE stmi;

        set @results = null;
        set @results2 = null;
        set @tableName = null;
        set @tbName = null;

END

1 个答案:

答案 0 :(得分:0)

如果您使用Delimiter $$则全部;应该用$$替换存储过程中的内容。你还应该将分隔符设置为;在你的命令结束时。