Solved

Case statement T-SQL syntax

Posted on 2004-04-07
3
5,224 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 125 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connect to SQL 2008 r2 server over the Internet 4 86
Access Runtime Error '3070' 5 52
Reoccurring Access Query 24 48
DB Shutdown Automatically 11 38
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

710 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