Get @@SERVERNAME from linked server

Some slightly shorter (and more natural, IMHO) approaches:

EXEC LinkedServer.[master].sys.sp_executesql N'SELECT @@VERSION;';

Or:

EXEC('SELECT @@VERSION;') AT LinkedServer;

I prefer the sp_executesql route because:

  1. It will still allow for strongly-typed parameters, instead of dealing with all kinds of string concatenation, escaping of single quotes, etc.

  2. It is easy to specify a particular database in that path, instead of having to put database prefixes on all the objects referenced in the query. And yes, you can define the database dynamically:

    DECLARE @db SYSNAME, @exec NVARCHAR(1024);
    SET @db = N'tempdb';
    SET @exec = N'LinkedServer.' + QUOTENAME(@db) + N'.sys.sp_executesql';
    EXEC @exec N'SELECT @@VERSION, DB_NAME();';
    

    And the linked server name, too, if need be:

    DECLARE @srv SYSNAME, @db SYSNAME, @exec NVARCHAR(1024);
    SET @srv = N'LinkedServer';
    SET @db = N'tempdb';
    SET @exec = QUOTENAME(@srv) + N'.' + QUOTENAME(@db) + N'.sys.sp_executesql';
    EXEC @exec N'SELECT @@VERSION, DB_NAME();';
    

You can use OPENQUERY as explained in MSDN to get the information of Linked server:

i.e using OPENQUERY ( linked_server ,'query' )

with something like below will give you server name using sys.dm_exec_connections dmv

SELECT * FROM OPENQUERY (
         linkedservername, 
         'SELECT
             @@SERVERNAME AS TargetServerName,
             SUSER_SNAME() AS ConnectedWith,
             DB_NAME() AS DefaultDB,
             client_net_address AS IPAddress
          FROM
              sys.dm_exec_connections
          WHERE
              session_id = @@SPID
        ')