代码之家  ›  专栏  ›  技术社区  ›  George Menoutis

如何确保特定的条件首先在视图中进行评估

  •  0
  • George Menoutis  · 技术社区  · 8 年前

    请考虑以下观点:

    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 回复  |  直到 8 年前
        1
  •  0
  •   SqlKindaGuy    8 年前

    你可以这样做,但我不知道这是否满足你的其他要求,你可能有-否则你可以创建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