SQL语句实现查询SQL Server服务器名称和IP地址
更新时间:2015年07月07日 10:34:23 投稿:junjie
这篇文章主要介绍了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
相关文章
SQLServer性能优化--间接实现函数索引或者Hash索引
本文主要介绍了SQLServer性能优化--间接实现函数索引或者Hash索引的解决方式。具有很好的参考价值。下面跟着小编一起来看下吧2017-03-03
浅述SQL Server的语句类别 数据库范式 系统数据库组成
本文着重讲解了SQL语句的组成以及数据库的三个范式,对系统数据库的组成进行简短的介绍。有兴趣的朋友可以看下2016-12-12
SQL Server误区30日谈 第12天 TempDB的文件数和需要和CPU数目保持一致
TempDB的文件没有必要分布在多个存储器之间。如果你看到PAGELATCH类型的等待,即使你进行了分布也不会改善性能,而如果PAGEIOLATCH型的等待,或许你需要多个存储器,但这也不是必然-有可能你需要讲整个TempDB迁移到另一个存储系统,而不是仅仅为TempDB增加一个文件2013-01-01


最新评论