order by
続編、tinytds、およびMSSQLを使用して-clauseを使用してビューを作成する必要があります
そうすると、エラーが発生します
TinyTds::Error: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. (Sequel::DatabaseError)
私のサンプルコード:
require 'sequel'
DB = Sequel.tinytds(
:host => 'server',
:database=> 'DB',
)
#Remove data from previous test
DB.drop_table(:testtab1) if DB.table_exists?(:testtab1)
DB.drop_view(:v_testtab1) rescue Sequel::DatabaseError
DB.drop_view(:v_testtab2) rescue Sequel::DatabaseError
DB.create_table(:testtab1){
primary_key :id
field :a, :type => :nvarchar, :size => 10
field :b, :type => :nvarchar, :size => 10
}
#Here the error comes up
#"SELECT * FROM `testtab1` ORDER BY `b`"
DB.create_view(:v_testtab1, DB[:testtab1].order_by(:b))
SQL側のソリューションを見るのは簡単です。の代わりに
SELECT * FROM `testtab1` ORDER BY `b`
私は〜が必要です
SELECT top 100 percent * FROM `testtab1` ORDER BY `b`
廃止された列が追加されたソリューションを見つけました(列がないdummy
と、無効なコンマが表示されます):
sel = DB[:testtab1].select(Sequel.lit('top 100 percent "" as dummy'), *DB[:testtab1].columns)
#SELECT top 100 percent "" as dummy, [ID], [A], [B] FROM [TESTTAB1]
DB.create_view(:v_testtab2, sel.order_by(:b))
同様の解決策は、次の方法で作成できますlimit
。
#Take a big number to get all entries.
#DB[:testtab1].count would take the number in moment of view creation, not usage.
sel = DB[:testtab1].limit(99999999999)
#SELECT TOP (99999999999) * FROM [TESTTAB1]
DB.create_view(:v_testtab3, sel.order_by(:b))
しかし、私はより良い解決策を探しています。別のより良い可能性はありますか?
重要な場合:
- ルビー2.1
- 続編 4.19
- tiny_tds-0.6.2-x64-mingw32
- MSSQL 10.50.2500.0、64 ビット