I have used COALESCE many times, but I guess I never came across this particular situation. Suppose there is only one line on @ param1.
select @test = COALESCE (column1, 'default') FROM Table1 WHERE column3 = @param1
In this case, if column1 is null, "default" is selected when the sql statement actually returns a row. What to do if there are no matching entries for @ param1.
Suppose I want to always have a default value for @test. Is this right or is there another way?
select @test = COALESCE( (select column1 FROM Table1 WHERE column3 = @param1), 'default').
I assumed that select @test = COALESCE (column1, 'default') FROM Table1 WHERE column3 = @param1 will contain "default" even if it did not return the row. Not.
I suppose that I can also check if @test is NULL after that and assign a default value.
source share