SQL Server 中需要访问MySQL的数据,可以通过调用MySQL的ODBC驱动,在SQL Server中添加LinkServer的方式实现。
1.从MySQL网站下载最新的MySQL ODBC驱动:http://www.mysql.com/downloads/connector/odbc/,目前的版本是5.3.9
2.在SQL Server所在的服务器安装
3.系统DSN–添加
4.在SQL Server建立Linkserver,可通过如下脚本实现:
EXEC master.dbo.sp_addlinkedserver @server = N'MYSQLLINK', @srvproduct=N'Mysql', @provider=N'MSDASQL', @datasrc=N'mysql'--此处的mysql是刚才所建数据源名称 EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'MYSQLLINK',@useself=N'False',@locallogin=NULL,@rmtuser=N'root',@rmtpassword='123456'
5.执行查询验证
SELECT * FROM OPENQUERY (MYSQLLINK, 'SELECT * FROM tablename')
6.操作mysql数据库
--操作MySQL 数据库 insert into openquery(mysqllink,'select * from testtable') select 1,'Peter',30; update openquery(mysqllink,'select * from testtable') set name='Jone' where id=1; delete from openquery(mysqllink,'select * from testtable') where id=1;