Friday, March 23, 2012

link a table with odbc into sql server?

is it possible to link a table with odbc into sql server?
it would be nice to link an MS Access table into sql server where i could use stored procedures to access the MS Access table.Yes you can create a Linked Server so that you can get at an Access table directly from Sql Server. To do it with an ODBC connection:

1. In Enterprise Manager expand the Security->Linked Server.
2. Right click the Linked Server node and select "New linked server..."
3. Under the General tab give the linked server a name. For Server type select the "Other data source" radio button. For Provider Name select "Microsoft Jet Ole Db Provider for ODBC".
Under Datasource enter the name of the ODBC driver.
4. If the Access db requires a username and password enter them under the Security Tab.
5. Click OK. You should see this linked server listed with nodes for Tables and Views.

I suggest using OpenQuery with syntax like: OpenQuery(LinkedServerName,'Select x,y,z FROM AccessTable'). It's not required that you use OpenQuery(...) but it seems more robust. When I've tried without using it I've gotten intermittant errors.

To simplify getting at the linked server data I create a view in Sql Server like:


CREATE VIEW dbo.ViewToAccessTable
AS
SELECT *
FROM OpenQuery( LINKDEDSERVERNAME, 'Select * from SomeAccessTable')

and then just refer to this view in all your Sql Server side code.

Note that when defining the linked server it is possible to use the "Microsoft Jet OLE DB Provider" instead of going through the ODBC driver. It's probably more efficient and reliable.

No comments:

Post a Comment