i need results based on a condition with out using dynamic sql
like when @param1=10
then i need all rows details from table1
else i need details from table2
i had tried something like below but it does not give the result
declare @param1 int=10;
select * from
case when @param1=10 then table1
else table2 end
Note : i know i can use if condition like below
if @param1=10
select * from table1
else
select * from table2
but in my case the query is very lengthy so i don't want to replace all query once again
so i hope i can just do something with case with out replacing all query again.
please help me to solve the problem
You can place a
UNION ALL
as a subquery, and then build the rest of your query around that:As an aside - if you have two tables that contain the same "types" of rows, such that you're wanting to interchange them in queries, it may be an indication that your data model is broken - what should have been modelled as a attribute has instead become embedded in your table names. E.g. rather than having
FemaleEmployees
andMaleEmployees
tables, it ought to be a singleEmployees
table with aGender
column.