联盟基于案例

时间:2016-05-18 19:25:06

标签: sql-server-2012 case union

有没有办法根据Case语句做一个联盟:

Case
    When @var = 1 Then Select * from TableA Union Select * From TableB
    When @Var = 2 Then Select * From TableC Union Select * From TableD
    When @Var = 3 Then Select * From TableA Union Select * From TableE
    ...
End

准确的信息以及从中可以处理的表格我只是好奇这个概念在SQL Server 2012上是否可行。

因此基于该评论,这将是正确的语法:

IF @var = 1
    Select * from TableA Union Select * From TableB
ELSE
IF @Var = 2
    Select * From TableC Union Select * From TableD
Else
IF @Var = 3
    Select * From TableA Union Select * From TableE
Else
...

1 个答案:

答案 0 :(得分:0)

基于该评论来自@Lamak我能够使用以下方式获得我想要的结果:

IF @var = 1
    Select * from TableA Union Select * From TableB
ELSE
IF @Var = 2
    Select * From TableC Union Select * From TableD
Else
IF @Var = 3
    Select * From TableA Union Select * From TableE
Else
...