免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4326 | 回复: 3
打印 上一主题 下一主题

sql server DB link [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-14 20:55 |只看该作者 |倒序浏览
sql server DB link



--add link

exec  sp_addlinkedserver @server='DBLinkCompalAPS',@srvproduct='',@provider='SQLOLEDB',@datasrc='cetebiddb',@catalog='bccdb'

exec  sp_addlinkedsrvlogin 'DBLinkCompalAPS',false,null,'ebid','******'



EXEC master.dbo.sp_addlinkedserver @server = N'link_cetebiddb',

            @srvproduct=N'sqldbmo', @provider=N'SQLOLEDB', @datasrc=N'cetebiddb',

            @provstr=N'User ID=ebidassword=******'

--drop link

Exec sp_droplinkedsrvlogin DBLinkCompalAPS,Null

Exec sp_dropserver DBLinkCompalAPS



--select

select *  from sys.servers



select top 1 *from DBLinkCompalAPS.bccdb.dbo.BCC_Admin_CartStyle





--

proc



SET QUOTED_IDENTIFIER ON

GO

SET ANSI_NULLS ON

GO





/**************************************************

name: usp_Create_DBLink_DBLinkCompalAPS

purpose: Created  DBLik for ControlDB

used table: APSCN10.dbo.pl_controlDB_inf

**************************************************/

ALTER     proc [dbo].[usp_Create_DBLink_DBLinkCompalAPS]

           @parm_plant nchar(04)

as

begin

set nocount on

declare @server nvarchar(10),@db nvarchar(10),@uid nvarchar(10),@pwd nvarchar(50),@web_uid nvarchar(10),@web_pwd nvarchar(20)

select top 1 @server=ControlDB_Server_name

                     ,@db=ControlDB_DB_name

                     ,@uid=ControlDB_uid

                     ,@pwd=ControlDB_password

                      ,@web_uid=substring(';'+aps_db_connection+';',charindex(';Uid=',';'+aps_db_connection+';')+len(';Uid='),charindex(';',';'+aps_db_connection+';',charindex('Uid=',';'+aps_db_connection+';'))-(charindex(';Uid=',';'+aps_db_connection+';')+len(';Uid=')))

                      ,@web_pwd=substring(';'+aps_db_connection+';',charindex('wd=',';'+aps_db_connection+';')+len('wd='),charindex(';',';'+aps_db_connection+';',charindex('Pwd=',';'+aps_db_connection+';'))-(charindex('wd=',';'+aps_db_connection+';')+len('wd=')))

from pl_controlDB_inf

where plant=@parm_plant

declare @sql nvarchar(2000)



Exec sp_droplinkedsrvlogin DBLinkCompalAPS,Null



Exec sp_dropserver DBLinkCompalAPS



set @sql='exec  sp_addlinkedserver @server=''DBLinkCompalAPS'',@srvproduct='''',@provider=''SQLOLEDB'',@datasrc='''+@server+''''

exec (@sql)



set @sql='exec  sp_addlinkedsrvlogin ''DBLinkCompalAPS'',''false'',null,'''+@web_uid+''','''+@web_pwd+''''

exec (@sql)

  

set @sql='exec  sp_addlinkedsrvlogin ''DBLinkCompalAPS'',''false'',null,'''+@uid+''','''+@pwd+''''

exec (@sql)



set nocount off

end





GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

论坛徽章:
0
2 [报告]
发表于 2011-03-14 21:00 |只看该作者
http://www.packtpub.com/article/ ... -on-sql-server-2008
Linking servers provides an elegant solution when you are faced with running queries against databases on distributed servers or looking at your distributed assets on disparate databases.

This article by Dr. Jay Krishnaswamy explains how to set up a MySQL linked server on SQL Server 2008 Enterprise. Configuring a linked MySQL server as well as querying a table on the MySQL linked server is described. The reader would benefit reviewing the first article on this series on MySQL Servers.
Introduction

MS SQL servers always provided remote access to servers through RPC mechanisms, but they had the limitation of being confined to invoking procedures on remote locations. A linked server (a virtual server) may be considered a more flexible way of achieving the same thing, with the added benefits of remote table access and distributed queries. Microsoft manages the link mechanism via OLE DB technology. Specifically, an OLE DB datasource points to the specific database that can be accessed using OLEDB.

In this article we will be creating a MySQL linked server on SQL Server 2008 and querying a database [TestMove] table shown in the next listing . In reviewing the previous article it may be noticed that the Employees tables was moved to MySQL database TestMove. In running the commands from the mysql> prompt it is assumed that the MySQL Server has been started.
Listing 1: employees table in TestMove

mysql> show tables;
+--------------------+
| Tables_in_testmove |
+--------------------+
| employees          |
+--------------------+
1 row in set (0.09 sec)

mysql>

Creating an ODBC DSN for MySQL

In the previous article on MySQL Servers cited earlier, a DSN was created for moving data. Essentially the same DSN can be used. Herein follows a brief review of the DSN MySQL_Link created along the same lines as in the previous referenced article. The ODBC driver used for creating this ODBC DSN is the one installed on the machine when the MySQL Server was installed as shown.

MySQL Linked Server on SQL Server 2008

The final interactive window where you may test the connectivity is shown in the next figure. You may notice that the database Testmove has been named in the ODBC DSN. The name MySQL_LINK is the ODBC DSN.

MySQL Linked Server on SQL Server 2008

When you close the window after clicking the OK button, a ODBC DSN item will be added to the System DSN tab of the ODBC wizard as shown.

MySQL Linked Server on SQL Server 2008
Steps to create a linked server from Management Studio

Right click the Linked Servers node to display a drop-down menu as shown in the next figure.

MySQL Linked Server on SQL Server 2008

Click on New Linked Server...item. This brings up the New Linked Server window as shown. The window is all empty except for a default Provider.

MySQL Linked Server on SQL Server 2008

The very first thing to do is to provide a name for this linked server. Herein it is LINKED_ MYSQL. We will be using one of the providers [Microsoft OLE DB Provider for ODBC] supported by SQL Server 2008. You may access the list of OLE DB Providers in the Providers sub-folder of the Linked Servers. Provide the other details as shown in the next figure. Make sure they are entered exactly as shown or according to how you have created the database on MySQL Server.

MySQL Linked Server on SQL Server 2008

Click on the Security list item under General in the left. This opens the 'Security' page of the New Linked Server wizard as shown. Change the login from the default "Be made without using a security context" to "Be made using this security context". Enter remote login. In this case it is "root" for the remote login and the password is the one used during the ODBC DSN (also the password for server authentication) creation.

MySQL Linked Server on SQL Server 2008

Make no changes to the Server Options page. Click OK. This creates a linked server Linked_MySQL as shown expanded in the Linked Server's node as shown. You may need to right click and refresh the Linked Servers' node to see the new linked server. As you can see in the figure, the 'User' tables are not displayed.

MySQL Linked Server on SQL Server 2008




Running Queries and reviewing results

Running system stored procedures can provide various levels of information and the database can be queried using the four part syntax and the openquery() method.
Information on the linked server

It is easy to find how the linked server is configured using system stored procedure sp_linkedsrvlogin on the SQL Server 2008. Open a Query window from File | New | Query Current Connection to open the query window and type in the following statement. The next figure shows the statement as well as the returned values. SQL Server 2008 querying has the intellisense report and this must be put to good use.

Exec sp_linkedsrvlogin

This shows all servers both local and remote as shown in the next figure.

MySQL Linked Server on SQL Server 2008

Information about the tables on the remote server can also be accessed by running a stored procedure. Executing the stored procedure sp_tables_ex as shown in the next figure (which displays the statement and the result of executing the stored procedure) can be used to obtain table information.

MySQL Linked Server on SQL Server 2008
Querying the table on the database

Data in the table on the linked server can be queried using the openquery() function. The syntax for this function shown next is very simple.

openquery ('linked server', 'query')

The next figure shows the result of running the openquery() function on the Linked_MySQL linked server.

MySQL Linked Server on SQL Server 2008

Although it should be possible to query the linked server using the four part syntax as in:

Select * from LINKED_MYSQL...employees

The above statement returns an error. This is probably a limitation of a combination of MSDASQL and the ODBC driver which does not provide the schema information correctly(this is just the author's opinion).
Are Remote Procedure Calls (RPC) allowed?

The easiest way to test this is to send out a call by running the following query against the linked server.

Execute('Select FirstName, LastName from employees') at Linked_MYSQL

If the linked server is not configured for RPC, then the result you get by running the above query is as shown in the next figure.

MySQL Linked Server on SQL Server 2008
Turn on RPC

Earlier on we skipped the Server Options page of the linked server. Back in the Management Studio right click linked server LINKED_MYSQL and from the drop-down choose to look at the properties at the bottom of the list. This brings up the LINKED_MYSQL properties window as shown. Click on Server Options. In the Server Options page change the values of RPC and RPCOUT to true, default for both being false.

MySQL Linked Server on SQL Server 2008

Now run the query that produced the error previously. The result is displayed in the next figure.

MySQL Linked Server on SQL Server 2008

You might have noted that only two columns were returned from the employees table. This was deliberate as trying to get all the column would produce an error due partly to the data types of data stored in the table and their compatibility with MSDASQL and the ODBC driver (Again, an author's opinion).
Creating Linked Server using TSQL

While the linked server can be created using the built-in wizard of the Management Studio, it can also be created using TSQL statements as in the following listing (run both statements, the first one creates the linked server and the second the logins).
Listing 2:

Exec master.dbo.sp_addlinkedserver
@server=N'MySQlbyCode',
@srvprodcut=N'MySQL',
@provider=N'MSDASQL',
@datasrc=N'MySQL_link'

Exec master.dbo.sp_addlinkedserverlogin
@server=N'MySQlbyCode',
@locallogin=NULL,
@rmtuser=N'root',
@rmtpassword=N'<your password>'
@rmtsrvname=N'localhost'

Summary

The article described the steps involved in configuring a MySql Linked server on SQL Server 2008 using the built-in New Linked Server wizard as well as TSQL. Method to query the linked server as well as enabling RPC were described.

If you have read this article you may be interested to view :

论坛徽章:
0
3 [报告]
发表于 2011-03-14 21:31 |只看该作者
本帖最后由 liyihongcug 于 2011-03-14 21:32 编辑

有时候 网上说的都是有问题 。版本更新过快 。 有编程经验对 解决问题非常有帮助
select * from [链接名字]。库名。schema.表名

反复测试

先调通 再看那些脚本 更容易理解 。

论坛徽章:
0
4 [报告]
发表于 2011-03-15 14:35 |只看该作者
Exec sp_helplinkedsrvlogin
exec sp_tables_ex 'LINKED_MYSQL'
select * from openquery(LINKED_MYSQL, 'select * from a_copy') -----成功
select * from openquery(LINKED_MYSQL, 'update a_copy ser u=1')----失败
Msg 7357, Level 16, State 2, Line 1
无法处理对象 "update a_copy ser u=1"。链接服务器 "LINKED_MYSQL" 的 OLE DB 访问接口 "MSDASQL" 指示该对象没有列,或当前用户没
sqlserver2008 可以异构访问 不同的db库.比如mysql5 .可以查询数据 . 但是在不能sqlserver2008更新  异构库mysql5的数据
商业db功能全强大 好用 稳定 .确实好
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP