Microsoft SQL Server使用UNION的多个条件语句的视图

问题描述 投票:0回答:1

在Microsoft SQL Server中有什么方法可以实现以下行为?

具有几个SQL语句的视图,可以根据传递给该视图的参数来调用它。

视图应该看起来像这样:

if param.contains "a"

  select name from table1

if param.contains "b"

  select name from table2

if param.contains "c"

  select name from table3

[我想拥有一个IF,在所有UNION之间,如果一个以上的条件为真,则结果为所有这些条件结果的UNION

例如,将以这种方式调用该视图(伪):

SELECT * FROM myView where param IN {"a", "b"}
sql sql-server tsql where-clause sql-view
1个回答
1
投票

您似乎想要:

create view myview as
select 'a' cat, name from table1
union all select 'b', name from table2
union all select 'c', name from table3

然后您可以像这样查询视图:

select * from myview where cat in ('a', 'b');
© www.soinside.com 2019 - 2024. All rights reserved.