Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:24:07+00:00 2026-05-23T01:24:07+00:00

I have inherited a terribly written MS Access database that I need to import

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:24:08+00:00Added an answer on May 23, 2026 at 1:24 am

    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 Country and StateProvince. 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:

    1. Access tables Country and StateProvince are shown in screenshots #1 and #2.

    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 SelectQuery and TableName should be specified by a valid table in Access. This is needed for initial configuration of the package. Here in this case, I have chosen Country, which does exist in Access.

    3. Select the variable SelectQuery and press F4 to view the properties pane. On the Properties pane, set the property EvaluateAsExpress to True and paste the expression "SELECT * FROM " + @[User::TableName] in the Expression property. This expression will evaluate to the table that is currently being looped through. Refer screenshot #4

    4. Screenshots #5 and #6 show that the tables dbo.Country and dbo.StateProvince do not exist in SQL Server.

    5. Configure the Control Flow tab of the SSIS package as shown in screenshot #7. Place a Script Task and connect it to a Foreach Loop container. Within the container, place an Execute SQL Task and a Data Flow Task.

    6. 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 by Foreach loop container.

    7. In the SQL Server database create a stored procedure named dbo.CreateTable using 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.

    8. Configure the Foreach loop container as shown in screenshots #8 and #9.

    9. Configure the Execute SQL Task as shown in screenshots #10 and #11.

    10. 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.

    11. Now, we will configure the Data Flow Task. Place an OLE DB Source and OLE DB Destination inside the Data Flow Task. Connect the OLE DB Source to OLE DB Destination. Refer screenshot #14.

    12. Configure the OLE DB Source as shown in screenshots #15 and #16.

    13. Configure the OLE DB Destination as shown in screenshots #17 and #18.

    14. Screenshot #19 shows sample package execution within Data Flow Task.

    15. 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 Employees are added to the Access with only columns Id and Name. 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:

    CREATE PROCEDURE [dbo].[CreateTable]
    (
        @TableName  VARCHAR(255)
    )
    AS
    BEGIN
    
        SET NOCOUNT ON
    
        DECLARE @SQL VARCHAR(MAX)
    
        SET @SQL = 'IF NOT EXISTS ( SELECT  * 
                                    FROM    sys.objects 
                                    WHERE   object_id = OBJECT_ID(N''[dbo].' + @TableName + ''') 
                                    AND     type in (N''U''))
                        CREATE TABLE [dbo].' + @TableName + '(
                            [ID] [int] NOT NULL,
                            [Name] [nvarchar](255) NULL
                            ) ON [PRIMARY]'
    
        EXEC (@SQL)
    END
    GO
    

    Script Task Code:

    C# code that can be used only in SSIS 2008 and above.

    /*
       Microsoft SQL Server Integration Services Script Task
       Write scripts using Microsoft Visual C# 2008.
       The ScriptMain is the entry point class of the script.
    */
    
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.OleDb;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    
    namespace ST_9b2714c55db14556be74ca92f345c4e3.csproj
    {
        [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
        public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        {
    
            #region VSTA generated code
            enum ScriptResults
            {
                Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
                Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
            };
            #endregion
    
            public void Main()
            {
                Variables varCollection = null;
                DataTable schemaTables = null;
                ArrayList tableNames = new ArrayList();
    
                Dts.VariableDispenser.LockForWrite("User::AccessTables");
                Dts.VariableDispenser.GetVariables(ref varCollection);
    
                using (OleDbConnection connection = new OleDbConnection(Dts.Connections["AccessDB"].ConnectionString.ToString()))
                {
                    string[] restrictions = new string[4];
                    restrictions[3] = "Table";    
                    connection.Open();
                    schemaTables = connection.GetSchema("Tables", restrictions);
                }
    
                foreach (DataRow row in schemaTables.Rows)
                {
                    foreach (DataColumn column in schemaTables.Columns)
                    {
                        if (column.ColumnName.ToUpper() == "TABLE_NAME")
                        {
                            tableNames.Add(row[column].ToString());
                        }
                    }
                }
    
                varCollection["User::AccessTables"].Value = tableNames;
    
                Dts.TaskResult = (int)ScriptResults.Success;
            }
        }
    }
    

    Screenshot #1:

    1

    Screenshot #2:

    2

    Screenshot #3:

    3

    Screenshot #4:

    4

    Screenshot #5:

    5

    Screenshot #6:

    6

    Screenshot #7:

    7

    Screenshot #8:

    8

    Screenshot #9:

    9

    Screenshot #10:

    10

    Screenshot #11:

    11

    Screenshot #12:

    12

    Screenshot #13:

    13

    Screenshot #14:

    14

    Screenshot #15:

    15

    Screenshot #16:

    16

    Screenshot #17:

    17

    Screenshot #18:

    18

    Screenshot #19:

    19

    Screenshot #20:

    20

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited a VB6/Access application that we have developed and sold for many
I have inherited a poorly written web application that seems to have errors when
I have inherited a medium sized database that we are trying to use with
I have inherited a web site that has all of the paths set using
I have inherited some legacy PHP code what was written back when it was
I have inherited a project that uses LLBLGen Pro for the DB layer. The
We have inherited VB6 dll which we need to make changes to. We have
We have inherited an ant build file but now need to deploy to both
I have inherited a load of VB6 code which has tons of individual OCX
I have some code that I've inherited, and it's not the greatest in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.