Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5257
  • Last Modified:

Case statement T-SQL syntax


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
SNilsson
Asked:
SNilsson
  • 2
1 Solution
 
TimCotteeCommented:
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
 
SNilssonAuthor Commented:

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
 
TimCotteeCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now