if else within CTE?

前端 未结 4 2145
孤独总比滥情好
孤独总比滥情好 2021-01-02 07:51

I want to execute select statement within CTE based on a codition. something like below

;with CTE_AorB
(
  if(condition)
    select * from table_A
   else
           


        
4条回答
  •  盖世英雄少女心
    2021-01-02 08:13

    If you are using a parameter, then you only need one statement.

    @ID (Some parameter)
    
    ;with CTE
    (
        select * from table_A WHERE id = @ID
        union all
        select * from table_B WHERE (id = @ID and condition)
    )
    

提交回复
热议问题