TinyTds ::错误:过程需要参数

时间:2015-03-25 03:45:39

标签: ruby sql-server activerecord tiny-tds

鉴于以下红宝石:

    sql2 = 'SELECT (CASE WHEN freq = 0 THEN \'pass\' ELSE \'fail\' END) FROM ( 
SELECT COUNT(*) freq FROM Grant_Award WHERE Grant_Award.org_ticker IS NULL )x;'
    pp ActiveRecord::Base.connection.select_all(sql2)

    sql1 = "SELECT (CASE WHEN freq = 0 THEN 'pass' ELSE 'fail' END) FROM ( SELECT COUNT(*) freq FROM Grant_Award WHERE Grant_Award.org_ticker IS NULL )x;"
    pp ActiveRecord::Base.connection.select_all(sql1)

    sql0 = File.readlines(File.join(BASE,'db','scripts','test','pass_or_fail.sql'))
    sql = (sql0.map {|l| l.strip}).join(' ')
    pp sql
    pp ActiveRecord::Base.connection.select_all(sql)

以上结果产生以下结果:

#<ActiveRecord::Result:0x38b0ce0
 @column_types={},
 @columns=[""],
 @hash_rows=nil,
 @rows=[["pass"]]>

#<ActiveRecord::Result:0x38b5ae0
 @column_types={},
 @columns=[""],
 @hash_rows=nil,
 @rows=[["pass"]]>

"SELECT (CASE WHEN freq = 0 THEN 'pass' ELSE 'fail' END) FROM ( SELECT COUNT(*) freq FROM Grant_Award WHERE Grant_Award.org_ticker IS NULL )x;"

C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-sqlserver-adapter-4.2.2/lib/active_record/connection_adapters/sqlserver/database_statements.rb:336:in `each': TinyTds::Error: Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.: EXEC sp_executesql 'SELECT (CASE WHEN freq = 0 THEN ''pass'' ELSE ''fail'' END) FROM ( SELECT COUNT(*) freq FROM Grant_Award WHERE Grant_Award.org_ticker IS NULL )x;' (ActiveRecord::StatementInvalid)

执行sql的前两次尝试返回就好了。第三个,从一个文件读入一个数组,然后加入,炸弹。我知道这必须与琴弦的处理有关。但我无法弄清楚这一点。

我做错了什么?为什么呢(tinytds或activerecord?)认为这是一个存储过程?

我想要做的是在文件夹中包含一大堆sql脚本,然后逐个执行它们。

详细信息:ruby 1.9.3,activerecord(4.2.0)和tiny_tds(0.6.2-x86-mingw32)

2 个答案:

答案 0 :(得分:1)

我今天早上醒来并意识到它必须与磁盘上文件的字符集有关。以下是一个解决方案[无需更改查询]:

ActiveRecord::Base.connection.select_all(sql0.join(" ").encode("UTF-8"))

我在提交时将字符集强制为utf-8,现在一切正常。

证明:

#<ActiveRecord::Result:0x3bede68
 @column_types={},
 @columns=[""],
 @hash_rows=nil,
 @rows=[["pass"]]>

#<ActiveRecord::Result:0x3bf2c68
 @column_types={},
 @columns=[""],
 @hash_rows=nil,
 @rows=[["pass"]]>

"SELECT (CASE WHEN freq = 0 THEN 'pass' ELSE 'fail' END) FROM ( SELECT COUNT(*) freq FROM Grant_Award WHERE Grant_Award.org_ticker IS NULL )x;"

#<ActiveRecord::Result:0x3bf5410
 @column_types={},
 @columns=[""],
 @hash_rows=nil,
 @rows=[["pass"]]>

答案 1 :(得分:0)

我认为文件导入生成的语句应为:

SELECT (CASE WHEN freq = 0 THEN 'pass' ELSE 'fail' END) FROM ( SELECT COUNT(*) freq FROM Grant_Award WHERE Grant_Award.org_ticker IS NULL );"

我认为语句末尾的x导致了错误,并想知道这是否已经误入了你的其他行。