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 2000 Forums
 Transact-SQL (2000)
 Help with "IF" sub query

Author  Topic 

jgn1013
Starting Member

24 Posts

Posted - 2008-09-22 : 11:03:13
I'm trying to add an IF to the end of a select query.
example
senario # 1 catselected = true and stateselected = true
(select somedata from sampletable where cat='active and state='ga')
senario #2 catselected = false and stateselected = true
(select somedata from sampletable where state='ga')
senario #3 catselected = true and stateselected = false
(select somedata from sampletable where cat='active')
senario #4 catselected = false and stateselected = false
(select somedata from sampletable )

**************************************************************
declare @catslected bit
declare @stateselected bit
decalere @allwords bit

set @allwords = 0
set @catselected = true
set @stateselected = false

IF @allwords = 0
select name, address, description from sampletable
where
IF catselected = 1 AND stateselected = 1
cat='active' and state='ga'
IF catselected = 0 AND state = 1
state='ga'
IF cateselected = 1 AND sate = 0
cat='active'
IF catselected = 0 and state = 0
/*get all with no restrictions*/


Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-22 : 11:13:04
this is what you want i guess

select somedata 
from sampletable
where ((cat='active' and catselected = true) or catselected = false)
and ((state='ga' and stateselected = true) or stateselected =false)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-22 : 11:28:03
[code]SELECT Name,
Address,
Description
FROM SampleTable
WHERE (@CatSelected = 0 OR Cat = 'Active')
AND (@StateSelected = 0 OR State = 'GA')[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -