title: Dropping and Adding Link Servers
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3104186/sql-server-is-there-an-if-exists-test-for-a-linked-server">SQL Server: Is there an &ldquo;IF EXISTS&rdquo; test for a linked server?</a>  </p>
  </blockquote>
  
  
  
  <p>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.</p>
  
  <p>So I want to take the same approach with the LinkServer. DROP it and Re-create it every time I run this code.</p>
  
  <p>However, I cannot re-create the link server after dropping and I get the error message: Server already exists.</p>
  
  <p>Here is my code:</p>
  
  <pre><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
  </code></pre>