Tải bản đầy đủ - 0 (trang)
1-12. Ad Hoc Imports of Access Data

1-12. Ad Hoc Imports of Access Data

Tải bản đầy đủ - 0trang

CHAPTER 1 ■ Sourcing Data from MS Office Applications



How It Works

This is very similar to reading—or importing—data from Excel, as described in Recipe 1-4. Indeed, it is so similar

that most of the comments stated for Excel will apply here, so I refer you to that recipe for the full details. Here,

I will only draw your attention to a subset of important points.

First, you are better off (in most cases), downloading and installing the latest ACE driver because it can read

all versions of Access from 97 upward. The old Jet driver cannot read Access 2007/2010 and the Jet driver exists

only as a 32-bit version. Also note the following:





If you are using Access 2007/2010/2013, then first download the 2007 ACE driver, as

described in Recipe 1-1.







Ad hoc queries must be enabled, as described in Recipe 1-4.



Second, I find that OPENROWSET is best when you will not be querying the database regularly, but just want to

see the data from inside SQL Server or perform an ad hoc load, and when you know the table names that you will

be querying. Note that you are sending the five parameters described in Table 1-4 to the OPENROWSET command.

Thirdly – you could need to be running SSMS as an Administrator.

Table 1-4.  OPENROWSET Parameters



Parameter



Example



Comments



Driver



'Microsoft.ACE.OLEDB.12.0'



The Jet driver can be used.



Source path and database



'C:\SQL2012DIRecipes\CH01\

CarSales.accdb'



You need the full path.



User



'admin'



The Access database user.



Password



''



Use an empty string if

there is no password.



Table, query (view) or pass-through query



stock



Can be a single Access

table or saved query or an

Access SQL string.



It is also worth paying attention to the use of semicolons to separate the three “subelements” composing the

Access database parameters, and commas to separate the driver and source data from these.

Finally, you can refine your query in several ways. In the following code snippets I will not be adding the

INSERT INTO ... SELECT or SELECT ... INTO code which you will doubtless be using. To start, you could send

an Access pass-through query, like this:

SELECT ID, Marque

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

'C:\SQL2012DIRecipes\CH01\CarSales.accdb';'admin';'',

'SELECT ID, Marque FROM stock WHERE ID = 5');

Or, if the WHERE clause is slightly more complex and involves quotes, you could try this:

SELECT ID, Marque

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

'C:\SQL2012DIRecipes\CH01\CarSales.accdb';'admin';'',

'SELECT ID, Marque FROM stock WHERE MAKE LIKE ''Tr%'' ');



38

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



You can also be selective in the data that is returned by tuning the SQL Server SELECT statement, in this way:

SELECT MAKE, MARQUE as CarType

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

'C:\SQL2012DIRecipes\CH01\CarSales.accdb';'admin';'', stock)

WHERE ID = 5;

And of course, the OPENDATASOURCE syntax works just as well, and of course, it allows you to alias columns

and apply a WHERE clause (here using the Jet driver, for a bit of variety):

SELECT MAKE, MARQUE as CarType FROM OPENDATASOURCE(

'Microsoft.Jet.OLEDB.4.0',

'Data Source = C:\SQL2012DIRecipes\CH01\CarSales.mdb;')...stock

WHERE ID = 5;

Although the title of this recipe states that this technique is for occasional data loads, there is nothing

to prevent you from using it as part of a regular T-SQL-based ETL process. However, SSIS does provide more

enhanced logging and error-trapping capacity – as well as none of the permissions issues which you can

experience with SSMS – which is why it is preferable in many cases.



Hints, Tips, and Traps





Remember that here you are writing T-SQL, not Access SQL. So, for instance, you cannot

use the IIF function with older versions of SQL Server and must use % instead of * in WHERE

clauses, as well as many other Access-specific SQL tricks!







You can use double-quotes ("") instead of doubled single quotes (‘‘ ‘‘) in LIKE clauses in

the pass-through query.







If the Access database file is password-protected, then you need to add the password to

the OPENROWSET parameters, for instance:

SELECT MAKE, MARQUE as CarType

FROM OPENDATASOURCE(

'Microsoft.ACE.OLEDB.12.0',

'Data Source = C:\SQL2012DIRecipes\CH01\CarSales.accdb;User

Id = Admin;Password = MyPassword')...stock;







If OPENDATASOURCE and OPENROWSET are using the Jet driver, they will not work in a 64-bit

environment in SQL Server (2005 and 2008). Remember that later versions of Access

and/or 64-bit environments necessitate the ACE driver. Consequently, the connection

parameter will be 'Microsoft.ACE.OLEDB.12.0'.







Unlike Excel worksheets—or Access files when using the SQL Server Import Wizard—

Access files and tables can be open when being queried with OPENROWSET and

OPENDATASOURCE.







It is a good idea to compact and repair Access files before attempting to read data from

them; corrupt data can cause problems that are best avoided.



39

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications







If you are using Access workgroup security (by definition on an Access 97–2003 database)

you need to add: 'Jet OLEDB:System database = C:\Windows\System.mdw'—or the path

to the workgroup security file.



1-13. Obtaining Access Data Without Regular Imports

Problem

You need to read data in an Access database on a regular basis, without importing the data every time.



Solution

Set up a linked server to Access. This is one way to do it:

1.



In SQL Server Management Studio, expand Server Objects. Right-click Linked Servers

and fill in the New Linked Server dialog box using the following parameters:

Linked Server



AccessACE



Provider



Microsoft Office 12.0 Access Database Engine OLEDB Provider



Product Name



Access



Data Source



C:\SQL2012DIRecipes\CH01\CarSales.accdb



The dialog box should look something like Figure 1-21.



40

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-21.  Creating an Access linked server in SSMS

2.



Click OK. You then see a linked server named Access2010 in the list of linked servers.

Of course, you have to enter your own Access data file and path in the Data Source

field.



3.



To read the data from a linked Access database, use the following four-part T-SQL

syntax:

SELECT ID, Marque from ACCESSACE...Stock WHERE ID IN(1,3) ORDER BY ID;



How It Works

This approach works best:





When you are querying the Access data regularly, but do not want to load the data into

SQL Server.



41

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

1-12. Ad Hoc Imports of Access Data

Tải bản đầy đủ ngay(0 tr)

×