Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do you use the case statement to execute a no. of programs base on the outcome

of a switch command

VB
How do you do the following in MS SQL SERVER 2008
  

run program segments


CASE NEW <40


do different program segment


CASE NEW >=40 AND NEW <100


do another program segment

CASE NEW >=100 AND NEW <=500

another program segment

OTHERWISE


do another program segment


Thanks
Posted
Updated 8-Oct-14 6:48am
v5
Comments
CHill60 8-Oct-14 12:43pm    
Are you asking how to use a case statement or how to run the programs?
Member 10744248 8-Oct-14 12:45pm    
the correct syntax.

please provide an example for running 3 different program segments
Maciej Los 8-Oct-14 12:51pm    
Use 'Reply' widget if you want to post comment to the other comment.

Sorry, but the description of issue is not clear. Try to improve it.
Member 10744248 8-Oct-14 12:49pm    
sorry how to use a case statement

In MS SQL Server 2008, you can use a CASE statement to conditionally execute different program segments based on the value of a variable

SQL
DECLARE @NEW INT

-- Assign a value to the @NEW variable
SET @NEW = <your_value_here>

-- Use a CASE statement to execute different program segments based on the value of @NEW
CASE 
    WHEN @NEW < 40 THEN
        -- Program segment for when @NEW is less than 40
        -- Execute your program segment here
        
    WHEN @NEW >= 40 AND @NEW < 100 THEN
        -- Program segment for when @NEW is between 40 and 99
        -- Execute your program segment here
        
    WHEN @NEW >= 100 AND @NEW <= 500 THEN
        -- Program segment for when @NEW is between 100 and 500
        -- Execute your program segment here
        
    ELSE
        -- Program segment for all other cases
        -- Execute your program segment here
END;
 
Share this answer
 
Comments
Dave Kreskowiak 26-Apr-24 9:56am    
10 years too late, but OK.
Richard Deeming 30-Apr-24 8:04am    
Also completely wrong.

Incorrect syntax near the keyword 'CASE'.
Incorrect syntax near the keyword 'ELSE'.
Dave Kreskowiak 30-Apr-24 9:52am    
Yeah, I didn't even try it. I figured if I keep hitting "Not an answer" on these old question/answers, someone was going to smack me.
 
Share this answer
 
v2
Comments
Maciej Los 8-Oct-14 13:06pm    
Short and to the point!
+5!
CHill60 8-Oct-14 17:42pm    
Thanks for sorting out the link as well!
Maciej Los 9-Oct-14 1:47am    
Welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900