Keep value of autonumber column when importing into Microsoft Access database

时光总嘲笑我的痴心妄想 提交于 2019-12-01 09:12:44

问题


What I try is to import several tables programmatically from Microsoft SQL Server to Microsoft Access.

Each SQL Server table has an identity column, and the corresponding Access tables, an autonumber column, too.

Now I want to generate SQL scripts to copy the data from SQL Server to Access and have the autonumber colum the same value as in SQL server.

Is this possible?

When doing it the other way from Access to SQL Server, it is rather easy by using SET IDENTITY_INSERT [MyTable] ON and later SET IDENTITY_INSERT [MyTable] OFF.

I found out that there is no such statement for Microsoft Access.

In addition I tried to create the Access tables to import into first with the identity field as type LONG and later use the ALTER TABLE ... ALTER COLUMN statement to switch to autonumber. I failed in doing so.

So my question: Is there any way to achieve my goal?


回答1:


If you use Insert Into and specify all column names in MS Access, it should work.

I just created a table with the following structure

Id (autonumber)
Firstname (text)
Secondname (text)
Lastname (text)

I ran this statement

docmd.RunSQL "insert into table2 (id, firstname, secondname, lastname) values (27, 'a', 'b', 'c')"

It worked and inserted 27 into the autonumber column




回答2:


Assuming you can see both the SQL Server tables (linked tables) and the MS Access tables while in the MS Access database, here is the procedure to do it without code. These instructions are for Access 2013, so while interface elements have moved, this should work for 2003, 2007, etc.

Your Access table into which you are importing should be free of any data.

  1. Close all Access objects (tables, queries, reports, etc.)
  2. Click Database Tools on the ribbon.
  3. Optional: Click Compact and Repair (this sets the AutoNumber counters back to 0 on empty tables)
  4. Click Create on the menu.
  5. Click Query Design on the ribbon. (the Show Tables dialog is open)
  6. Add the SQL Server source table to the query.
  7. Close the Show Tables dialog.
  8. Click the Append Query button on the ribbon.
  9. Select the Access table into which you want to import data and click OK.
  10. In the SQL Server table, double-click any fields that you want to import, or double-click the * field if all column names match and you want to import all fields.
  11. For each field that is added to the append query below, check that all fields map to a field in the Access table. If not, in the Append To attribute of each field, select the field in Access that should receive the data from SQL Server.
  12. Click the Run button on the ribbon.

Access will tell you how many records you are about to append - you can use this information to verify that you are getting all your data.

You can save this query if you think you'll need to use it again to sync the tables.

If you want to get fancy, you could create a delete query which deletes all the records in the access table, then create a macro which runs everything in this order:

  1. Delete records
  2. Compact and Repair Database
  3. Run import from SQL Server



回答3:


The secret is to temporarily un-assign the autonumber as the primary key. This allows Appending AutoNumber Field to your records without any trouble (just make sure you do not have any duplicates when appending, otherwise when you re-assign the primary key you will get an error).



来源:https://stackoverflow.com/questions/3374375/keep-value-of-autonumber-column-when-importing-into-microsoft-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!