I have inherited a terribly written MS Access database that I need to import into SQL. The Access database has several thousand tables in it with field definitions that are identical. I have some experience with SSIS, and importing one table is pretty simple.
However, I need to create a process where I can loop through the list of several thousand table names and import each table. I found this statement, that will get a list of all the table names in an Access database:
SELECT Name FROM MSysObjects WHERE (((MSysObjects.Type)=1) AND ((Left([Name],4))<>”MSys”)) ;
However, I am unsure of how to use this (script task syntax?). I would think I would want to do this to populate a SSIS variable of an “object” type. That way, I can use a ForEach Loop to cycle through this list of tables and perform the importing. How can I do this? Or is there a better way to cycle through each table in the database and perform the same process?
I would greatly appreciate any suggestions. Thanks you!
Here is one possible way that you can achieve loading Access data into SQL Server as long as all the tables in Access have the same structure. This example will loop through tables in Access namely
CountryandStateProvince. The package in this example will create these two tables in SQL if they don’t exist and then populate them with data from Access.Step-by-step process:
Access tables
CountryandStateProvinceare shown in screenshots #1 and #2.On the SSIS package, create two OLE DB Connections to connect to SQL Server and Access as shown in screenshot #3. Also, create 3 variables as shown in screenshot #4. Variables
SelectQueryandTableNameshould be specified by a valid table in Access. This is needed for initial configuration of the package. Here in this case, I have chosenCountry, which does exist in Access.Select the variable
SelectQueryand press F4 to view the properties pane. On the Properties pane, set the propertyEvaluateAsExpressto True and paste the expression"SELECT * FROM " + @[User::TableName]in theExpressionproperty. This expression will evaluate to the table that is currently being looped through. Refer screenshot #4Screenshots #5 and #6 show that the tables
dbo.Countryanddbo.StateProvincedo not exist in SQL Server.Configure the
Control Flowtab of the SSIS package as shown in screenshot #7. Place aScript Taskand connect it to aForeach Loop container. Within the container, place anExecute SQL Taskand aData Flow Task.Replace the code in the Script Task with the code given under the Script Task Code section. This code will loop the Access schema and will fetch only the table names. The list of table names are then stored in the package variable
AccessTables, which will then used byForeach loop container.In the SQL Server database create a stored procedure named
dbo.CreateTableusing the script provided under SQL Scripts Section. This stored procedure will create a table in the SQL Server if it didn’t already exist.Make sure that you alter the table schema defined in the stored procedure according to your needs.Configure the
Foreach loop containeras shown in screenshots #8 and #9.Configure the Execute SQL Task as shown in screenshots #10 and #11.
We cannot configure Data Flow Task at this point because the tables don’t exist in SQL Server. So, we will execute the package at this point so the Access table structures are created in the SQL Server. Screenshot #12 shows sample package execution. Screenshot #13 shows that the table structures have been created in SQL Server but they are not yet populated with data.
Now, we will configure the
Data Flow Task. Place anOLE DB SourceandOLE DB Destinationinside the Data Flow Task. Connect the OLE DB Source to OLE DB Destination. Refer screenshot #14.Configure the
OLE DB Sourceas shown in screenshots #15 and #16.Configure the
OLE DB Destinationas shown in screenshots #17 and #18.Screenshot #19 shows sample package execution within
Data Flow Task.Screenshot #20 shows that SQL Server tables are now populated with data from Access tables.
This example will work only for tables having the same structure but differing in the name. If another table named
Employeesare added to the Access with only columnsIdandName. Executing this example package will create the same table in SQL Server and will also populate it with the data.Hope that helps.
SQL Scripts:
Script Task Code:
C# code that can be used only in
SSIS 2008 and above.Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10:
Screenshot #11:
Screenshot #12:
Screenshot #13:
Screenshot #14:
Screenshot #15:
Screenshot #16:
Screenshot #17:
Screenshot #18:
Screenshot #19:
Screenshot #20: