Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Case statement T-SQL syntax

Posted on 2004-04-07
3
Medium Priority
?
5,241 Views
Last Modified: 2010-08-05

This works:
SET @X = Case @Y
          WHEN 1 THEN 444
          WHEN 2 THEN 555
END


How can I make this work:
Case @Y
    WHEN 1 THEN
          SET @X =  444
          SET @Z =  222
    WHEN 2 THEN
          SET @X =  555
          SET @Z =  333
END
0
Comment
Question by:SNilsson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 10772925
Hi SNilsson,

You cannot, the case statement doesn't work that way, you need to use IF..ELSE syntax:

IF @Y = 1
    Begin
        Set @X = 444
        Set @Z = 222
    End
If @Y = 2
    Begin
        Set @X=555
        Set @Z=333
    End

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 8

Author Comment

by:SNilsson
ID: 10772985

Okey, in that case what would be the most efficient, using several If statement or two case statements (Y can hold 7 different valuse so it would be 7 IF's) ?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10773002
SNilsson,

50/50 I guess, either way you have 14 sets to do, probably the IFs would be easier to read and maintain.

Tim.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question