views:

48

answers:

3
+1  Q: 

sql help please

Hi All,

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?

thanks, rod

+1  A: 

example

IF NOT EXISTS (select * from customers where customerID = 'ALFKI')
BEGIN
    SELECT '1'
END
ELSE
BEGIN
    SELECT '2'
END
SQLMenace
Can this be done inline within a parent sql as a subselect?
rod
post more details
SQLMenace
+1  A: 

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.

rod
A: 

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
Denis Valeev