2

I am familiar using Case in a Select statement, etc.

But I am unsure how to use it to execute code, like using it instead of an If statement.

Does anyone know if it's possible? I tried this, but doesn't work:

CASE @Variable
  WHEN 0 THEN 
      SELECT * FROM TABLE1
      ...
      BREAK;
  WHEN 1 THEN
      SELECT * FROM TABLE2
      ...
      BREAK;
END

In other words, I should get this functionality:

if(@Variable=1) SELECT * FROM TABLE1
if(@Variable=2) SELECT * FROM TABLE2

But using a case statement for the argument.

This is a sample of code, so please do not take it literally. The objective is find out if you can use CASE statements in the same manner as a IF THEN statement. Like using switch in c#

Control Freak
  • 12,965
  • 30
  • 94
  • 145

1 Answers1

2

As far as I know, CASE statements do not exist. The SQL Standard only speaks of CASE expressions, of which you're aware. This has already been mentioned in this question.

Keep in mind that the IF...ELSE statements are vendor-specific, and are not part of the standard. But they're basically all we have in this regard.

Community
  • 1
  • 1
voithos
  • 68,482
  • 12
  • 101
  • 116
  • I had a feeling you couldn't use it like this. I was just asking to make sure, since all other Ms technologies I've used do have a Case/Switch statement for arguments. But I guess your answer is correct. Thank you. – Control Freak Nov 12 '14 at 00:48
  • @ControlFreak: Yes, unfortunately, T-SQL is often lacking in the "programmer-niceties" department when it comes to programmatic-type things, as opposed to relational-type things. – voithos Nov 12 '14 at 00:52