2016-04-01 2 views
0
result = client.execute("SELECT TOP (2000) node_id FROM [td].[node] WHERE (node_id = 220)") 
result.each do |row| 
    if result.nil? 
    puts 'Node id could not found' 
    else 
    puts row 
    puts 'Node is found on the database' 
    end 
end 

私のスクリプトにはRubytiny_tds gemが使用されています。 mssqlデータベースでは、node_id 220は存在しません。私はこのスクリプトを実行するとパスしますが、出力メッセージ 'Node id could not found'が表示されません。Ruby MSSQLクエリエラーメッセージが見つかりません

答えて

0

あなたもループに入るのですか? resultがnil(または空)の場合、ループに入る行オブジェクトはありません。

result = client.execute("SELECT TOP (2000) node_id FROM [td].[node] WHERE (node_id = 220)") 
result.each do |row| 
    puts 'I came into the loop' 
    if result.nil? 
    puts 'Node id could not found' 
    else 
    puts row 
    puts 'Node is found on the database' 
    end 
end 

これはあなたのケースでは何も生成できないと確信しています。結果がループ外で空であるかどうかをテストする必要があります。 result.empty場合 `戻ります何

result = client.execute("SELECT TOP (2000) node_id FROM [td].[node] WHERE (node_id = 220)") 
#test here whether result is empty or not 
result.each do |row| 
    puts row 
    puts 'Node is found on the database' 
end 
+0

?'あなたが望んでいるものをおそらく – user3174886

+0

が、私はあなたの結果オブジェクトの種類確かに知りません。あなたはテストすることができます。 – gvo

0

はREADMEを見てください:https://github.com/rails-sqlserver/tiny_tds

クライアントは常にResultオブジェクトを返します - ので、クエリはDBで一致する行を発見した場合でも、結果はnilされることはありません。

関連する問題