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
精彩评论