andrea
andrea

Reputation: 1358

Switch or Case in SQL

I'm writing a store procedure that needs to perform a SWITCH of a string variable and doing something different depending on the value, a pseudo-code could be:

CASE @my_string
WHEN 'value'
UPDATE table 1
WHEN 'Other_value'
UPDATE table 2

I can't find any example of a switch done in this way, is it possible or do I need to use IF/ELSE everytime?

Thank you

Upvotes: 0

Views: 122

Answers (1)

Lamak
Lamak

Reputation: 70668

Using IF would be the typical way:

IF @my_string = 'value'
BEGIN
     UPDATE SomeTable
     WHERE somecondition
END
IF @my_string = 'Other_value'
BEGIN
     UPDATE SomeOtherTable
     WHERE somecondition
END

Upvotes: 3

Related Questions