Is there any easy way to return single scalar or default value if query doesn\'t return any row?
At this moment I have something like this code example:
I suppose you could use @@ROWCOUNT to see if any will be returned.
SELECT Name FROM Users WHERE Id = @UserId
if(@@ROWCOUNT = 0)
SELECT 'John Doe'
You could also use a variable if you're expecting one row.
declare @name varchar(100)
set @name = (select top 1 name from users where id = @userId)
if(@name is null) set @name = 'John Doe'
select @name