I have a table [MyTable] with a column [MyColumn] NVarchar(50). I have a nonclustered index on this column, now while running the below two queries:
SELECT 1
FROM [MyTable] M
WHERE M.[MyColumn] = @MyColumn
SELECT 1
FROM [MyTable] M
WHERE M.[MyColumn] = COALESCE(@MyColumn, M.[MyColumn] )
I noticed the first query is using Index Seek (NonClustered) and the second one is using Index Scan (Non Clustered). May I know how will I make use of index seek with coalesce or isnull ?
Perhaps not an answer to your question but you can have two different queries. One for the case where
@MyColumn is nulland one for the case where you want to use@MyColumnin the where clause.