I have a number of databases with tables Table1
with the same structures. All table names are listed in the main1
database in the table testbases
. It is necessary in a loop to go through all the rows from the Table1 and insert them into query.
SELECT * FROM [DBNAME].[DBO].Table1.Client
UNION
To end up with a big query like this:
SELECT * FROM [DBNAME1].[DBO].Table1.Client
UNION
SELECT * FROM [DBNAME2].[DBO].Table1.Client
UNION
SELECT * FROM [DBNAME3].[DBO].Table1.Client
UNION
SELECT * FROM [DBNAME4].[DBO].Table1.Client
UNION
etc...
How can I do this efficiently and automatically, so I don't need to manually change the query every time we add a client?