views:

82

answers:

4

Was wondering if it was possible to get the mac address of the server using a stored procedure? I did some searching for a while but found nothing. This is using SQL2008.

Update
Unfortunately the answer that uses newsequentialid() can return the wrong MacAddress if there is VMWare on the server.

A: 

Well, since in Sql Server 2008, a Stored proc can contained managed code, it should be possible, however, you'd technically be running a c#/vb app which was launched by SqlServer, rather than truly getting the info from a stored proc, although that more of a theoric rather than practical difference.

James Curran
Managed procedures *are* stored procedures, the term technically covers both T-SQL and CLR. And there is no 'app launched by SQL Server' either, SQL Server *is* a CLR host itself, it does not launch apps to run managed code.
Remus Rusanu
Granted I phrased that badly. how 'bout "SQL doesn't enter into this solution, so it's not really a stored proc in the conventional sense, but it's run like one"
James Curran
Actually this doesn't work because you need to creat a SQLServerProject to deploy and debug CLR stored procedures.SQLServerProject doesn't support System.Managment.
Donny V.
You don't need System.Management. You want System.Net.NetworkInformation, which is available.
James Curran
Check out this question http://stackoverflow.com/questions/151231/how-do-i-get-the-local-network-ip-address-of-a-computer-programmatically-c (Skipped the accepted answer and read the next one or mine further down)
James Curran
I'm looking at the available references and I'm not seeing System.NET.
Donny V.
@Donny: System.Net.NetworkInformation is a namespace, not an assembly. It is contained in the System.dll assembly.
James Curran
+1  A: 

sys.sysprocesses, net_address column. For my connection from SSMS it's 795C70BAD9B0

There is no equivalent in sys.dm_exec_connections (net_address is IP address not MAC address and is 111.222.111.222 from my SSMS). In fact there is no sys.sysprocesses equivalent either

So you can just query sys.sysprocesses in your own stored proc...

gbn
But that's the address of a client connecting to the server, which is not necessarily connecting from the same machine. As far as I can see, internal system processes don't have that column filled.
James Curran
+2  A: 

I would guess that you'd need to execute a shell command from SQL to get the MAC address. If I recall correctly, you have to turn on the execute shell command option before you can use it. Then, you could run "getmac" to retrieve a list of MAC address for the interfaces on the server. You'd have to work your way through the text returned, but that shouldn't be too hard.

Darkwater23
+3  A: 

A somewhat round about method!

declare @t table
(
i uniqueidentifier default newsequentialid(),
m as cast(i as char(36))
)

insert into @t default values;

select
    substring(m,25,2) + '-' + 
    substring(m,27,2) + '-' + 
    substring(m,29,2) + '-' +
    substring(m,31,2) + '-' +
    substring(m,33,2) + '-' +
    substring(m,35,2) AS MacAddress
FROM @t
Martin Smith
oooh... very clever...
gbn
<strike>It works like a charm but I still don't understand how its getting it.</strike> I get it, newsequentialid uses mac addresses. Thank you by the way.
Donny V.
Unfortunately newsequentialid() can return the wrong MacAddress if there is VMWare on the server.
Donny V.
@Donny - Ah to be honest I felt a bit uncomfortable relying on that anyway. I guess the algorithm could change without warning at some point in the future.
Martin Smith