用Mysql5.6.5里运行是正确的,现在用5.1,就不对了该怎么改呢?
drop procedure if exists usp_add_comment;
CREATE PROCEDURE usp_add_comment(
 v_gameid int,
 v_commenter varchar(20),
 v_content varchar(255), 
 v_startRow int, 
 v_endRow int,
 out v_count int
)
begin
 set v_startRow=v_startRow-1;
 insert into t_comment(GameId,Commenter,Content) values(v_gameid,v_commenter,v_content); 
 commit; 
 select count(1) into v_count from t_comment where GameId=v_gameid; 
 select Commenter,CommentTime,Content from t_comment where GameId=v_gameid order by CommentTime desc limit 
--这行出错,如果直接写上数字的话,不会出错的。
v_startRow,v_endRow;
end;

解决方案 »

  1.   

    limit 后面不能用变量.
    用prepare 吧
    http://topic.csdn.net/u/20120903/14/9c8d1431-5454-43a2-b77d-882d2770e303.html?r=79578973
      

  2.   

    MYSQL中 limit后的参数不允许为变量,只能是常数。
      

  3.   

    用动态语句来完成
    SET @ASQL=CONCAT('SQL语句 limit ',数字);
    prepare stml from @asql;
    execute stml;
      

  4.   


    limit后可以为变量。DELIMITER $$USE `test`$$DROP PROCEDURE IF EXISTS `test`$$CREATE DEFINER=`admin`@`%` PROCEDURE `test`(v_limit INT)
    BEGIN
        
        SELECT * FROM a LIMIT v_limit;    
        
    END$$DELIMITER ;====================================================mysql> SELECT * FROM a ;
    +------+------+
    | c_id | name |
    +------+------+
    |    1 | a    | 
    |    2 | b    | 
    |    3 | ??   | 
    +------+------+
    3 rows in set (0.11 sec)mysql> call test(1);
    +------+------+
    | c_id | name |
    +------+------+
    |    1 | a    | 
    +------+------+
    1 row in set (0.04 sec)
      

  5.   

    5.1版本里面不允许limit 后面带变量,5.5之后改进了,支持变量。
    5.1只能通过prepare 来实现。