that looks fine, actually, in the basics, so it should work
the question is: why you want/need dynamic sql.
with CTE, you would avoid dynamic sql which may give you issues at other levels.
;WITH main AS ( Select * from tableA inner join table B ON ... )SELECT * from main join table_c on ...UNION ALLSELECT * from main join table_d on ...
the question is: why you want/need dynamic sql.
with CTE, you would avoid dynamic sql which may give you issues at other levels.
Open in new window