If the idea is to not filter by Col3 , if @list is null, you can do it the same way
Declare @list varchar(100), @Col2 varchar(100) SELECT * FROM Table1 t1 INNER JOIN dbo.Table2 t2 on t1.Col1 = t2.Col1 where t1.Col2 = ISNULL(@Col2,t1.Col2) AND (@list IS NULL OR t1.Col3 IN (select * from dbo.SplitMe(@list)))
but that would be nice if the tables were very small. For plan caching purposes, it is usually best to split all these permutations into your own SQL queries, rather than trying to write one size for the entire query or (if the number of permutations becomes too large), consider using dynamic SQL (see Dynamic search conditions in T -SQL )
Note. This does not guarantee that the Split function will not be called. There is no guaranteed bid evaluation procedure. But this means that if it is called, it will not affect the result of the request. ( Change , assuming, of course, what is called when NULL does not actually cause any cf Remus comment error)
source share