Saturday, February 25, 2012

Capturing Server Name?

Hi y'all...long time, I know...

I have what OUGHT to be a simple question...or so I think (which really hurts, mind you...)

I am writing a stored proc that will reside on several different databases and be used to write a row to a "wait table" that is used to control processing in the various databases...

Essentially, the stored proc already exists, and writes what is essentially a note (or process semaphore) that says "Hey, Process XYZ is waiting on the completion of process 123"

Problem is...process XYZ has the same name on a number of different servers, so I have to come up with a way to differentiate the process name that's waiting on a job on a single server (in other words, 5 or more XYZ's can be waiting on a single job on a single server in the network, and the wait table resides on that single server).

So...my thought (again, think pain) is that I will put a process name of "SERVER.XYZ" into the wait table.

The SP I will use to write the "waiting on" semaphore is a common one, so - long story short(er) I need a way to capture the name of the current server (like db_name(), only server_name() - or something like it).

Any suggestions? Thanks in advance...
Paulselect SERVERPROPERTY ('ServerName')

that'll do it...

Thanks for reading though!|||SELECT @.@.SERVERNAME may also work for you...|||Do you mean the name of the server that the database is on? If so, then you have your solution. If you wanted the client server's name, you could use:

select hostname
from master..sysprocesses
where spid = @.@.spid

If you go this route, you will have to make sure that all of your connection strings supply a hostname, though. Not all applications do. Also, the hostname can be spoofed, so be careful about hardcoding that. Good luck.|||...or simply HOST_NAME()|||I'll be darned. Microsoft actually came up with something that might be useful. I may have to try that with an application that spoofs a hostname, and see what happens. Thanks, rdjabarov.

No comments:

Post a Comment