I am trying to get the Server Name from a LinkedServer and assign it to a variable.
From this question (Get @@SERVERNAME from linked server), I found that either of these return the Server Name.
EXEC LinkedServer.[master].sys.sp_executesql N'SELECT @@VERSION;';
or
EXEC('SELECT @@VERSION;') AT LinkedServer;
Yet, when I try to assign that value to a parameter, SQL Server states it's incorrect:
SET @DbServerName = (EXEC TMR_DM_LS.[master].sys.sp_executesql N'SELECT @@SERVERNAME')
Msg 156, Level 15, State 1, Line XXXX Incorrect syntax near the keyword 'EXEC'.
Msg 102, Level 15, State 1, Line XXXX Incorrect syntax near ')'
What am I doing wrong?