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)
 Case in Where Clause

Author  Topic 

acko
Yak Posting Veteran

52 Posts

Posted - 2003-06-22 : 15:02:13
Hi everybody
Can I use case statement in WHERE clause.
For example

DECLARE @VAR1 nvarchar(50)
DECLARE @VAR2 int
SELECT * FROM ORDERS
WHERE CASE @VAR1
WHEN 'Customers' THEN CustomerID = @VAR2
WHEN 'Employee' THEN EmployeeID = @VAR2
END

This is not working OK.
Can someone explain me the right way?
Thanks.
Best regards
Alex



robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-06-22 : 15:16:17
CASE does not control the flow of execution (as it does in VB), it returns a value like a function does. Therefore, CASE will not work unless it has an equal sign directly to its left.

DECLARE @VAR1 varchar(50), @VAR2 int
SELECT * FROM ORDERS
WHERE CustomerID = CASE @VAR1 WHEN 'Customers' THEN @VAR2 ELSE CustomerID END
AND EmployeeID = CASE @VAR1 WHEN 'Employee' THEN @VAR2 ELSE EmployeeID END


Go to Top of Page

acko
Yak Posting Veteran

52 Posts

Posted - 2003-06-23 : 09:33:04
Thanks very much

Go to Top of Page
   

- Advertisement -