вторник, 27 ноября 2012 г.

How-to access to Acces or Excel file from MS SQL Server 64bit

  1. Install Microsoft Access Database Engine 2010 64bit and Service Pack 1 for Microsoft Access Database Engine 2010 64bit
  2. Run this SQL command
    exec master..xp_enum_oledb_providers
    In result you will see "Microsoft.ACE.OLEDB.12.0"
  3. Set SQL Server parameter "ad hoc distributed queries" to 1 with this script
    sp_configure 'show advanced options', 1;
    go
    reconfigure;
    go
    sp_configure 'Ad Hoc Distributed Queries', 1;
    go
    reconfigure;
    go
    You can read about parameter "ad hoc distributed queries" in this article ad hoc distributed queries Server Configuration Option
  4. If you get an error "Ad hoc update to system catalogs is not supported." run this SQL command
    sp_configure 'allow updates', 0;
    go
    reconfigure;
    go
  5. Set options "AllowInProcess" and "DynamicParameters" for  Microsoft.ACE.OLEDB.12.0  provider to 1. Run this SQL script
    use [master]
    go
    exec master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
    go
    exec master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
    go
  6. MDB file should be in local folder in SQL Server.
  7. After all you can use this SQL command like this
    SELECT CustomerID, CompanyName
          FROM OPENROWSET('Microsoft.Ace.OLEDB.12.0',
                'C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb';
                'admin';'',Customers)