Last week I posted a quick example of looping through multiple servers using SSIS and being in a bit of a hurry I didn’t really finish. Last time I created the loop and got it working, but didn’t do anything with it. So this time I’m going to demonstrate using the loop to pull a list of databases and some information about each from the three different servers I set up last time into a central location.
- Starting from the end of the previous post.
- Put the name of a valid instance into the ServerName variable. If you don’t do this then when you try to use the ConnMan1 connection manager to set up the source in the data flow you’ll get errors. The expression on the connection manager will blank out the server name when it tries to make a connection if the variable remains blank.
- Add a new OLE DB connection manager pointing to the instance and database where the data will be stored. I’ve called mine “DataStore”.
- Add a table on the “DataStore” instance.
- I used the following query to create the table:
SELECT TOP 0 @@SERVERNAME AS Server_Name, sys.databases.database_id, sys.databases.name, sys.server_principals.name AS database_owner, sys.databases.compatibility_level, sys.databases.collation_name, sys.databases.recovery_model_desc, sys.databases.page_verify_option_desc INTO Database_List FROM sys.databases JOIN sys.server_principals ON sys.databases.owner_sid = sys.server_principals.sid
- For those database purists out there here is a primary key:
CREATE UNIQUE CLUSTERED INDEX pk_Database_List ON Database_List(Server_Name, database_id)
- I used the following query to create the table:
- Add an Execute SQL task to initialize the table.
- Connect the task to the loop.
- Add a dataflow task inside of the loop.
- Add an OLE DB source and an OLE DB destination to the dataflow and connect them.
- Configure the OLE DB source.
- Set the OLE DB connection manager to ConnMan1
- Set the Data access mode to SQL command
- Set the SQL command text to the following query
SELECT @@SERVERNAME AS Server_Name, sys.databases.database_id, sys.databases.name, sys.server_principals.name AS database_owner, sys.databases.compatibility_level, sys.databases.collation_name, sys.databases.recovery_model_desc, sys.databases.page_verify_option_desc INTO Database_List FROM sys.databases JOIN sys.server_principals ON sys.databases.owner_sid = sys.server_principals.sid
- Configure the OLE DB destination.
At this point you should be able to run the package and pull your database list for the servers set up in the loop. Next week I’ll demonstrate pulling the list of servers from a table.