SQL 2008 R2 データベースを検索で使用すると、COALESCE 関数に本当に苦労していることがわかりました。
コード:
where
i.id_categ = COALESCE(@id_categ, i.id_categ )
and i.id_brand = COALESCE(@id_brand , i.id_brand )
and i.id_model = COALESCE(@id_model , i.id_model )
and i.id_type = COALESCE(@id_karoseria, i.id_type )
and i.id_fuel = COALESCE(@id_palivo, i.id_fuel )
and (i.year between @year_from and @year_to)
and (i.price between @price_from and @price_to)
動的変数:
ALTER PROCEDURE [dbo].[spInzeratSelect]
@id_categ int = null,
@id_brand int = null,
@id_model int = null,
@id_fuel int = null,
@id_type int = null,
検索は、これらの変数の有無にかかわらず機能するはずです。
基準:
with COALESCE = Total Execution Time: 3582
without COALESCE conditions = Total Execution Time: 13
あなたは違いを得る...
COALESCE を無視して、異なるアプローチで動的 SQL 選択を作成する良い解決策はありますか?
ありがとう。