LEFT JOIN
(SELECT
CASE
WHEN EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'TABLE_A')
THEN
SELECT q23.MRID, q23.NTP
FROM
(SELECT MRID, NSIA4 NTP FROM TABLE_A a, TABLE_B b
WHERE a.obj = b.co
UNION
SELECT MRID, CASE WHEN a.NBR = 0 then NTPSERV END NTP FROM TABLE_C a, TABLE_B b WHERE a.obj = b.co) q23
ELSE SELECT q23.MRID , q23.NTP from ( SELECT MRID, CASE WHEN a.NBR = 0 then NTPSERV END NTP FROM TABLE_C a, TABLE_B b WHERE a.obj = b.co) q23
END ) q24 ON q0.MRID = q24.MRID
declare @sql nvarchar(max);
if exists (select 1 from information_schema.tables where table_name = 'Table_a')
begin
@sql = 'select col from table_a';
end;
else
begin
@sql = 'select col from table_b';
end;
exec sp_executesql @sql;