How to operate MySQL through SQL Server 2008 _mssql2008

Source: Internet
Author: User
Tags odbc mysql host
Database test Environment
1. SQL Server 2008

2. MySQL 5.1.36
Database:test
Table:testtable

Create a MySQL test table
Copy Code code as follows:

CREATE TABLE ' TestTable ' (
' id ' int (one) DEFAULT NULL,
' Name ' varchar DEFAULT NULL,
' Age ' int (one) DEFAULT NULL
)


Create link Server
To create a link to MySQL interaction (Oracle-like Dblink), in SQL Server Manager, right-click the server Objects directory in linked Servers Click New linked server:

linked server link name
Server Type Selects "Other data source"
Provider Select Microsoft OLE DB Provider for ODBC Drivers
Product name fill in MySQL host address
Provider String fills in Driver={mysql ODBC 5.1 Driver}; Server=servername;database=mydb;
User=myusername; password=mypassword;option=3;

Can also be created directly through TSQL:
Copy Code code as follows:

EXEC Master.dbo.sp_addlinkedserver
@server = N ' Mysqllink ',
@srvproduct = N ' localhost ',
@provider = N ' Msdasql ',
@provstr = N ' driver={mysql ODBC 5.1 Driver}; Server=localhost;
Database=test; User=root; password=root;option=3; '

EXEC Master.dbo.sp_addlinkedsrvlogin
@rmtsrvname = N ' Mysqllink ',
@useself = N ' False ',
@locallogin = N ' localhost ',
@rmtuser = N ' root ',
@rmtpassword = N ' root '

When you are done, you will see the links in the following figure:


Operating the MySQL database
In SQL Server, you manipulate MySQL by using the OPENQUERY function, which contains two parameters: the first is the name of the link that was created, and the second is the SQL query, which returns a read or modify operation on linked Server. Examples of the following operations:

OPENQUERY ([LinkedServer], ' SELECT * from [remotetable] ') inserts test data into TestTable:

Insert INTO OPENQUERY (Mysqllink, ' select * from TestTable ')
Select 1, ' Peter ', 30; query data:

SELECT * FROM OPENQUERY (mysqllink, ' select * from TestTable ');

Modify Data:

Copy Code code as follows:

Update OpenQuery (Mysqllink, ' select * from TestTable ')
Set name= ' Jone ' where id=1;


Delete data:
Copy Code code as follows:

Delete from OPENQUERY (Mysqllink, ' select * from TestTable ')
where id=1;

Related reference

http://dev.mysql.com/downloads/mirror.php?id=376346

Http://www.connectionstrings.com/mysql
Author: Gnie

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.