数据库 Rails 调用存储过程问题,代码和错误附上

nicetyler · 2017年08月15日 · 最后由 nicetyler 回复于 2017年08月16日 · 6162 次阅读

这个问题有点难表述,大致的就是调用存储过程的时候第 6 个参数我想传一个变量进去,然后接到存储过程返回的值,代码如下,以及报错也附上了

调用存储过程函数:

class Article

  def news_list(type_id,begin_time,end_time,page,page_size,i_count)
    rs = ActiveRecord::Base.connection.exec_query("call p_article_list( '#{type_id}','#{begin_time}','#{end_time}','#{page}','#{page_size}',#{i_count})")
    # Read through the result set hash.
    a_list = Array.new
    rs.each do |row|
      a_temp             = Hash.new
      a_temp["id"]      = row['id']
      a_temp["title"]    = row['title']
      a_temp["type_id"] = row['type_id']
      a_temp["news_time"]   = row['news_time']
      a_temp["create_user_id"] = row["create_user_id"]
      a_temp["created_at"] = row["created_at"]
      a_temp["update_user_id"] = row["update_user_id"]
      a_temp["updated_at"] = row["updated_at"]
      a_temp["description"] = row["description"]
      a_temp["content"] = row["content"]
      a_temp["status_id"] = row["status_id"]
      a_list.append(a_temp)
    end
    a_list
  end


  def free_result
    ActiveRecord::Base.connection.raw_connection.store_result while ActiveRecord::Base.connection.raw_connection.next_result

  end
end

调用这个函数:

def company_articles
  @i_count = 0
  page = params[:page]
  page = page.to_i
  if page <1 then
    page = 1
  end
  puts page
  o_test = Article.new
  o_test.free_result()
  @a_list = o_test.news_list(1,"2000-01-01","2018-01-01",page,4,@i_count)
end

存储过程代码

CREATE PROCEDURE p_article_list(IN v_type_id   TINYINT, IN v_begin_time DATETIME, IN v_end_time DATETIME, IN v_page INT,
                                IN v_page_size INT, INOUT v_count INT)
  BEGIN
/*
v_type_id
0:get all 
*/
    DECLARE m_start INT;

    SET @count=0;
    SET m_start=(v_page-1)*v_page_size;

    SELECT A.id,A.type_id,B.description AS `type`,A.title,A.news_time,
        A.description,A.content,A.status_id
    FROM t_article A
    INNER JOIN t_type B ON A.type_id=B.id
    WHERE A.flag=1
        AND (v_type_id=0 OR A.type_id=v_type_id)
        AND A.news_time BETWEEN v_begin_time AND v_end_time
    ORDER BY A.news_time DESC
    LIMIT m_start,v_page_size;

    SELECT count(1) INTO v_count
    FROM t_article A
    INNER JOIN t_type B ON A.type_id=B.id
    WHERE A.flag=1
        AND (v_type_id=0 OR A.type_id=v_type_id)
        AND A.news_time BETWEEN v_begin_time AND v_end_time;

END;

在 mysql 下的查询结果: 这个可以查出来,变量也能接住

SET @i=0;


call p_article_list( '1','2000-01-01','2018-01-01','1','4',@i);
SELECT @i;

报错截图:

不知道有大佬直到怎么解决,如果没办法就写两个存储过程吧,java 里面是可以直接传参数进去,查询可以查出来,我的主要问题就是我想传@i_count变量进去,然后获得存储过程输出的值

这个问题有人知道吗...就是分页里面我要获取搜索出来的列的数量,然后我存储过程里面返回了,但是传入参数的时候没有变量接住

需要 登录 后方可回复, 如果你还没有账号请 注册新账号