How to return default value from SQL query

后端 未结 8 1891
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 13:25

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:



        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 13:47

    Assuming the name is not nullable and that Id is unique so can match at most one row.

     SELECT 
        ISNULL(MAX(Name),'John Doe')
     FROM 
        Users 
     WHERE 
        Id = @UserId  
    

提交回复
热议问题