SQL add filter only if a variable is not null
You can translate your requirement into : SELECT route_id [ROUTE_ID] FROM route_master(NOLOCK) WHERE route_ou = 2 AND (@l_s_query is null OR route_query = @l_s_query) AND lang_id = 1 OPTION (RECOMPILE) The OPTION (RECOMPILE) is optional but can give better execution plans at the expense of extra compilation time as discussed in the canonical article on … Read more