Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过activerecord sqlserver适配器连接到SQLServer2005时,从Rails执行SQL查询_Ruby On Rails_Sql Server 2005_Activerecord - Fatal编程技术网

Ruby on rails 通过activerecord sqlserver适配器连接到SQLServer2005时,从Rails执行SQL查询

Ruby on rails 通过activerecord sqlserver适配器连接到SQLServer2005时,从Rails执行SQL查询,ruby-on-rails,sql-server-2005,activerecord,Ruby On Rails,Sql Server 2005,Activerecord,因此,我通过activerecord sqlserver适配器从Rails应用程序连接到SQLServer2005 我可以通过执行 Mymodel.execute_procedure("thisProcedure", param1, param2) 但我有一个存储过程,它有一个SQL INOUT变量,我遇到了麻烦。我执行它,但我没有看到该变量返回 现在我尝试执行一些原始sql,比如 declare @thisVar int EXEC thatProcedure 1, 1, @thisVar =

因此,我通过activerecord sqlserver适配器从Rails应用程序连接到SQLServer2005

我可以通过执行

Mymodel.execute_procedure("thisProcedure", param1, param2)
但我有一个存储过程,它有一个SQL INOUT变量,我遇到了麻烦。我执行它,但我没有看到该变量返回

现在我尝试执行一些原始sql,比如

declare @thisVar int
EXEC thatProcedure 1, 1, @thisVar = @thisVar output
print @thisVar
当我这样做的时候

sql = "declare @thisVar int
EXEC thatProcedure 1, 1, @thisVar = @thisVar output
print @thisVar"

foo = Mymodel.connection.execute(sql)
我没有犯任何错误,一切看起来都很成功。我得到了具有DBI::StatementHandle类的foo。我如何实际看到来自SQL的响应


提前谢谢

自从我涉足DBI已经有一段时间了,我们离开了存储过程,转而使用ORMs。要从StatementHandle对象获取数据,需要发出fetch all。它应该具有所有的返回值/输出参数以及结果集(如果适用)作为数组。这是dbi版本.4.1,我相信这是最后一个使用sqlserver适配器的版本

sql = "declare @thisVar int
EXEC thatProcedure 1, 1, @thisVar = @thisVar output
print @thisVar"

foo = Mymodel.connection.execute(sql)

result_array = foo.fetch_all
然后,您可以在生成的数组中循环

这是DBI文档

您可能还想看看新的activerecord sqlserver适配器。它完全消除了DBI,但不确定它如何处理SP

我的建议是,无论如何,混合使用ORMs和存储过程是非常困难的,因为您正在将业务逻辑分为两层,而且管理起来越来越困难


祝你好运

谢谢你的信息!某物的定义是什么?我尝试了foo.fetch\u all,得到了“DBI::InterfaceError:语句已关闭!”doh,对此表示抱歉。当我写这篇文章的时候,我正在看2年前的代码,某物藏在里面——正如你所怀疑的那样。我承认,我在处理存储过程时绕过了AR,特别是出于这个原因,我仍然在“熟悉”rails。另一个选项可能是让存储过程将参数作为结果集返回
选择@output\u parameter作为somevalue
。那么至少你正在处理AR可以处理的事情。