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.
| Author |
Topic |
|
acko
Yak Posting Veteran
52 Posts |
Posted - 2003-06-22 : 15:02:13
|
| Hi everybodyCan I use case statement in WHERE clause.For exampleDECLARE @VAR1 nvarchar(50)DECLARE @VAR2 intSELECT * FROM ORDERSWHERE CASE @VAR1 WHEN 'Customers' THEN CustomerID = @VAR2 WHEN 'Employee' THEN EmployeeID = @VAR2 ENDThis 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 ENDAND EmployeeID = CASE @VAR1 WHEN 'Employee' THEN @VAR2 ELSE EmployeeID END |
 |
|
|
acko
Yak Posting Veteran
52 Posts |
Posted - 2003-06-23 : 09:33:04
|
| Thanks very much |
 |
|
|
|
|
|