Access数据导入SQLServer2008R2

时间:2024-03-31 16:08:14

环境:office Access 2010+SQLServer2008R2+Windows Server 2008R2

方式:Transact-SQL

方法:

  1. 在SQL SERVER里查询access数据:

   SELECT *

   FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0',

   'Data Source="c:\DB.mdb";User ID=;Password=')...表名

  2. 将access导入SQL server

   SELECT *

   INTO newtable

   FROM OPENDATASOURCE ('Microsoft.ACE.OLEDB.12.0',

   'Data Source="c:\DB.mdb";User ID=;Password=' )...表名

  3. 将SQL SERVER表里的数据插入到Access表中

  insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',

   'Data Source=" c:\DB.mdb";User ID=;Password=')...表名

   (列名1,列名2)

   select 列名1,列名2 from sql表

问题:

  1、如何保持标识列值不变: 

  有自动增长列时,插入会提示:仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'XXX'中的标识列指定显式值.

     set IDENTITY_INSERT [INFO] ON

     INSERT INTO [sdedb].[dbo].[INFO]
    ([ID]
    ,[NAME])
    SELECT
    [ID]
    ,[NAME]
    FROM OPENDATASOURCE ('Microsoft.ACE.OLEDB.12.0',
    'Data Source="c:\DB.ACCDB";User ID=;Password=' )...[INFO];
    set IDENTITY_INSERT [INFO] off
GO

2、SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

运行以下SQL语句启用Ad Hoc Distributed Queries:

 exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

运行以下SQL语句关闭Ad Hoc Distributed Queries:

 exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

3、安装AccessDatabaseEngine_X64.exe,不然无法找到数据库引擎