I have a couple of tables in my database and I build a query to combine data from them and store that data in a temporary table on which I use as input for other queries. But since a new delivery will be added to te database on a daily basis, I would like to only change one variable, instead of going through my code and adjust every FROM clause.
Is it possible to use a concat function inside a FROM clause so that it will select the required table based on the input variable? Like:
DECLARE @colTableUP TABLE (
x varchar(50)
,y varchar(50)
,z varchar(50)
)
DECLARE @version AS string
SET @version = '20160620'
INSERT INTO @colTableUP
SELECT x, y, z
FROM CONCAT('[dbo].[Table1_', @version ,']')
UNION ALL
SELECT x, y, z
FROM CONCAT('[dbo].[Table2_', @version ,']')
UNION ALL
etc...
Since I don't get the code working I would like to know if it is possible at all to use CONCAT inside FROM, or is there any other best practice to achieve the required result instead of adjusting it all manually?