Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete All SQL Server Linked Servers on single server

Tags:

sql

sql-server

Im using SQL Server Management Studio 2008 (ssms.exe) connected with a local SQL Server 2000, so I notice that every time I try enter on Linked Server option It crash inmediatly so I want to delete all the servers linkeds there for try again.

What script should I use or what command on T-SQL I have to run for delete all and without specifying the name of each server linked.

Thanks

like image 790
Jonathan Escobedo Avatar asked Dec 22 '22 06:12

Jonathan Escobedo


1 Answers

You can execute sp_dropserver for all linked servers using the database cursor. The following example shows how to do this.

    DECLARE @sql NVARCHAR(MAX)

    DECLARE db_cursor CURSOR FOR  
        select 'sp_dropserver ''' + [name] + '''' from sys.servers

    OPEN db_cursor   
    FETCH NEXT FROM db_cursor INTO @sql   

    WHILE @@FETCH_STATUS = 0   
    BEGIN   

           EXEC (@sql)

           FETCH NEXT FROM db_cursor INTO @sql   
    END   

    CLOSE db_cursor   
    DEALLOCATE db_cursor
like image 147
gyromonotron Avatar answered Dec 24 '22 18:12

gyromonotron