Skip to content
Advertisement

Iterate through a list of servers in SQL Server

I have a script similar to below and get:

Could not find server ‘@CURSERVER’ in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

I am trying to get SQL Server Agent jobs from all linked servers. I don’t think there is a problem with my linked servers but can I not have the server name as a variable?

Advertisement

Answer

You cannot use a variable as a identifier (server name, database name, table name, etc.). Instead, construct a dynamic SQL statement and execute.

Below is an example including corrections and improvements to your original script.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement