variable for a linked server name

  • How do I use a variable name for a linked server name in a linked server query?

    set @servername = (SELECT TOP (1) [ServerName]

    FROM [Database].[dbo].[Servers]

    declare @servername =

    SELECT * FROM [SERVER_NAME].[DATABASE_NAME].[SCHEMA_NAME].[TABLE_NAME]

    SELECT * FROM [@servername ].[DATABASE_NAME].[SCHEMA_NAME].[TABLE_NAME]

     

    Ideally how can I backup all servers/databases onto a single fileshare from a single server as a sqljob ?One server will be chosen from this query SELECT TOP (1) [ServerName]

    FROM [Database].[dbo].[Servers] every time the sqljob runs.

    • This topic was modified 2 months, 1 week ago by  mtz676.
    • This topic was modified 2 months, 1 week ago by  mtz676.
  • Dynamic SQL is one way of creating a variable SELECT statement that can be executed.

    • This reply was modified 2 months, 1 week ago by  Phil Parkin.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply