【问题标题】:Error while getting the row count in mysql stored procedure for second time第二次在mysql存储过程中获取行数时出错
【发布时间】:2015-06-15 18:37:29
【问题描述】:

我正在编写一个存储过程来将数据插入到 tbluser 表中。以下是我的存储过程:

BEGIN

  /* Check if user already exits*/
  DECLARE user_count INT;
  select count(*) from tblusers into user_count where name = 'joseph';  

  /* Calculate the row count of table and calculate the id for new row*/
  DECLARE totalrows,current_id INT;
  select count(*) from tblusers into totalrows; 
  SET current_id = totalrows + 1;

 /*Other part of procedure with insert statement*/

END

我收到错误名称 MYSQL 1064: 在 DECLARE totalrows,current_id INT 行;说 mysql 语法是错误的。如果我在存储过程中省略了前两个语句,那么它可以正常工作。请纠正我哪里错了,我是在 mysql 中使用存储过程的新手

【问题讨论】:

    标签: mysql stored-procedures


    【解决方案1】:

    Declare 语句必须是 begin 语句之后的第一个语句。第一个选择是把它扔掉。见:http://dev.mysql.com/doc/refman/5.0/en/declare.html

    【讨论】:

    • 但是 INT 是变量 user_count 的数据类型?我错了吗
    • 是的,这个答案不正确:INT 确实是数据类型。我想知道 MySQL 是否不喜欢语句的位置。如果你把它移到另一个 DECLARE 旁边,这样两者之间就不再有 SQL 语句了怎么办?
    • 澄清了我的问题。非常感谢丰富
    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多