开发者

Case statement in SQL Server 2005

I am not sure what to use in this scenario, but I think a Case statement is apt.

I do not know the syntax however. Can someone please guide me?

I have a variable called @Action whi开发者_如何学运维ch can have about 30 different values.

I want to do something like this

 CASE
 WHEN @Action = 'InsertTbl1' THEN 
  BEGIN
   -- Some Insert statements and one update statements
  END
 WHEN @Action = 'RecalculateCol3' THEN 
  BEGIN
   -- Some update statements
  END
 WHEN @Action = 'Closed' THEN 
  BEGIN
   -- Some delete statements and some update statements

  END
--- and so on.....
 ELSE 
  BEGIN
  END
 END


Suggest a structure of IF and ELSE IF to mimic a switch.

IF @MyVar = 'Foo'
BEGIN
    --react to Foo        
END

ELSE IF @MyVar = 'Bar'
BEGIN
    --react to Bar       
END
ELSE
BEGIN
    --default case.
END


Yes, you can use Else If. For example:

declare @temp int
set @temp = 3

if @temp = 1
    print '1'
else if @temp > 1 and @temp < 3
    print '2'
else if @temp >= 3
    print '3'

I would still think about breaking it up into separate procedures as suggested by others

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜