Consider you are giver a task of gathering information from multiple servers. What are the different ways you can automate this?
- Create linked servers in each server pointing to your central repository
- Create Separate SSIS package in each servers which loads data to your central repository
- Create one SSIS package with multiple data sources and duplicate the tasks for each data source.
- Create one SSIS package which dynamically builds connection to each server and does the data loading tasks.
If you choose an option between 1 and 3 , then you will have to do some extra work for gathering the server information when there are new servers added to your environment.
If you choose the 4th option then also you will have some extra work but it is very small and easy when compared to the first 3 options. Here you need to just add the new Server Names to the table and that is all and everything else will be taken care by the SSIS package.
In this post I am going to explain how to create a SSIS package which loops through multiple SQL server and gathers information by building the connection to those servers dynamically.
- Open “Microsoft Visual Studio”
- Create a new “Integration Services Project”
- Create a SSIS Package
- Create 2 Connection Managers, 1 for the source (which will be set dynamically for each iteration of the loop) and the other for destination (which will remain constant, in other terms your Central Repository)
- Store All your Server Names in a table preferably in the Central Repository (Destination Connection)
- Add 2 variables
- Add an "Execute SQL Task" with SQL Statement like "select FullName from dba..tbl_ListOfServers".
- Set the result set to Full Result Set.
- On the Result Set page, Add a Result
- Set Result Name "0" and assign it to your Object variable (In our case it is “ConnectionVariable”).
- Now add a “Foreach Loop Container”
- Connect it from the “Execute SQL Task”
- Inside the “Foreach Loop Container” , add the required “Data Flow Task”
- Now Right-Click on the “Foreach Loop Container” and click on “Edit”
- Now Go to “Collection” page and Set “Enumerator” to “Foreach ADO Enumerator” and “Enumerator Configuration ” to the Object Variable(In Our Case “ConnectionVariable”) and Set “Enumeration Mode” to “Rows in the First Table”
- Now go to the “Variable Mapping” page and Choose the “String Variable” (In our case it is “ServerName”) and set the Index=0
- Now, in the “Data Flow Task” add a “OLE DB Source” (Dynamic Connection) and connect it down to a “OLE DB Destination” (which will be your Central Repository).
- Now Select the Source Connection Manager (In Our Case it is “Source”) and Right-Click on this Source and choose “Properties”.
- Expand the “Expressions” Option and click on the browse button (…)
- Now in the “Property Expression Editor”, Choose the property “ServerName” and click on browse button (…). Now choose the String Variable (in our case “ServerName”) and Drag and Drop this variable into the “Expression” box and click “OK” and “OK”.
a. Name: ConnectionVariable (A name of your choice)
Scope: Package
Type: Object
Value: System.Object
b. Name: ServerName (A name of your choice)
Scope: Package
Type: String
Value: A Valid Server Name
This article is also available in pdf format for downloading.
Please Click here to get your copy.