Dropping and Adding Link Servers [duplicate]

♀尐吖头ヾ 提交于 2020-01-22 15:18:06

问题


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

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.

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

回答1:


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

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



来源:https://stackoverflow.com/questions/8887727/dropping-and-adding-link-servers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!