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
 General SQL Server Forums
 New to SQL Server Programming
 Conditionated WHERE

Author  Topic 

jggtz
Starting Member

32 Posts

Posted - 2008-06-18 : 21:52:08
MS SQL 2005

I hope this is clear

How can I conditionate a WHERE in a SELECT using an input parameter?

This is,
If the parameter is 1 then use WHERE field1=A
If the parameter is 2 then use WHERE field1=A AND field2=B

Thanks

JG

Goldmember
Starting Member

9 Posts

Posted - 2008-06-18 : 23:39:47
Use the IF-Else Conditional programming logic

For example

IF(paramater1 = whatever value you expect)
WHERE field1=A
ELSE(paramater2 = whatever value you expect)
WHERE field2=B

try something similar to this

It will be difficult for the WHERE clause to use the AND boolean operator. I don't think it works, but it might if you create another IF ELSE STATEMENT after which inputs the second parameter using the second field.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 02:12:48
[code]WHERE field1=A
AND (@Parameter<>2 OR (@Parameter=2 AND field2=B))[/code]
Go to Top of Page

jggtz
Starting Member

32 Posts

Posted - 2008-06-19 : 03:27:13
quote:
Originally posted by visakh16

WHERE field1=A
AND (@Parameter<>2 OR (@Parameter=2 AND field2=B))




That's it!
Thanks
Go to Top of Page
   

- Advertisement -