Sunday, February 28, 2016

Create a Linked Server MySql to SQL SERVER

While working on one of the projects,i came across a scenario where i have a database that client was having and it was provided by some other provider through their application.I have to get data from that database and using that data i had to do some calculations on that and store the results in my database.There were two scenarios in this case:
1)i get all the required tables and data from that database and create my own database and dump those details into mine and then use it.
2)Create a link between my database and the existing database,so that i can fire queries directly on that database.
Using first approach will not give me updated records at any point of time but my scenario was realtime data.So i thought of using second approach.This approach gave me realtime data as i was querying the existing data and using it.But there is one drawback that it might be somewhat slow.
In this article i will explain how to create a linked server in Sql Server.Here i will be using ODBC Driver to fetch data from MYSQL database.Following are the steps to connect a MYSQL database to a SQL Server: First of all we need to install appropriate MYSQL Odbc Driver based on the operating system from the below link .After the driver has been installed Go To Control Panel->Administrative Tools ->Data Sources(ODBC)->System DSN.Now Press Add Button .

Select MYSQL Driver Listed(MYSQL(ODBC) 5.3 ANSI Driver) and click finish. This will open up MYSQL Configuration Window.Fill Data Source Name as MYSQL(this can be anything).TCP/IP Server as localhost.Port as 3306(default port for mysql),User Name-root,Password -your database password and click test.This will show up a success message.Now Select database and click Ok. 
.
We are done with MYSQL System DSN,Now we will Linked Server to MYSQL in SQL Server.Open Sql Server->Server Objects->Linked Server.Right Click on Linked Servers->Add New Linked Server.


This will Open up a Linked Server Properties Dialog.Fill Linked Server as MYSQL_LINKED,Select Provider as Microsoft OLEDB Provider For ODBC Drivers.Product Name as MySQl,DataSource as MySQL_Linked(whatever name is given while creating DSN).Provider String as-
DRIVER=(MySQL ODBC 5.2 ANSI Driver);SERVER=localhost;PORT=3306;DATABASE=databasename; USER=username;PASSWORD=password;OPTION=3;
Leave location as blank and Catalog as database name (in mysql).

Drill down to Server Object → Linked Servers → Providers, right-click MSDASQL, and select “Properties”. The Provider Options for Microsoft OLE DB Provider for ODBC Drivers dialog box will open allowing you to configure several options. Ensure the following four options are checked: Nested queries
Level zero only
Allow inprocess
Supports ‘Like’ Operator
All other options should be unchecked. When done, click “OK”.

In addition to this, you can enable provider options on the SQLOLEDB, In my case I select the Dynamic Parameter and Allow inprocess.
We are done with setting up a linked server.Now we have to test it by firing some basic queries.There are 3 ways by which we can query a linked server.
1)Open Query.
2)Select using 4 part notation. 

3)Execute Function. 
Open Query function requires 2 parameters 1)Linked Server Name,2)Query
select * from openquery (MYSQL_LINKED, 'select * from test.user_details'); INSERT OPENQUERY (MYSQL_LINKED, 'select name,address from test.user_details') VALUES ('Rajeev','Bangalore'); UPDATE OPENQUERY (MYSQL_LINKED, 'select name from test.user_details WHERE user_id = 100006413534648') SET name = 'Akash'; DELETE OPENQUERY (MYSQL_LINKED, 'select name from test.user_details WHERE user_id = 100006413534648')
Note:-For Update/Delete on Linked Server we need to set RPC and RPC OUT properties of Linked Server as true(Right Click Linked Server->Properties->Server Option Tab->RPC-True,RPC OUT -True.
4 Part Notation  -We can also execute queries on linked server using 4 part notations like:
SELECT * FROM linkedserver...tablename but for this we need to change MSDASQL Provider property->Check the box that says “level zero only” in providers.

select * from MYSQL_LINKED...user_details INSERT into MYSQL_LINKED...user_details(name,address) VALUES ('Rajeev','Bangalore'); UPDATE MYSQL_LINKED...user_details set name='Akash' where USER_ID='100006413534649'; DELETE from MYSQL_LINKED...user_details where USER_ID='100006413534649';
Execute Function can also be used for querying linked server.
EXECUTE('delete from test.user_details WHERE user_id = 100006413534647') AT MYSQL_LINKED

No comments :

Post a Comment