Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to code a conditional SQL statement

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2011-01-06 : 08:48:16
Could any of you SQL Guru's help me out with the below logic?
I am trying to code a conditional SQL statement, basically if/else.

if product_id is not null
select * from product
where product_id = 1234 and service_id != 5678
else
select * from service
where serivce_symbol = 'xxx' and service_id != 5678



Kristen
Test

22859 Posts

Posted - 2011-01-06 : 09:24:06
"if product_id is not null"

product_id has to be a variable / expression - like @product_id

other than that it looks fine
Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2011-01-06 : 09:32:48
Can I avoid using a variable for product_id?
Can it be done using case or exist statements?

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-01-06 : 09:59:24
Well product_id is a column - so you have to tell it what table / row you are talking about! otherwise it doesn't make sense.

Explaining what you want to have happen will help
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-01-06 : 11:43:25
Help us to help you:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -