I have a stored procedure which has more than 2000 lines of code .I need to change the table names in from clause based on a condition .The column names will remain same in both the tables .
For example:-
Declare @condition char(1)
set @condition=(Select close_flg from log
where last_run_dt >=
(Select cur_dt from dt)
)
--If @condition is 'A' then below sql will execute
Select a.columnA,
Case
when @condition='A' then 'D'
Else 'M'
END,
b.ColumnC
from TableA as a
inner join TableB as b
on a.Column=b.Column
Now if @condition is ‘B’ then i need to change the sql as
(Only the inner join is different rest all the columns in the select query are same )
Select
-- the same columns as above
from TableC as c
inner join TableD as d
on c.Column=d.Column
I want to make my SQL generic to reduce the duplicate code .Is it even possible to do so .
Thanks
You can use dynamic query for your scenario like this :
Now transform above technique according to your scenario.