如何确保首先评估特定的WHERE条件

时间:2018-06-22 09:28:14

标签: sql-server tsql view where-clause openquery

请考虑以下视图:

create view x as
select 1 as [source],* from some_table
union all
select 2,* from some_other_table

我跑步时

select * from x where source=1

我可以确定甚至没有执行select 2....查询吗?

原因是在我的情况下,这是一个速度缓慢的openquery,我想避免这种情况。

1 个答案:

答案 0 :(得分:0)

您可以执行类似的操作,但是我不知道这是否满足您可能有的其他要求-否则您可以创建2个视图。

CREATE proc dbo.usp_selectspecificquery 

@source int
AS

BEGIN

IF(@source = 1)
BEGIN

Select 1 as source, * from some_table

END
ELSE
Select 1 as source,* from some_table

union all
select 2 as source,* from some_other_table
END

exec dbo.usp_selectspecificquery 1