Hola a tod@s.
Tengo una consulta, que no es que sea muy pesada, pero la tengo utilizo 4
veces en un select union all, donde lo que cambian son los parámetros del
where:
select 1 from tabla1 inner join tabal2 on a = b
where salida = @ida and ofe = 0
union all
select 1 from tabla1 inner join tabal2 on a = b
where salida = @llegada and ofe = 0
union all
select 1 from tabla1 inner join tabal2 on a = b
where salida = @ida and ofe <> 0
union all
select 1 from tabla1 inner join tabal2 on a = b
where salida = @llegada and ofe <> 0
Había pensado guardar los datos del select1 (sin tener en cuenta las
condiciones) en una variable tabla, y sobre ésta realizar las uniones:
select 1 from @tabla1
where salida = @ida and ofe = 0
union all
select 1 from @tabla1
where salida = @llegada and ofe = 0
union all
select 1 from @tabla1
where salida = @ida and ofe <> 0
union all
select 1 from @tabla1
where salida = @llegada and ofe <> 0
¿Vale la pena?
Muchas gracias.
Leer las respuestas