SQL 删除和添加链接服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8887727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:02:25  来源:igfitidea点击:

Dropping and Adding Link Servers

sqlsql-server-2008tsql

提问by Asynchronous

Possible Duplicate:
SQL Server: Is there an “IF EXISTS” test for a linked server?

可能的重复:
SQL Server:是否有针对链接服务器的“IF EXISTS”测试?

I am trying to create a block of code that will Create a Link Server/Drop a link server. I had posted similar question about adding and dropping a function and the solution was to drop the function and re-create it.

我正在尝试创建一个将创建链接服务器/删除链接服务器的代码块。我已经发布了关于添加和删除函数的类似问题,解决方案是删除函数并重新创建它。

So I want to take the same approach with the LinkServer. DROP it and Re-create it every time I run this code.

所以我想对 LinkServer 采取相同的方法。每次运行此代码时,删除它并重新创建它。

However, I cannot re-create the link server after dropping and I get the error message: Server already exists.

但是,删除后我无法重新创建链接服务器,并且收到错误消息:服务器已存在。

Here is my code:

这是我的代码:

IF OBJECT_ID('AccessDataSource') IS NOT NULL
EXEC master.sys.sp_dropserver 'AccessDataSource','droplogins'

GO

EXEC sp_addlinkedserver
@server     = 'AccessDataSource'
,@srvproduct = 'OLE DB Provider for ACE ' 
,@provider   = 'Microsoft.ACE.OLEDB.12.0'
,@datasrc    = 'N:\Database_Tools\AccessDB\delphi.accdb'
GO

回答by

This is actually a duplicate of at least SQL Server: Is there an "IF EXISTS" test for a linked server?

这实际上至少是SQL Server的副本:是否有针对链接服务器的“IF EXISTS”测试?

But what you want is something like:

但你想要的是这样的:

IF EXISTS(SELECT * FROM sys.servers WHERE name = N'AccessDataSource')
EXEC master.sys.sp_dropserver 'AccessDataSource','droplogins'  
GO

As is also answered in the other question - you might want to take a look at: http://msdn.microsoft.com/en-us/library/ms178530.aspx

正如在另一个问题中也回答的一样 - 您可能想看看:http: //msdn.microsoft.com/en-us/library/ms178530.aspx