简体   繁体   中英

Import MS ACCESS database tables into SQL server

I am trying to import MS Access tables into SQL server.

I tried 2 methods:

1)When I follow the steps in SQL server => right click DB name => Tasks => Import Data... I will be able to import the tables successfully.

(This is the site I referred: https://help.avalara.com/Frequently_Asked_Questions/Rate_Files_FAQ/How_do_I_import_MS_Access_database_into_a_SQL_Server_database%3F )

But it is dropping all the primary key and foreign key constraints.

It is only importing the table columns not the key constraints. I want all the primary keys and foreign keys to be applied for the columns as it is in the MS Access.

2) Using SSMA (as given in this site: https://support.office.com/en-us/article/migrate-an-access-database-to-sql-server-7bac0438-498a-4f53-b17b-cc22fc42c979 ). But the SSMA version and SQL Server doesn't match and I have to install higher version of SQL Server again.

Is there any easier way to import all those tables into SQL Server along with the key constrains as well?
Please help.

Thanks in advance.

You don't mention what version of Access you have. And you also don't mention what version of SQL server you have. With any recent relative version of SQL server, you should be able to use + run SSMA. It does have/give you a choice of what version of SQL server to use. Upto and including Access 2010, there is a "built in" database export that works with SQL server, but after Access/office 2010, then you have to use SSMA, as the sql migration tools built into access were dropped.

So, you well note that for some reason you can't use SSMA, but without mention of what versions of access/sql server you are using, then our hands (and suggestions) are tied here.

Hopefully this will help, even though its a bare bones template.

I have had to migrate many a company's access db's to sql server. I have even built tools to assist in this process. Its a huge pain in the neck, but not terribly difficult in terms of actual work. You could write vba code to get tables definitions easily and then building out this template would be an easy task.

IF OBJECT_ID('tempdb..#yourtableinAccess') IS NOT NULL
    DROP TABLE #yourtableinAccess;

CREATE TABLE #yourtableinAccess
    (
        --Model After your local access table
    );
CREATE TABLE yourtableinSQL
    (
        --Model After your local access table
    );

INSERT INTO #yourtableinAccess
SELECT 
    -- build your select statement
    -- be conscious of autoincrements and what not. 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'UNC PATH TO YOUR ACCESS FILE';'admin';'', YourAccessTableName)

MERGE yourtableinSQL AS TargetTable
USING #yourtableinAccess AS SourceTable
    ON TargetTable.PRIMARYKEY = SourceTable.PRIMARYKEY
WHEN MATCHED
    AND (
        ISNULL(TargetTable.COLUMN, 0) <> ISNULL(SourceTable.COLUMN, 0)
        OR ISNULL(TargetTable.COLUMN, 0) <> ISNULL(SourceTable.COLUMN, 0)
        -- etc etc etc
        -- note isnull might not be appropriate. Just review your data structure
        )
    THEN
        UPDATE
        SET 
            TargetTable.[COLUMN] = SourceTable.[COLUMN]
            ,TargetTable.[COLUMN] = SourceTable.[COLUMN]
            -- etc etc etc
            -- build your data structure here too
WHEN NOT MATCHED
    THEN
        INSERT 
            (

            )
        VALUES 
            (

            );

Note - I used the merge statement has I needed to grab deltas of data during the migration process of each table, so a simple insert into wasnt going to be sufficient.

Anyway, happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM