SQL語句完成查詢SQL Server辦事器稱號和IP地址。本站提示廣大學習愛好者:(SQL語句完成查詢SQL Server辦事器稱號和IP地址)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL語句完成查詢SQL Server辦事器稱號和IP地址正文
獲得辦事器稱號:
SELECT SERVERPROPERTY('MachineName') select @@SERVERNAME select HOST_NAME()
獲得IP地址可使用xp_cmdshell履行ipconfig敕令:
--開啟xp_cmdshell exec sp_configure'show advanced options', 1 reconfigure with override exec sp_configure'xp_cmdshell', 1 reconfigure with override exec sp_configure'show advanced options', 0 reconfigure with override go begin declare @ipline varchar(200) declare @pos int declare @ip varchar(40) set nocount on set @ip = null if object_id('tempdb..#temp') is not null drop table #temp create table #temp(ipline varchar(200)) insert #temp exec master..xp_cmdshell'ipconfig' select @ipline = ipline from #temp where upper(ipline) like '%IPv4 地址%'--這裡須要留意一下,體系分歧這裡的婚配值就分歧 if @ipline is not null begin set @pos = charindex(':',@ipline,1); set @ip = rtrim(ltrim(substring(@ipline , @pos + 1 , len(@ipline) - @pos))) end select distinct(rtrim(ltrim(substring(@ipline , @pos + 1 , len(@ipline) - @pos)))) as ipaddress from #temp drop table #temp set nocount off end go
然則許多情形下因為平安成績是不許可應用xp_cmdshell,可以經由過程查詢SYS.DM_EXEC_CONNECTIONS :
SELECT SERVERNAME = CONVERT(NVARCHAR(128),SERVERPROPERTY('SERVERNAME')) ,LOCAL_NET_ADDRESS AS 'IPAddressOfSQLServer' ,CLIENT_NET_ADDRESS AS 'ClientIPAddress' FROM SYS.DM_EXEC_CONNECTIONS WHERE SESSION_ID = @@SPID