Hi Aravind , Thankyou for using Microsoft Q&A platform and thanks for posting your query here.
As per my understanding , you are trying to build a solution in ADF to fetch the DBNames from the SQL Server dynamically and query the data of tables from all the available DBs in the server and copy it to destination table. Please let me know if that is not what you are looking for.
First of all , you need to keep the lookup/control/metadata table called details table updated to hold all the available DBNames. For that , you can use the inbuilt sys object to find out all the available DBs : SELECT name FROM sys.databases
You can write an Stored procedure to find out all the DB names coming from sys.databases table left joining with details table and finding all non-matching records(present in sys.databases but not in details table) and insert the same in details table.
Now , You would also need all the table names which needs to be copied from all the databases. To store the table names
Since there would be multiple tables that needs to be copied from all the databases, following query can be used for that: SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'
- Store the table name too in the control table to loop through. Cross apply both the results and store in the final control table. Suppose you have 500 DB having 10 tables each then now the control table would have 5000 rows .
Now, You can use lookup activity to get the db names and table names from the details table.
Next, use ForEach activity to loop through each of the records.
Create parameterized linked service where Dbname would be fetched dynamically and create parameterized dataset where tablename would be fetched dynamically .
Inside the foreach, use copy activity and in source formulate the query as select * from @{item().dbname}.@{item().tablename} .
Make sure to use pre-copy script in sink settings to truncate the table at target before the load .
Hope it helps. Please accept the answer by clicking on Accept answer button. Thankyou