开发者

SQL: If-condition when result does not exist? [closed]

开发者 https://www.devze.com 2023-01-14 00:58 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably an开发者_如何学Cswered in its current form.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably an开发者_如何学Cswered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

The following sql snippet below is a subselect of a parent sql.

Is there a way to say, "If customer doesn't exist I want to run a different sql"?

select orderdate, 
    (select contactname from customers where customerID = 'ALFKI' or select 'No Customer') as contactname

from orders

I know this can be solved with a join but mainly interested of the possibility?


example

IF NOT EXISTS (select * from customers where customerID = 'ALFKI')
BEGIN
    SELECT '1'
END
ELSE
BEGIN
    SELECT '2'
END


Please disregard this question. I should have been weary asking because the question sounded even confusing for me.

Turns out I was in the wrong place when the answer was elsewhere. The answer was solved with precedence parenthesis just like that. Sorry for the confusion.

rod.


The thing that you're trying to do here is called LEFT JOIN with ISNULL:

select OrderDate, isnull(c.FullName, 'No customer') CustomerName
from Orders o
left join Customers c on o.CustomerId = c.CustomerId
0

精彩评论

暂无评论...
验证码 换一张
取 消