Skip to content
Advertisement

Replace text from VIEW Definition

I have the following VIEW definition:

create view v_1
as
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'

Scenario 1: Want to remove(or replace with empty string) table t1 select statement.

View should look like:

create view v_1
as
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'  

Scenario 2: Want to remove table t4 select statement.

View should look like:

create view v_1
as
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'

Note: Depends on the requirement may want to remove any select statement from VIEW definition.

Advertisement

Answer

Instead of view, create inline table function also know as view with parameters. For example:

create function v_1
(   
    @HasQuery1 BIT
   ,@HasQuery2 BIT
   ,@HasQuery3 BIT
   ,@HasQuery4 BIT
)
RETURNS TABLE
as
RETURN
(
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
    AND @HasQuery1  = 1
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
    AND  @HasQuery2  = 1
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
    AND  @HasQuery3  = 1
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
    AND  @HasQuery4  = 1
)

You can use the function in joins like view, but you can pass which data to be returned. For example:

SELECT *
FROM T1 A
INNER JOIN V_1(1, 0, 0, 1) B
   ON ....
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement