Setting a variable from a statement that starts with EXEC
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?
sql-server t-sql linked-server
New contributor
add a comment |
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?
sql-server t-sql linked-server
New contributor
add a comment |
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?
sql-server t-sql linked-server
New contributor
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?
sql-server t-sql linked-server
sql-server t-sql linked-server
New contributor
New contributor
edited 9 mins ago
Solomon Rutzky
47.8k579173
47.8k579173
New contributor
asked Jan 18 at 22:14
user1566280user1566280
82
82
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try something like the following:
DECLARE @DbServerName sysname;
EXEC [TMR_DM_LS].[master].sys.sp_executesql
N'SELECT @DbServerName_tmp = @@SERVERNAME;',
N'@DbServerName_tmp sysname OUTPUT',
@DbServerName_tmp = @DbServerName OUTPUT;
SELECT @DbServerName AS [RemoteName];
HOWEVER, the above is how to return info in general. IF you are truly only wanting the server name, then I think you already have it on your local server. The Linked Server definition, depending on how it was configured, might very well have the name you are looking for:
DECLARE @DbServerName sysname;
SELECT @DbServerName = [data_source]
FROM sys.servers
WHERE [name] = N'TMR_DM_LS';
SELECT @DbServerName AS [RemoteName];
See if that works before doing the actual remote execution since it would be much more efficient to get the value locally.
UPDATE
O.P. replied that the desired value was indeed in the sys.servers
system catalog view.
add a comment |
You can't do it like that because you are returning a result set (via SELECT
) and not a scalar value. If you wanted to put the result into a variable you could do something like this:
DECLARE @version SYSNAME;
DECLARE @params NVARCHAR(MAX);
SET @params = '@server_name SYSNAME OUTPUT';
EXEC LinkedServer.[master].sys.sp_executesql
N'SELECT @version = @@VERSION;',
@params,
@version = @version OUTPUT;
SELECT @version;
Using the OUTPUT parameter (sp_executesql)
add a comment |
A simple script showing how to return the server name using openquery
the only difference is that here I put the returning values into variables
--first query
--to find out the available linked servers
select *
from sys.servers
where is_data_access_enabled = 1
and is_linked = 1
--ckecking it out
SELECT * FROM OPENQUERY (
[CTDB12], -- the name here must be one returned from the first query
'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
')
-- you need to have proper permissions
--OLE DB provider "SQLNCLI11" for linked server "CTDB12" returned message "Deferred prepare could not be completed.".
--Msg 297, Level 16, State 1, Line 8
--The user does not have permission to perform this action.
--lets try another server
SELECT * FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
--now lets declare variables to keep the info and show them
declare @DbServerName sysname
declare @theuser sysname
declare @IPAddress sysname
SELECT @DbServerName = TargetServerName,
@theuser = ConnectedWith,
@IPAddress = IPAddress
FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
print @DbServerName
print @theuser
print @IPAddress
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
user1566280 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227523%2fsetting-a-variable-from-a-statement-that-starts-with-exec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try something like the following:
DECLARE @DbServerName sysname;
EXEC [TMR_DM_LS].[master].sys.sp_executesql
N'SELECT @DbServerName_tmp = @@SERVERNAME;',
N'@DbServerName_tmp sysname OUTPUT',
@DbServerName_tmp = @DbServerName OUTPUT;
SELECT @DbServerName AS [RemoteName];
HOWEVER, the above is how to return info in general. IF you are truly only wanting the server name, then I think you already have it on your local server. The Linked Server definition, depending on how it was configured, might very well have the name you are looking for:
DECLARE @DbServerName sysname;
SELECT @DbServerName = [data_source]
FROM sys.servers
WHERE [name] = N'TMR_DM_LS';
SELECT @DbServerName AS [RemoteName];
See if that works before doing the actual remote execution since it would be much more efficient to get the value locally.
UPDATE
O.P. replied that the desired value was indeed in the sys.servers
system catalog view.
add a comment |
Try something like the following:
DECLARE @DbServerName sysname;
EXEC [TMR_DM_LS].[master].sys.sp_executesql
N'SELECT @DbServerName_tmp = @@SERVERNAME;',
N'@DbServerName_tmp sysname OUTPUT',
@DbServerName_tmp = @DbServerName OUTPUT;
SELECT @DbServerName AS [RemoteName];
HOWEVER, the above is how to return info in general. IF you are truly only wanting the server name, then I think you already have it on your local server. The Linked Server definition, depending on how it was configured, might very well have the name you are looking for:
DECLARE @DbServerName sysname;
SELECT @DbServerName = [data_source]
FROM sys.servers
WHERE [name] = N'TMR_DM_LS';
SELECT @DbServerName AS [RemoteName];
See if that works before doing the actual remote execution since it would be much more efficient to get the value locally.
UPDATE
O.P. replied that the desired value was indeed in the sys.servers
system catalog view.
add a comment |
Try something like the following:
DECLARE @DbServerName sysname;
EXEC [TMR_DM_LS].[master].sys.sp_executesql
N'SELECT @DbServerName_tmp = @@SERVERNAME;',
N'@DbServerName_tmp sysname OUTPUT',
@DbServerName_tmp = @DbServerName OUTPUT;
SELECT @DbServerName AS [RemoteName];
HOWEVER, the above is how to return info in general. IF you are truly only wanting the server name, then I think you already have it on your local server. The Linked Server definition, depending on how it was configured, might very well have the name you are looking for:
DECLARE @DbServerName sysname;
SELECT @DbServerName = [data_source]
FROM sys.servers
WHERE [name] = N'TMR_DM_LS';
SELECT @DbServerName AS [RemoteName];
See if that works before doing the actual remote execution since it would be much more efficient to get the value locally.
UPDATE
O.P. replied that the desired value was indeed in the sys.servers
system catalog view.
Try something like the following:
DECLARE @DbServerName sysname;
EXEC [TMR_DM_LS].[master].sys.sp_executesql
N'SELECT @DbServerName_tmp = @@SERVERNAME;',
N'@DbServerName_tmp sysname OUTPUT',
@DbServerName_tmp = @DbServerName OUTPUT;
SELECT @DbServerName AS [RemoteName];
HOWEVER, the above is how to return info in general. IF you are truly only wanting the server name, then I think you already have it on your local server. The Linked Server definition, depending on how it was configured, might very well have the name you are looking for:
DECLARE @DbServerName sysname;
SELECT @DbServerName = [data_source]
FROM sys.servers
WHERE [name] = N'TMR_DM_LS';
SELECT @DbServerName AS [RemoteName];
See if that works before doing the actual remote execution since it would be much more efficient to get the value locally.
UPDATE
O.P. replied that the desired value was indeed in the sys.servers
system catalog view.
edited 6 mins ago
answered Jan 18 at 22:40
Solomon RutzkySolomon Rutzky
47.8k579173
47.8k579173
add a comment |
add a comment |
You can't do it like that because you are returning a result set (via SELECT
) and not a scalar value. If you wanted to put the result into a variable you could do something like this:
DECLARE @version SYSNAME;
DECLARE @params NVARCHAR(MAX);
SET @params = '@server_name SYSNAME OUTPUT';
EXEC LinkedServer.[master].sys.sp_executesql
N'SELECT @version = @@VERSION;',
@params,
@version = @version OUTPUT;
SELECT @version;
Using the OUTPUT parameter (sp_executesql)
add a comment |
You can't do it like that because you are returning a result set (via SELECT
) and not a scalar value. If you wanted to put the result into a variable you could do something like this:
DECLARE @version SYSNAME;
DECLARE @params NVARCHAR(MAX);
SET @params = '@server_name SYSNAME OUTPUT';
EXEC LinkedServer.[master].sys.sp_executesql
N'SELECT @version = @@VERSION;',
@params,
@version = @version OUTPUT;
SELECT @version;
Using the OUTPUT parameter (sp_executesql)
add a comment |
You can't do it like that because you are returning a result set (via SELECT
) and not a scalar value. If you wanted to put the result into a variable you could do something like this:
DECLARE @version SYSNAME;
DECLARE @params NVARCHAR(MAX);
SET @params = '@server_name SYSNAME OUTPUT';
EXEC LinkedServer.[master].sys.sp_executesql
N'SELECT @version = @@VERSION;',
@params,
@version = @version OUTPUT;
SELECT @version;
Using the OUTPUT parameter (sp_executesql)
You can't do it like that because you are returning a result set (via SELECT
) and not a scalar value. If you wanted to put the result into a variable you could do something like this:
DECLARE @version SYSNAME;
DECLARE @params NVARCHAR(MAX);
SET @params = '@server_name SYSNAME OUTPUT';
EXEC LinkedServer.[master].sys.sp_executesql
N'SELECT @version = @@VERSION;',
@params,
@version = @version OUTPUT;
SELECT @version;
Using the OUTPUT parameter (sp_executesql)
answered Jan 18 at 22:42
Mr.BrownstoneMr.Brownstone
8,92932342
8,92932342
add a comment |
add a comment |
A simple script showing how to return the server name using openquery
the only difference is that here I put the returning values into variables
--first query
--to find out the available linked servers
select *
from sys.servers
where is_data_access_enabled = 1
and is_linked = 1
--ckecking it out
SELECT * FROM OPENQUERY (
[CTDB12], -- the name here must be one returned from the first query
'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
')
-- you need to have proper permissions
--OLE DB provider "SQLNCLI11" for linked server "CTDB12" returned message "Deferred prepare could not be completed.".
--Msg 297, Level 16, State 1, Line 8
--The user does not have permission to perform this action.
--lets try another server
SELECT * FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
--now lets declare variables to keep the info and show them
declare @DbServerName sysname
declare @theuser sysname
declare @IPAddress sysname
SELECT @DbServerName = TargetServerName,
@theuser = ConnectedWith,
@IPAddress = IPAddress
FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
print @DbServerName
print @theuser
print @IPAddress
add a comment |
A simple script showing how to return the server name using openquery
the only difference is that here I put the returning values into variables
--first query
--to find out the available linked servers
select *
from sys.servers
where is_data_access_enabled = 1
and is_linked = 1
--ckecking it out
SELECT * FROM OPENQUERY (
[CTDB12], -- the name here must be one returned from the first query
'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
')
-- you need to have proper permissions
--OLE DB provider "SQLNCLI11" for linked server "CTDB12" returned message "Deferred prepare could not be completed.".
--Msg 297, Level 16, State 1, Line 8
--The user does not have permission to perform this action.
--lets try another server
SELECT * FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
--now lets declare variables to keep the info and show them
declare @DbServerName sysname
declare @theuser sysname
declare @IPAddress sysname
SELECT @DbServerName = TargetServerName,
@theuser = ConnectedWith,
@IPAddress = IPAddress
FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
print @DbServerName
print @theuser
print @IPAddress
add a comment |
A simple script showing how to return the server name using openquery
the only difference is that here I put the returning values into variables
--first query
--to find out the available linked servers
select *
from sys.servers
where is_data_access_enabled = 1
and is_linked = 1
--ckecking it out
SELECT * FROM OPENQUERY (
[CTDB12], -- the name here must be one returned from the first query
'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
')
-- you need to have proper permissions
--OLE DB provider "SQLNCLI11" for linked server "CTDB12" returned message "Deferred prepare could not be completed.".
--Msg 297, Level 16, State 1, Line 8
--The user does not have permission to perform this action.
--lets try another server
SELECT * FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
--now lets declare variables to keep the info and show them
declare @DbServerName sysname
declare @theuser sysname
declare @IPAddress sysname
SELECT @DbServerName = TargetServerName,
@theuser = ConnectedWith,
@IPAddress = IPAddress
FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
print @DbServerName
print @theuser
print @IPAddress
A simple script showing how to return the server name using openquery
the only difference is that here I put the returning values into variables
--first query
--to find out the available linked servers
select *
from sys.servers
where is_data_access_enabled = 1
and is_linked = 1
--ckecking it out
SELECT * FROM OPENQUERY (
[CTDB12], -- the name here must be one returned from the first query
'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
')
-- you need to have proper permissions
--OLE DB provider "SQLNCLI11" for linked server "CTDB12" returned message "Deferred prepare could not be completed.".
--Msg 297, Level 16, State 1, Line 8
--The user does not have permission to perform this action.
--lets try another server
SELECT * FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
--now lets declare variables to keep the info and show them
declare @DbServerName sysname
declare @theuser sysname
declare @IPAddress sysname
SELECT @DbServerName = TargetServerName,
@theuser = ConnectedWith,
@IPAddress = IPAddress
FROM OPENQUERY (
[QG-V-SQLRPL1-PR], -- the name here must be one returned from the first query
'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
')
print @DbServerName
print @theuser
print @IPAddress
answered Jan 18 at 22:49
marcello miorellimarcello miorelli
5,7041962129
5,7041962129
add a comment |
add a comment |
user1566280 is a new contributor. Be nice, and check out our Code of Conduct.
user1566280 is a new contributor. Be nice, and check out our Code of Conduct.
user1566280 is a new contributor. Be nice, and check out our Code of Conduct.
user1566280 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227523%2fsetting-a-variable-from-a-statement-that-starts-with-exec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown