March 1, 2021 at 10:58 pm
Hi,
I want to retrieve the name of the remote server in my SQL server query:
exec ('CREATE OR ALTER PROCEDURE ins_tmp_proc AS
BEGIN
INSERT INTO dbo.tmp (job_id, name, ..., server)
SELECT job_id, name, .., @@SERVERNAME from msdb.dbo.sysjobs
UNION
SELECT job_id, name, .., @@SERVERNAME from <remoteServer>.msdb.dbo.sysjobs
END')
I tried executing just the SELECT statement by itself and I see that @@SERVERNAME part returns the current server name (server name on which the query is run) even for the remoteServer. I tried serverproperty('MachineName') but it still returns the current server name and not the remote server name.
Is there a way to return the name of the remote server in a remote query like above?
Thanks.
March 2, 2021 at 12:34 am
I guess I'm a little confused... You already know the remote server name or you wouldn't be able to query its' msdb database. Why not just substitute a literal for @@SERVERNAME in the unioned query?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2021 at 1:02 am
The way we do this at one of our servers is this:
exec ('CREATE OR ALTER PROCEDURE ins_tmp_proc AS
BEGIN
INSERT INTO dbo.tmp (job_id, name, ..., server)
SELECT job_id, name, .., ' NAME_OF_MY_REMOTE_SERVER' from msdb.dbo.sysjobs
UNION
SELECT job_id, name, .., @@SERVERNAME from <remoteServer>.msdb.dbo.sysjobs
END') AT NAME_OF_MY_REMOTE_SERVER
the above will execute whatever code is between EXEC ('
and ')
AT the remote server whose name is NAME_OF_MY_REMOTE_SERVER, which you obviously KNOW before you attempt to execute any code on it 🙂
Likes to play Chess
March 2, 2021 at 1:09 am
'AT' is the actual keyword, part of the syntax. You can run it like the above example from an Agent Job, for example..
and also you need to remember to do
USE MyDatabaseName;
statement before CREATE or ALTER PROC, as in your example. As you are creating that Proc in a particular database. The name of which you very likely know as well.
Likes to play Chess
March 2, 2021 at 1:23 am
I'm thinking that you've changed the purpose of the code. The first query in the code uses the 3 part naming convention and seems to be meant to look at msdb on the current instance and only the second query is meant to look at a different instance.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2021 at 7:13 pm
Haa. How did I miss it? Such a simple solution!
March 2, 2021 at 7:28 pm
Haa. How did I miss it? Such a simple solution!
Curious... which solution?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2021 at 7:47 pm
exec ('CREATE OR ALTER PROCEDURE ins_tmp_proc AS
BEGIN
INSERT INTO dbo.tmp (job_id, name, ..., server)
SELECT job_id, name, ..., @@SERVERNAME from msdb.dbo.sysjobs
UNION ALL
SELECT job_id, name, ..., ''<remoteserver>'' from <remoteServer>.msdb.dbo.sysjobs
END')
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
March 2, 2021 at 7:50 pm
by the way, since we already have touched the subject,
is it a good idea at all to execute anything like this on a remote server as EXEC (...) AT <remote server>?
Linked servers/ running ad hoc qry on it like that seems like not a best practice, entailing many issues..
Likes to play Chess
March 2, 2021 at 7:55 pm
by the way, since we already have touched the subject,
is it a good idea at all to execute anything like this on a remote server as EXEC (...) AT <remote server>?
Linked servers/ running ad hoc qry on it like that seems like not a best practice, entailing many issues..
Yes, it's often a very good idea, particularly when there's a (very) large table on the remote box that needs to be processed.
As a quick example, say I'm joining a 2-row local table to a 2M row remote table. If I do the request locally, SQL has to bring all 2M rows to the local box. If I run it remotely, SQL has to bring the 2 rows over. Obviously the latter is vastly preferable for performance.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
March 2, 2021 at 8:16 pm
by the way, since we already have touched the subject,
is it a good idea at all to execute anything like this on a remote server as EXEC (...) AT <remote server>?
Linked servers/ running ad hoc qry on it like that seems like not a best practice, entailing many issues..
I agree with Scott on this but you actually have two different subjects here. The first subject is running the query AT the remote server. Scott covered that as being a good thing to do.
It being an "ad hoc" query might not be so good. It would, IMHO, be better to have a stored procedure on the remote server and execute it using a remote call that uses AT. Of course, "It Depends" a bit on what else is going on and there are other ways to even handle queries to different RDBMS's without having to have a stored procedure on the remote box (OpenQuery across a linked server actually does work a treat in a similar fashion). 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2021 at 8:19 pm
My thanks to Jeff, Voldemar and Scott.
@jeff, I was thinking of getting the remote server name through some variable or a function in remote server while I missed the obvious solution of using the servername literal in single quotes, like you all have mentioned. Bit embarassing.
March 3, 2021 at 1:55 am
My thanks to Jeff, Voldemar and Scott.
@jeff, I was thinking of getting the remote server name through some variable or a function in remote server while I missed the obvious solution of using the servername literal in single quotes, like you all have mentioned. Bit embarassing.
BAWAAA-HAAAA!!! Been there, done that. I remember in my early days with computers (before PC's came out) and I was using some software to draw WBS charts. My boss did a review of the final chart after I printed it out and he said "I need a dotted line for this node to that node". I told him the software wouldn't do that. He smiled, opened his desk drawer, pulled out a ruler and a pen and said, "Do I need to repeat myself"?
Anyway, thanks for the feedback. Really appreciate it. To be honest, I though it might be me that was missing something.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 8, 2021 at 6:55 pm
Ram wrote:My thanks to Jeff, Voldemar and Scott.
@jeff, I was thinking of getting the remote server name through some variable or a function in remote server while I missed the obvious solution of using the servername literal in single quotes, like you all have mentioned. Bit embarassing.
BAWAAA-HAAAA!!! Been there, done that. I remember in my early days with computers (before PC's came out) and I was using some software to draw WBS charts. My boss did a review of the final chart after I printed it out and he said "I need a dotted line for this node to that node". I told him the software wouldn't do that. He smiled, opened his desk drawer, pulled out a ruler and a pen and said, "Do I need to repeat myself"?
Anyway, thanks for the feedback. Really appreciate it. To be honest, I though it might be me that was missing something.
Ahh, the old "if the software can't do it, maybe you need more hardware" solution.
March 8, 2021 at 7:43 pm
Jeff Moden wrote:Ram wrote:My thanks to Jeff, Voldemar and Scott.
@jeff, I was thinking of getting the remote server name through some variable or a function in remote server while I missed the obvious solution of using the servername literal in single quotes, like you all have mentioned. Bit embarassing.
BAWAAA-HAAAA!!! Been there, done that. I remember in my early days with computers (before PC's came out) and I was using some software to draw WBS charts. My boss did a review of the final chart after I printed it out and he said "I need a dotted line for this node to that node". I told him the software wouldn't do that. He smiled, opened his desk drawer, pulled out a ruler and a pen and said, "Do I need to repeat myself"?
Anyway, thanks for the feedback. Really appreciate it. To be honest, I though it might be me that was missing something.
Ahh, the old "if the software can't do it, maybe you need more hardware" solution.
Heh... now THAT'S FUNNY!!!! 😀 Thanks for the great laugh... I needed it!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply