Microsoft SQL server View of multiple conditional statements with UNION
Microsoft SQL server View of multiple conditional statements with UNION
有什么方法可以在 Microsoft SQL 服务器中实现以下行为?
具有多个 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"}
您似乎想要:
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');
有什么方法可以在 Microsoft SQL 服务器中实现以下行为?
具有多个 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"}
您似乎想要:
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');