SSIS Package with Dynamic ADO Connection.
I have created a package that collects database information from different instances all over the network and stored in one central database table.
For this, I have collected Server Name, Database Name, Logical File Name, Physical File Name and File Size in MB.
I have stored all instance info in my central database table.
Table Script.
Create Table SQLServerInstances ( ServerName Varchar(20), DBName Varchar(20), UserName Varchar(20), PWD Varchar(20) ) Insert Into SQLServerInstances Values('yourLocalMachineName','AdventureWorks2012','sa','password')
I had taken one Execute SQL Task to get list of instances then move it to Foreach loop and store the values to database table using Data Flow Task.
Step1 : Drag Execute SQL Task, Foreach Loop Container and Data Flow task under Foreach Loop Container.
Step2 : Create Variables.
–> Obj_Result : DataType Object.
–> ServerName : DataType String.
–> DBName : DataType String.
–> UserName1 : DataType String.
–> PWD : DataType String.
Step3 : Right click on Execute SQL Task –> Edit and fill the details as shown in below figure.
Step4 : Select Result Set on left pane of Execute SQL Task and select your object variable.
Step5 : Right Click on Foreach Loop Container –> Edit.
Select Foreach ADO Enumerator in Enumerator and Select Obj_Result in ADO Object source varialbe field.
Step6 : Select Variable Mapping on left pane of Foreach Loop Container and select varialbes as you see in below figure.
Step7 : Now Right click on Data Flow Task –> Edit.
Step8 : Right click on OLE DB source. create one OLEDB data connection with name Dynamic Connection with your local database connection.
SQL Command.
SELECT @@ServerName as ServerName, DB_NAME(database_id) AS DatabaseName, Name AS LogicalName , Physical_Name as PhysicalName, (size*8)/1024 DBSize FROM sys.master_files
Step9 : Select Columns from left pane of OLE DB Source Editor and confirm Columns.
Step10 : Right Click on Data Conversion Task –> Edit and set data types for columns.
Step11 : Create one database table to store all servers’s database info in central database.
Create Table DatabaseInfo ( ServerName Varchar(20), DBName varchar(20), LogicalName Varchar(200), PhysicalName Varchar(200), DBSize int )
Step12 : Right click on mapping on left pane of OLE DB Destination Editor and map the varialbes to table columns.
Step13 : Now right click on “DynamicConnection” that you have created before to make it dynamic.
Rikght click on it and click on Property.
Select Expression field button that will open one popup screen.
Map DynamicConnection’s parameter with varialbes to make it dynamic and run the project.
Regards,
Nirav gajjar