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
 SQL Server Development (2000)
 Switch Case / Select Case In SQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-18 : 10:41:58
Brian writes "I have been racking my brains to figure a more efficient way to test multiple conditions in a stored procedure under MS SQL 7.0. Idealy it would be something similar to SWITCH..CASE..CASE ELSE. I am currently using multiple IF..BEGIN..END statements, and though it performs well enough I just think there has to be a cleaner way.

Thanks
Brian Dugan"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-18 : 12:13:20
The CASE statement exists in SQL Server, but it is not a control-of-flow statement like SWITCH, it's more like a function. It works something like this:

SELECT FirstName, LastName,
CASE Sex
WHEN 'M' THEN 'Mr.'
WHEN 'F' THEN 'Ms.'
ELSE '' END AS Salutation
FROM myTable


It's important to remember that CASE returns a value. If you need to change the actions based on a condition then CASE won't work for you; you'll have to stick with multiple IF statements.

Take a look here for more info, also check Books Online for CASE:

http://www.sqlteam.com/item.asp?ItemID=922
http://www.sqlteam.com/redir.asp?ItemID=3223
http://www.sqlteam.com/redir.asp?ItemID=2212

Go to Top of Page
   

- Advertisement -