我知道我可以使用此查询获取SQL Server连接的IPAddress和ConnectionID:
从sys.dm_exec_connections中选择client_net_address,connection_id 其中session_id = @@ spid
但有没有办法获得相应的MAC地址?
感谢。
答案 0 :(得分:4)
见下文:
select net_address from sysprocesses where spid = @@SPID
答案 1 :(得分:2)
以下部分是sql命令,用于获取当前使用的网络的服务器MAC地址:
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
select * from @t
请注意,如果您有超过1个NIC,则此结果将仅显示一个。
答案 2 :(得分:1)
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
CREATE FUNCTION [dbo].[GetCurrentIP] ()
RETURNS varchar(255)
AS
BEGIN
DECLARE @IP_Address varchar(255);
SELECT @IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = @@SPID;
Return @IP_Address;
END
此代码来自[1]:http://www.codeproject.com/Tips/323721/How-to-get-Client-IP-Address-in-SQL-Server