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)
 Integrate IF/THEN/ELSE statement in WHERE clause

Author  Topic 

clasch
Starting Member

1 Post

Posted - 2009-07-28 : 19:21:37
Hi there,
I have a WHERE clause containing several 'AND'-statements. After one of this 'AND' I need an IF-statement integrated.
I always get an error message that there is an incorrect syntax near the 'IF' but can't figure out why.
Example
WHERE Expression1
and (Expression2)
and (Expression3)
and IF
BEGIN (Expression4) END
THEN Expression5
ELSE Expression6
and (Expression7)
ORDER BY ...

Any help how I could fix this problem? Thanks a lot!

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-07-28 : 19:47:12
IF can only be used as a flow of control statement.

You can probably use a CASE for what you want to do. You can read about CASE in SQL Server 2005 Books Online.


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -